Ejemplo n.º 1
0
    def forward(self, s="/tmp/stockholm/", S="/tmp/stockholm_", N=5):
        ''' Motion 1-iteration forward 2D DWT of a sequence of images.

        Compute the 2D-DWT of each image of the sequence s.

        Input:
        -----

            s: the sequence of images to be transformed.

        Output:
        ------

            S: the sequence of decompositions (transformed images).

        '''
        for i in range(N):
            img = image.read("{}{:03d}".format(s, i))
            pyr = self.dwt.forward(img)
            decomposition.write(pyr, "{}{:03d}".format(S, i))
Ejemplo n.º 2
0
Archivo: MDWT.py Proyecto: urdi10/MCDWT
    def forward(self, prefix="/tmp/", N=5):
        '''Motion 1-iteration forward 2D DWT of a sequence of images.

        Compute the forward 2D-DWT of each image of the sequence
        placed at <prefix>.

        Input
        -----

            prefix: the sequence of images to be transformed.
            N: number of images.

        Output
        ------

            (disk): the sequence of decompositions.

        '''
        for i in range(N):
            img = image.read(prefix, "{:03d}".format(i))
            pyr = self.dwt.forward(img)
            decomposition.write(pyr, prefix, "{:03d}".format(i))
Ejemplo n.º 3
0
    def forward(self, s="/tmp/stockholm_", S="/tmp/mc_stockholm_", N=5, T=2):
        '''A Motion Compensated Discrete Wavelet Transform.

        Compute the MC 1D-DWT. The input video s (as a sequence of
        1-levels decompositions) must be stored in disk and the output (as a
        1-levels MC decompositions) will be stored in S.

        Imput:
        -----

            prefix : s

                Localization of the input images. Example: "/tmp/stockholm_".

             N : int

                Number of images to process.

             T : int

                Number of levels of the MCDWT (temporal scales). Controls
                the GOP size.

                  T | GOP_size
                ----+-----------
                  0 |        1
                  1 |        2
                  2 |        4
                  3 |        8
                  4 |       16
                  5 |       32
                  : |        :

        Returns
        -------

            prefix : S

                Localization of the output decompositions. For example:
                "/tmp/mc_stockholm_".

        '''
        x = 2
        for t in range(T):  # Temporal scale
            i = 0
            aL, aH = decomposition.read("{}{:03d}".format(s, 0))
            decomposition.write((aL, aH), "{}{:03d}".format(S, 0))
            while i < (N // x):
                bL, bH = decomposition.read("{}{:03d}".format(
                    s, x * i + x // 2))
                cL, cH = decomposition.read("{}{:03d}".format(s, x * i + x))
                bH = self.__forward_butterfly(aL, aH, bL, bH, cL, cH)
                decomposition.write((bL, bH),
                                    "{}{:03d}".format(S, x * i + x // 2))
                decomposition.write((cL, cH), "{}{:03d}".format(S, x * i + x))
                aL, aH = cL, cH
                i += 1
            x *= 2
Ejemplo n.º 4
0
 def backward(self, S="/tmp/mc_stockholm_", s="/tmp/stockholm_", N=5, T=2):
     x = 2**T
     for t in range(T):  # Temporal scale
         i = 0
         aL, aH = decomposition.read("{}{:03d}".format(S, 0))
         decomposition.write((aL, aH), "{}{:03d}".format(s, 0))
         while i < (N // x):
             bL, bH = decomposition.read("{}{:03d}".format(
                 S, x * i + x // 2))
             cL, cH = decomposition.read("{}{:03d}".format(S, x * i + x))
             bH = self.__backward_butterfly(aL, aH, bL, bH, cL, cH)
             decomposition.write((bL, bH),
                                 "{}{:03d}".format(s, x * i + x // 2))
             decomposition.write((cL, cH), "{}{:03d}".format(s, x * i + x))
             aL, aH = cL, cH
             i += 1
         x //= 2
Ejemplo n.º 5
0
                        "--index",
                        help="Index of the image/decomposition",
                        default="000")
    parser.add_argument("-w",
                        "--wavelet",
                        help="Wavelet name",
                        default="bior3.5")
    parser.add_argument("-s",
                        "--show",
                        action='store_true',
                        help="Show available wavelet names")

    args = parser.parse_args()
    dwt = DWT(wavelet=args.wavelet)

    if args.show:
        dwt.show()
    else:
        if args.backward:
            if __debug__:
                print("Backward transform")
            d = decomposition.read(args.prefix, args.index)
            i = dwt.backward(d)
            image.write(i, args.prefix, args.index)
        else:
            if __debug__:
                print("Forward transform")
            i = image.read(args.prefix, args.index)
            d = dwt.forward(i)
            decomposition.write(d, args.prefix, args.index)
Ejemplo n.º 6
0
                        "--image",
                        help="Image to be transformed",
                        default="/tmp/stockholm/000")

    parser.add_argument("-d",
                        "--decomposition",
                        help="Decomposition to be transformed",
                        default="/tmp/stockholm_000")

    args = parser.parse_args()

    dwt = DWT()
    if args.backward:
        if __debug__:
            print("Backward transform")
        d = decomposition.read("{}".format(args.decomposition))
        i = dwt.backward(d)
        #i = np.rint(i)
        image.write(i, "{}".format(args.image))
    else:
        if __debug__:
            print("Forward transform")
        i = image.read("{}".format(args.image))
        d = dwt.forward(i)
        #LL = np.rint(d[0])
        #LH = np.rint(d[1][0])
        #HL = np.rint(d[1][1])
        #HH = np.rint(d[1][2])
        #decomposition.write((LL, (LH, HL, HH)), "{}".format(args.decomposition))
        decomposition.write(d, "{}".format(args.decomposition))
Ejemplo n.º 7
0
    def forward_(prefix="/tmp/", N=5, K=2):
        '''A Motion Compensated Discrete Wavelet Transform.

        Compute the MC 1D-DWT. The input video (as a sequence of images)
        must be stored in disk (<input> directory) and the output (as a
        sequence of DWT coefficients that are called decompositions) will be
        stored in disk (<output> directory).

        Arguments
        ---------

            prefix : str

                Localization of the input/output images. Example:
                "/tmp/".

             N : int

                Number of images to process.

             K : int

                Number of levels of the MCDWT (temporal scales). Controls
                the GOP size. 

                  K | GOP_size
                ----+-----------
                  0 |        1
                  1 |        2
                  2 |        4
                  3 |        8
                  4 |       16
                  5 |       32
                  : |        :

        Returns
        -------

            None.

        '''

        # import ipdb; ipdb.set_trace()
        #k = 0
        for k in range(K):  # spatial scale
            x = 2
            while x < N:
                i = 0  # first image of the butterfly
                A = image.read("{}{:03d}_{}".format(prefix, i, k))
                dwtA = dwt.forward(A)
                L_y = dwtA[0].shape[0]
                L_x = dwtA[0].shape[1]
                decomposition.write(dwtA,
                                    "{}{:03d}_{}".format(prefix, i, k + 1))
                zero_L = np.zeros(dwtA[0].shape, np.float64)
                zero_H = (zero_L, zero_L, zero_L)
                AL = dwt.backward(dwtA[0], zero_H)
                if __debug__:
                    image.write(AL,
                                "{}{:03d}_{}".format(prefix + "_AL_", i, k))
                AH = dwt.backward(zero_L, dwtA[1])
                if __debug__:
                    image.write(AH,
                                "{}{:03d}_{}".format(prefix + "_AH_", i, k))
                while i < (N // x):
                    print("k={} i={} x={} B={} C={}".format(
                        k, i, x, x * i + x // 2, x * i + x))
                    B = image.read("{}{:03d}_{}".format(
                        prefix, x * i + x // 2, k))
                    dwtB = dwt.forward(B)
                    BL = dwt.backward(dwtB[0], zero_H)
                    BH = dwt.backward(zero_L, dwtB[1])
                    C = image.read("{}{:03d}_{}".format(prefix, x * i + x, k))
                    dwtC = dwt.forward(C)
                    decomposition.write(
                        dwtC, "{}{:03d}_{}".format(prefix, x * i + x, k + 1))
                    CL = dwt.backward(dwtC[0], zero_H)
                    if __debug__:
                        image.write(
                            CL, "{}{:03d}_{}".format(prefix + "_CL_",
                                                     x * i + x, k))
                    CH = dwt.backward(zero_L, dwtC[1])
                    if __debug__:
                        image.write(
                            CH, "{}{:03d}_{}".format(prefix + "_CH_",
                                                     x * i + x, k))

                    if __debug__:
                        BLA = motion_compensation(AL, BL, AL)
                        BLC = motion_compensation(CL, BL, CL)
                        prediction = (BLA + BLC) / 2
                        image.write(
                            prediction,
                            "{}{:03d}_{}".format(prefix + "_prediction_L_",
                                                 x * i + x // 2, k))

                    BHA = motion_compensation(AL, BL, AH)
                    BHC = motion_compensation(CL, BL, CH)
                    if __debug__:
                        image.write(
                            BH, "{}{:03d}_{}".format(prefix + "_BH_",
                                                     x * i + x // 2, k))
                    prediction = (BHA + BHC) / 2
                    if __debug__:
                        image.write(
                            prediction,
                            "{}{:03d}_{}".format(prefix + "_prediction_",
                                                 x * i + x // 2, k))
                    rBH = BH - prediction
                    if __debug__:
                        image.write(
                            rBH, "{}{:03d}_{}".format(prefix + "_residue_",
                                                      x * i + x // 2, k))
                    rBH = dwt.forward(rBH)
                    rBH[0][0:L_y, 0:L_x, :] = dwtB[0]
                    decomposition.write(
                        rBH, "{}{:03d}_{}".format(prefix, x * i + x // 2,
                                                  k + 1))
                    AL = CL
                    AH = CH
                    i += 1
                x *= 2