Esempio n. 1
0
    def __init__(self, *args):  # (image, height, filter1, filter2, edges)
        self.pyrType = 'Laplacian'
        if len(args) > 0:
            self.image = args[0]
        else:
            print "pyr = Lpyr(image, height, filter1, filter2, edges)"
            print "First argument (image) is required"
            return

        if len(args) > 2:
            filt1 = args[2]
            if isinstance(filt1, basestring):
                filt1 = namedFilter(filt1)
            elif len(filt1.shape) != 1 and (filt1.shape[0] != 1
                                            and filt1.shape[1] != 1):
                print "Error: filter1 should be a 1D filter (i.e., a vector)"
                return
        else:
            filt1 = namedFilter('binom5')
        if len(filt1.shape) == 1:
            filt1 = filt1.reshape(1, len(filt1))
        elif self.image.shape[0] == 1:
            filt1 = filt1.reshape(filt1.shape[1], filt1.shape[0])

        if len(args) > 3:
            filt2 = args[3]
            if isinstance(filt2, basestring):
                filt2 = namedFilter(filt2)
            elif len(filt2.shape) != 1 and (filt2.shape[0] != 1
                                            and filt2.shape[1] != 1):
                print "Error: filter2 should be a 1D filter (i.e., a vector)"
                return
        else:
            filt2 = filt1

        maxHeight = 1 + maxPyrHt(self.image.shape, filt1.shape)

        if len(args) > 1:
            if args[1] is "auto":
                self.height = maxHeight
            else:
                self.height = args[1]
                if self.height > maxHeight:
                    print("Error: cannot build pyramid higher than %d levels" %
                          (maxHeight))
                    return
        else:
            self.height = maxHeight

        if len(args) > 4:
            edges = args[4]
        else:
            edges = "reflect1"

        # make pyramid
        self.pyr = []
        self.pyrSize = []
        pyrCtr = 0
        im = numpy.array(self.image).astype(float)
        if len(im.shape) == 1:
            im = im.reshape(im.shape[0], 1)
        los = {}
        los[self.height] = im
        # compute low bands
        for ht in range(self.height - 1, 0, -1):
            im_sz = im.shape
            filt1_sz = filt1.shape
            if im_sz[0] == 1:
                lo2 = corrDn(image=im, filt=filt1, edges=edges, step=(1, 2))
                #lo2 = numpy.array(lo2)
            elif len(im_sz) == 1 or im_sz[1] == 1:
                lo2 = corrDn(image=im, filt=filt1, edges=edges, step=(2, 1))
                #lo2 = numpy.array(lo2)
            else:
                lo = corrDn(image=im,
                            filt=filt1.T,
                            edges=edges,
                            step=(1, 2),
                            start=(0, 0))
                #lo = numpy.array(lo)
                lo2 = corrDn(image=lo,
                             filt=filt1,
                             edges=edges,
                             step=(2, 1),
                             start=(0, 0))
                #lo2 = numpy.array(lo2)

            los[ht] = lo2

            im = lo2

        # adjust shape if 1D if needed
        self.pyr.append(lo2.copy())
        self.pyrSize.append(lo2.shape)

        # compute hi bands
        im = self.image
        for ht in range(self.height, 1, -1):
            im = los[ht - 1]
            im_sz = los[ht - 1].shape
            filt2_sz = filt2.shape
            if len(im_sz) == 1 or im_sz[1] == 1:
                hi2 = upConv(image=im,
                             filt=filt2.T,
                             edges=edges,
                             step=(1, 2),
                             stop=(los[ht].shape[1], los[ht].shape[0])).T
            elif im_sz[0] == 1:
                hi2 = upConv(image=im,
                             filt=filt2.T,
                             edges=edges,
                             step=(2, 1),
                             stop=(los[ht].shape[1], los[ht].shape[0])).T
            else:
                hi = upConv(image=im,
                            filt=filt2,
                            edges=edges,
                            step=(2, 1),
                            stop=(los[ht].shape[0], im_sz[1]))
                hi2 = upConv(image=hi,
                             filt=filt2.T,
                             edges=edges,
                             step=(1, 2),
                             stop=(los[ht].shape[0], los[ht].shape[1]))

            hi2 = los[ht] - hi2
            self.pyr.insert(pyrCtr, hi2.copy())
            self.pyrSize.insert(pyrCtr, hi2.shape)
            pyrCtr += 1
    def __init__(self, image, height='auto', filt='binom5', edges='reflect1'):
        self.pyrType = 'Gaussian'
        self.image = image

        if isinstance(filt, basestring):
            self.filt = namedFilter(filt)
        else:
            self.filt = filt
        if not (numpy.array(self.filt.shape) == 1).any():
            raise Exception("filt should be a 1D filter (i.e., a vector)")

        # when the first dimension of the image is 1, we need the filter to have shape (1, x)
        # instead of the normal (x, 1) or we get a segfault during corrDn / upConv. That's because
        # we need to match the filter to the image dimensions
        if self.image.shape[0] == 1:
            self.filt = self.filt.reshape(1, max(self.filt.shape))

        maxHeight = 1 + maxPyrHt(self.image.shape, self.filt.shape)

        if height == "auto":
            self.height = maxHeight
        else:
            self.height = height
            if self.height > maxHeight:
                raise Exception("Cannot build pyramid higher than %d levels" % (maxHeight))

        self.edges = edges

        # make pyramid
        self.pyr = []
        self.pyrSize = []
        pyrCtr = 0
        im = numpy.array(self.image).astype(float)

        if len(im.shape) == 1:
            im = im.reshape(im.shape[0], 1)

        self.pyr.append(im.copy())
        self.pyrSize.append(im.shape)
        pyrCtr += 1

        for ht in range(self.height-1,0,-1):
            im_sz = im.shape
            filt_sz = self.filt.shape
            if im_sz[0] == 1:
                lo2 = corrDn(image=im, filt=self.filt, step=(1, 2))
                #lo2 = numpy.array(lo2)
            elif len(im_sz) == 1 or im_sz[1] == 1:
                lo2 = corrDn(image=im, filt=self.filt, step=(2, 1))
                #lo2 = numpy.array(lo2)
            else:
                lo = corrDn(image=im, filt=self.filt.T, step=(1, 2),
                            start=(0, 0))
                #lo = numpy.array(lo)
                lo2 = corrDn(image=lo, filt=self.filt, step=(2, 1),
                             start=(0, 0))
                #lo2 = numpy.array(lo2)                

            self.pyr.append(lo2.copy())
            self.pyrSize.append(lo2.shape)

            pyrCtr += 1

            im = lo2
Esempio n. 3
0
    def __init__(self, *args):    # (image, height, order, twidth)
        self.pyr = []
        self.pyrSize = []
        self.pyrType = 'wavelet'

        if len(args) > 0:
            im = args[0]
        else:
            print "First argument (image) is required."
            return

        #------------------------------------------------
        # defaults:

        if len(args) > 2:
            filt = args[2]
        else:
            filt = "qmf9"
        if isinstance(filt, basestring):
            filt = namedFilter(filt)

        if len(filt.shape) != 1 and filt.shape[0] != 1 and filt.shape[1] != 1:
            print "Error: filter should be 1D (i.e., a vector)";
            return
        hfilt = modulateFlip(filt)

        if len(args) > 3:
            edges = args[3]
        else:
            edges = "reflect1"

        # Stagger sampling if filter is odd-length:
        if filt.shape[0] % 2 == 0:
            stag = 2
        else:
            stag = 1

        # if 1D filter, match to image dimensions
        if len(filt.shape) == 1 or filt.shape[1] == 1:
            if im.shape[0] == 1:
                filt = filt.reshape(1, filt.shape[0])
            elif im.shape[1] == 1:
                filt = filt.reshape(filt.shape[0], 1)

        max_ht = maxPyrHt(im.shape, filt.shape)

        if len(args) > 1:
            ht = args[1]
            if ht == 'auto':
                ht = max_ht
            elif(ht > max_ht):
                print "Error: cannot build pyramid higher than %d levels." % (max_ht)
        else:
            ht = max_ht
        ht = int(ht)
        self.height = ht + 1  # used with showPyr() method
        for lev in range(ht):
            if len(im.shape) == 1 or im.shape[1] == 1:
                lolo = corrDn(image = im, filt = filt, edges = edges,
                              step = (2,1), start = (stag-1,0))
                hihi = corrDn(image = im, filt = hfilt, edges = edges,
                              step = (2,1), start = (1, 0))
            elif im.shape[0] == 1:
                lolo = corrDn(image = im, filt = filt, edges = edges,
                              step = (1,2), start = (0, stag-1))
                hihi = corrDn(image = im, filt = hfilt.T, edges = edges,
                              step = (1,2), start = (0,1))
            else:
                lo = corrDn(image = im, filt = filt, edges = edges,
                            step = (2,1), start = (stag-1,0))
                hi = corrDn(image = im, filt = hfilt, edges = edges,
                            step = (2,1), start = (1,0))
                lolo = corrDn(image = lo, filt = filt.T, edges = edges,
                              step = (1,2), start = (0, stag-1))
                lohi = corrDn(image = hi, filt = filt.T, edges = edges,
                              step = (1,2), start = (0,stag-1))
                hilo = corrDn(image = lo, filt = hfilt.T, edges = edges,
                              step = (1,2), start = (0,1))
                hihi = corrDn(image = hi, filt = hfilt.T, edges = edges,
                              step = (1,2), start = (0,1))

            if im.shape[0] == 1 or im.shape[1] == 1:
                self.pyr.append(hihi)
                self.pyrSize.append(hihi.shape)
            else:
                self.pyr.append(lohi)
                self.pyrSize.append(lohi.shape)
                self.pyr.append(hilo)
                self.pyrSize.append(hilo.shape)
                self.pyr.append(hihi)
                self.pyrSize.append(hihi.shape)
            im = lolo.copy()
        self.pyr.append(lolo)
        self.pyrSize.append(lolo.shape)
Esempio n. 4
0
    def __init__(self, *args):    # (image height, filter file, edges)
        self.pyrType = 'steerable'
        if len(args) > 0:
            self.image = numpy.array(args[0])
        else:
            print "First argument (image) is required."
            return

        #------------------------------------------------
        # defaults:

        if len(args) > 2:
            if args[2] == 'sp0Filters':
                filters = sp0Filters()
            elif args[2] == 'sp1Filters':
                filters = sp1Filters()
            elif args[2] == 'sp3Filters':
                filters = sp3Filters()
            elif args[2] == 'sp5Filters':
                filters = sp5Filters()
            elif os.path.isfile(args[2]):
                print "Filter files not supported yet"
                return
            else:
                print "filter parameters value %s not supported" % (args[2])
                return
        else:
            filters = sp1Filters()

        harmonics = filters['harmonics']
        lo0filt = filters['lo0filt']
        hi0filt = filters['hi0filt']
        lofilt = filters['lofilt']
        bfilts = filters['bfilts']
        steermtx = filters['mtx']
        
        max_ht = maxPyrHt(self.image.shape, lofilt.shape)
        if len(args) > 1:
            if args[1] == 'auto':
                ht = max_ht
            elif args[1] > max_ht:
                print "Error: cannot build pyramid higher than %d levels." % (
                    max_ht)
                return
            else:
                ht = args[1]
        else:
            ht = max_ht

        if len(args) > 3:
            edges = args[3]
        else:
            edges = 'reflect1'

        #------------------------------------------------------

        nbands = bfilts.shape[1]

        self.pyr = []
        self.pyrSize = []
        for n in range((ht*nbands)+2):
            self.pyr.append([])
            self.pyrSize.append([])

        im = self.image
        im_sz = im.shape
        pyrCtr = 0

        hi0 = corrDn(image = im, filt = hi0filt, edges = edges);

        self.pyr[pyrCtr] = hi0
        self.pyrSize[pyrCtr] = hi0.shape

        pyrCtr += 1

        lo = corrDn(image = im, filt = lo0filt, edges = edges)
        for i in range(ht):
            lo_sz = lo.shape
            # assume square filters  -- start of buildSpyrLevs
            bfiltsz = int(math.floor(math.sqrt(bfilts.shape[0])))

            for b in range(bfilts.shape[1]):
                filt = bfilts[:,b].reshape(bfiltsz,bfiltsz).T
                band = corrDn(image = lo, filt = filt, edges = edges)
                self.pyr[pyrCtr] = numpy.array(band)
                self.pyrSize[pyrCtr] = (band.shape[0], band.shape[1])
                pyrCtr += 1

            lo = corrDn(image = lo, filt = lofilt, edges = edges, step = (2,2))

        self.pyr[pyrCtr] = numpy.array(lo)
        self.pyrSize[pyrCtr] = lo.shape
Esempio n. 5
0
    def __init__(self,
                 image,
                 height='auto',
                 filter='sp1Filters',
                 edges='reflect1'):
        """Steerable pyramid. image parameter is required, others are optional
        
        - `image` - a 2D numpy array
  
        - `height` - an integer denoting number of pyramid levels desired.  'auto' (default) uses
        maxPyrHt from pyPyrUtils.

        - `filter` - The name of one of the steerable pyramid filters in pyPyrUtils:
        `'sp0Filters'`, `'sp1Filters'`, `'sp3Filters'`, `'sp5Filters'`.  Default is `'sp1Filters'`.
    
        - `edges` - specifies edge-handling.  Options are:
           * `'circular'` - circular convolution
           * `'reflect1'` - reflect about the edge pixels
           * `'reflect2'` - reflect, doubling the edge pixels
           * `'repeat'` - repeat the edge pixels
           * `'zero'` - assume values of zero outside image boundary
           * `'extend'` - reflect and invert
           * `'dont-compute'` - zero output when filter overhangs input boundaries.
        """
        self.pyrType = 'steerable'
        self.image = numpy.array(image)

        if filter == 'sp0Filters':
            filters = sp0Filters()
        elif filter == 'sp1Filters':
            filters = sp1Filters()
        elif filter == 'sp3Filters':
            filters = sp3Filters()
        elif filter == 'sp5Filters':
            filters = sp5Filters()
        elif os.path.isfile(filter):
            raise Exception("Filter files not supported yet")
        else:
            raise Exception("filter parameters value %s not supported" %
                            (filter))
        self.filt = filters

        self.edges = edges

        harmonics = filters['harmonics']
        lo0filt = filters['lo0filt']
        hi0filt = filters['hi0filt']
        lofilt = filters['lofilt']
        bfilts = filters['bfilts']
        steermtx = filters['mtx']

        max_ht = maxPyrHt(self.image.shape, lofilt.shape)
        if height == 'auto':
            ht = max_ht
        elif height > max_ht:
            raise Exception("cannot build pyramid higher than %d levels." %
                            (max_ht))
        else:
            ht = height

        nbands = bfilts.shape[1]

        self.pyr = []
        self.pyrSize = []
        for n in range((ht * nbands) + 2):
            self.pyr.append([])
            self.pyrSize.append([])

        im = self.image
        im_sz = im.shape
        pyrCtr = 0

        hi0 = corrDn(image=im, filt=hi0filt, edges=edges)

        self.pyr[pyrCtr] = hi0
        self.pyrSize[pyrCtr] = hi0.shape

        pyrCtr += 1

        lo = corrDn(image=im, filt=lo0filt, edges=edges)
        for i in range(ht):
            lo_sz = lo.shape
            # assume square filters  -- start of buildSpyrLevs
            bfiltsz = int(math.floor(math.sqrt(bfilts.shape[0])))

            for b in range(bfilts.shape[1]):
                filt = bfilts[:, b].reshape(bfiltsz, bfiltsz).T
                band = corrDn(image=lo, filt=filt, edges=edges)
                self.pyr[pyrCtr] = numpy.array(band)
                self.pyrSize[pyrCtr] = (band.shape[0], band.shape[1])
                pyrCtr += 1

            lo = corrDn(image=lo, filt=lofilt, edges=edges, step=(2, 2))

        self.pyr[pyrCtr] = numpy.array(lo)
        self.pyrSize[pyrCtr] = lo.shape
Esempio n. 6
0
    def __init__(self, *args):    # (image, height, filter, edges)
        self.pyrType = 'Gaussian'
        if len(args) < 1:
            print ("pyr = Gpyr(image, height, filter, edges)")
            print ("First argument (image) is required")
            return
        else:
            self.image = args[0]

        if len(args) > 2:
            filt = args[2]
            if not (filt.shape == 1).any():
                print ("Error: filt should be a 1D filter (i.e., a vector)")
                return
        else:
            print ("no filter set, so filter is binom5")
            filt = namedFilter('binom5')
            if self.image.shape[0] == 1:
                filt = filt.reshape(1,5)
            else:
                filt = filt.reshape(5,1)

        maxHeight = 1 + maxPyrHt(self.image.shape, filt.shape)

        if len(args) > 1:
            if args[1] is "auto":
                self.height = maxHeight
            else:
                self.height = args[1]
                if self.height > maxHeight:
                    print ( "Error: cannot build pyramid higher than %d levels"
                            % (maxHeight) )
                    return
        else:
            self.height = maxHeight

        if len(args) > 3:
            edges = args[3]
        else:
            edges = "reflect1"

        # make pyramid
        self.pyr = []
        self.pyrSize = []
        pyrCtr = 0
        im = numpy.array(self.image).astype(float)

        if len(im.shape) == 1:
            im = im.reshape(im.shape[0], 1)

        self.pyr.append(im.copy())
        self.pyrSize.append(im.shape)
        pyrCtr += 1

        for ht in range(self.height-1,0,-1):
            im_sz = im.shape
            filt_sz = filt.shape
            if im_sz[0] == 1:
                lo2 = corrDn(image = im, filt = filt, step = (1,2))
                #lo2 = numpy.array(lo2)
            elif len(im_sz) == 1 or im_sz[1] == 1:
                lo2 = corrDn(image = im, filt = filt, step = (2,1))
                #lo2 = numpy.array(lo2)
            else:
                lo = corrDn(image = im, filt = filt.T, step = (1,2),
                            start = (0,0))
                #lo = numpy.array(lo)
                lo2 = corrDn(image = lo, filt = filt, step = (2,1),
                             start = (0,0))
                #lo2 = numpy.array(lo2)                

            self.pyr.append(lo2.copy())
            self.pyrSize.append(lo2.shape)

            pyrCtr += 1

            im = lo2
Esempio n. 7
0
    def __init__(self, *args):  # (image height, filter file, edges)
        self.pyrType = 'steerable'
        if len(args) > 0:
            self.image = numpy.array(args[0])
        else:
            print "First argument (image) is required."
            return

        #------------------------------------------------
        # defaults:

        if len(args) > 2:
            if args[2] == 'sp0Filters':
                filters = sp0Filters()
            elif args[2] == 'sp1Filters':
                filters = sp1Filters()
            elif args[2] == 'sp3Filters':
                filters = sp3Filters()
            elif args[2] == 'sp5Filters':
                filters = sp5Filters()
            elif os.path.isfile(args[2]):
                print "Filter files not supported yet"
                return
            else:
                print "filter parameters value %s not supported" % (args[2])
                return
        else:
            filters = sp1Filters()

        harmonics = filters['harmonics']
        lo0filt = filters['lo0filt']
        hi0filt = filters['hi0filt']
        lofilt = filters['lofilt']
        bfilts = filters['bfilts']
        steermtx = filters['mtx']

        max_ht = maxPyrHt(self.image.shape, lofilt.shape)
        if len(args) > 1:
            if args[1] == 'auto':
                ht = max_ht
            elif args[1] > max_ht:
                print "Error: cannot build pyramid higher than %d levels." % (
                    max_ht)
                return
            else:
                ht = args[1]
        else:
            ht = max_ht

        if len(args) > 3:
            edges = args[3]
        else:
            edges = 'reflect1'

        #------------------------------------------------------

        nbands = bfilts.shape[1]

        self.pyr = []
        self.pyrSize = []
        for n in range((ht * nbands) + 2):
            self.pyr.append([])
            self.pyrSize.append([])

        im = self.image
        im_sz = im.shape
        pyrCtr = 0

        hi0 = corrDn(image=im, filt=hi0filt, edges=edges)

        self.pyr[pyrCtr] = hi0
        self.pyrSize[pyrCtr] = hi0.shape

        pyrCtr += 1

        lo = corrDn(image=im, filt=lo0filt, edges=edges)
        for i in range(ht):
            lo_sz = lo.shape
            # assume square filters  -- start of buildSpyrLevs
            bfiltsz = int(math.floor(math.sqrt(bfilts.shape[0])))

            for b in range(bfilts.shape[1]):
                filt = bfilts[:, b].reshape(bfiltsz, bfiltsz).T
                band = corrDn(image=lo, filt=filt, edges=edges)
                self.pyr[pyrCtr] = numpy.array(band)
                self.pyrSize[pyrCtr] = (band.shape[0], band.shape[1])
                pyrCtr += 1

            lo = corrDn(image=lo, filt=lofilt, edges=edges, step=(2, 2))

        self.pyr[pyrCtr] = numpy.array(lo)
        self.pyrSize[pyrCtr] = lo.shape
Esempio n. 8
0
    def __init__(self, *args):    # (image, height, filter1, filter2, edges)
        self.pyrType = 'Laplacian'
        if len(args) > 0:
            self.image = args[0]
        else:
            print "pyr = Lpyr(image, height, filter1, filter2, edges)"
            print "First argument (image) is required"
            return

        if len(args) > 2:
            filt1 = args[2]
            if isinstance(filt1, basestring):
                filt1 = namedFilter(filt1)
            elif len(filt1.shape) != 1 and ( filt1.shape[0] != 1 and
                                             filt1.shape[1] != 1 ):
                print "Error: filter1 should be a 1D filter (i.e., a vector)"
                return
        else:
            filt1 = namedFilter('binom5')
        if len(filt1.shape) == 1:
            filt1 = filt1.reshape(1,len(filt1))
        elif self.image.shape[0] == 1:
            filt1 = filt1.reshape(filt1.shape[1], filt1.shape[0])

        if len(args) > 3:
            filt2 = args[3]
            if isinstance(filt2, basestring):
                filt2 = namedFilter(filt2)
            elif len(filt2.shape) != 1 and ( filt2.shape[0] != 1 and
                                            filt2.shape[1] != 1 ):
                print "Error: filter2 should be a 1D filter (i.e., a vector)"
                return
        else:
            filt2 = filt1

        maxHeight = 1 + maxPyrHt(self.image.shape, filt1.shape)

        if len(args) > 1:
            if args[1] is "auto":
                self.height = maxHeight
            else:
                self.height = args[1]
                if self.height > maxHeight:
                    print ( "Error: cannot build pyramid higher than %d levels"
                            % (maxHeight) )
                    return
        else:
            self.height = maxHeight

        if len(args) > 4:
            edges = args[4]
        else:
            edges = "reflect1"

        # make pyramid
        self.pyr = []
        self.pyrSize = []
        pyrCtr = 0
        im = numpy.array(self.image).astype(float)
        if len(im.shape) == 1:
            im = im.reshape(im.shape[0], 1)
        los = {}
        los[self.height] = im
        # compute low bands
        for ht in range(self.height-1,0,-1):
            im_sz = im.shape
            filt1_sz = filt1.shape
            if im_sz[0] == 1:
                lo2 = corrDn(image = im, filt = filt1, edges = edges,
                             step = (1,2))
                #lo2 = numpy.array(lo2)
            elif len(im_sz) == 1 or im_sz[1] == 1:
                lo2  = corrDn(image = im, filt = filt1, edges = edges,
                              step = (2,1))
                #lo2 = numpy.array(lo2)
            else:
                lo = corrDn(image = im, filt = filt1.T, edges = edges,
                            step = (1,2), start = (0,0))
                #lo = numpy.array(lo)
                lo2 = corrDn(image = lo, filt = filt1, edges = edges,
                             step = (2,1), start = (0,0))
                #lo2 = numpy.array(lo2)

            los[ht] = lo2
                
            im = lo2

        # adjust shape if 1D if needed
        self.pyr.append(lo2.copy())
        self.pyrSize.append(lo2.shape)

        # compute hi bands
        im = self.image
        for ht in range(self.height, 1, -1):
            im = los[ht-1]
            im_sz = los[ht-1].shape
            filt2_sz = filt2.shape
            if len(im_sz) == 1 or im_sz[1] == 1:
                hi2 = upConv(image = im, filt = filt2.T, edges = edges,
                             step = (1,2), stop = (los[ht].shape[1],
                                                   los[ht].shape[0])).T
            elif im_sz[0] == 1:
                hi2 = upConv(image = im, filt = filt2.T, edges = edges,
                             step = (2,1), stop = (los[ht].shape[1],
                                                   los[ht].shape[0])).T
            else:
                hi = upConv(image = im, filt = filt2, edges = edges,
                            step = (2,1), stop = (los[ht].shape[0], im_sz[1]))
                hi2 = upConv(image = hi, filt = filt2.T, edges = edges,
                             step = (1,2), stop = (los[ht].shape[0], 
                                                   los[ht].shape[1]))
                                       
            hi2 = los[ht] - hi2
            self.pyr.insert(pyrCtr, hi2.copy())
            self.pyrSize.insert(pyrCtr, hi2.shape)
            pyrCtr += 1
Esempio n. 9
0
    def __init__(self, image, height='auto', filter1='binom5', filter2=None, edges='reflect1'):
        self.pyrType = 'Laplacian'
        self.image = image

        if isinstance(filter1, basestring):
            filter1 = namedFilter(filter1)
        elif filter1.ndim != 1 and (filter1.shape[0] != 1 and filter1.shape[1] != 1):
            raise Exception("Error: filter1 should be a 1D filter (i.e., a vector)")

        if filter1.ndim == 1:
            filter1 = filter1.reshape(1, len(filter1))
        # when the first dimension of the image is 1, we need the filter to have shape (1, x)
        # instead of the normal (x, 1) or we get a segfault during corrDn / upConv. That's because
        # we need to match the filter to the image dimensions
        elif self.image.shape[0] == 1:
            filter1 = filter1.reshape(1, max(filter1.shape))

        if isinstance(filter2, basestring):
            filter2 = namedFilter(filter2)
        elif filter2 is None:
            filter2 = filter1
        elif filter2.ndim != 1 and (filter2.shape[0] != 1 and filter2.shape[1] != 1):
            raise Exception("Error: filter2 should be a 1D filter (i.e., a vector)")

        if filter2.ndim == 1:
            filter2 = filter2.reshape(1, len(filter2))
        elif self.image.shape[0] == 1:
            filter2 = filter2.reshape(1, max(filter2.shape))

        self.filter1 = filter1
        self.filter2 = filter2
        
        maxHeight = 1 + maxPyrHt(self.image.shape, filter1.shape)

        if isinstance(height, basestring) and height == "auto":
            self.height = maxHeight
        else:
            self.height = height
            if self.height > maxHeight:
                raise Exception("Error: cannot build pyramid higher than %d levels" % (maxHeight))

        # make pyramid
        self.pyr = []
        self.pyrSize = []
        pyrCtr = 0
        im = numpy.array(self.image).astype(float)
        if len(im.shape) == 1:
            im = im.reshape(im.shape[0], 1)
        los = {}
        los[self.height] = im
        # compute low bands
        for ht in range(self.height-1,0,-1):
            im_sz = im.shape
            filter1_sz = filter1.shape
            if im_sz[0] == 1:
                lo2 = corrDn(image = im, filt = filter1, edges = edges,
                             step = (1,2))
                #lo2 = numpy.array(lo2)
            elif len(im_sz) == 1 or im_sz[1] == 1:
                lo2  = corrDn(image = im, filt = filter1, edges = edges,
                              step = (2,1))
                #lo2 = numpy.array(lo2)
            else:
                lo = corrDn(image = im, filt = filter1.T, edges = edges,
                            step = (1,2), start = (0,0))
                #lo = numpy.array(lo)
                lo2 = corrDn(image = lo, filt = filter1, edges = edges,
                             step = (2,1), start = (0,0))
                #lo2 = numpy.array(lo2)

            los[ht] = lo2
                
            im = lo2

        # adjust shape if 1D if needed
        self.pyr.append(lo2.copy())
        self.pyrSize.append(lo2.shape)

        # compute hi bands
        im = self.image
        for ht in range(self.height, 1, -1):
            im = los[ht-1]
            im_sz = los[ht-1].shape
            filter2_sz = filter2.shape
            if len(im_sz) == 1 or im_sz[1] == 1:
                hi2 = upConv(image = im, filt = filter2.T, edges = edges,
                             step = (1,2), stop = (los[ht].shape[1],
                                                   los[ht].shape[0])).T
            elif im_sz[0] == 1:
                hi2 = upConv(image = im, filt = filter2.T, edges = edges,
                             step = (2,1), stop = (los[ht].shape[1],
                                                   los[ht].shape[0])).T
            else:
                hi = upConv(image = im, filt = filter2, edges = edges,
                            step = (2,1), stop = (los[ht].shape[0], im_sz[1]))
                hi2 = upConv(image = hi, filt = filter2.T, edges = edges,
                             step = (1,2), stop = (los[ht].shape[0], 
                                                   los[ht].shape[1]))
                                       
            hi2 = los[ht] - hi2
            self.pyr.insert(pyrCtr, hi2.copy())
            self.pyrSize.insert(pyrCtr, hi2.shape)
            pyrCtr += 1
Esempio n. 10
0
    def __init__(self, image, height='auto', filter='sp1Filters', edges='reflect1'):
        """Steerable pyramid. image parameter is required, others are optional
        
        - `image` - a 2D numpy array
  
        - `height` - an integer denoting number of pyramid levels desired.  'auto' (default) uses
        maxPyrHt from pyPyrUtils.

        - `filter` - The name of one of the steerable pyramid filters in pyPyrUtils:
        `'sp0Filters'`, `'sp1Filters'`, `'sp3Filters'`, `'sp5Filters'`.  Default is `'sp1Filters'`.
    
        - `edges` - specifies edge-handling.  Options are:
           * `'circular'` - circular convolution
           * `'reflect1'` - reflect about the edge pixels
           * `'reflect2'` - reflect, doubling the edge pixels
           * `'repeat'` - repeat the edge pixels
           * `'zero'` - assume values of zero outside image boundary
           * `'extend'` - reflect and invert
           * `'dont-compute'` - zero output when filter overhangs input boundaries.
        """
        self.pyrType = 'steerable'
        self.image = numpy.array(image)

        if filter == 'sp0Filters':
            filters = sp0Filters()
        elif filter == 'sp1Filters':
            filters = sp1Filters()
        elif filter == 'sp3Filters':
            filters = sp3Filters()
        elif filter == 'sp5Filters':
            filters = sp5Filters()
        elif os.path.isfile(filter):
            raise Exception("Filter files not supported yet")
        else:
            raise Exception("filter parameters value %s not supported" % (filter))
        self.filt = filters

        self.edges = edges

        harmonics = filters['harmonics']
        lo0filt = filters['lo0filt']
        hi0filt = filters['hi0filt']
        lofilt = filters['lofilt']
        bfilts = filters['bfilts']
        steermtx = filters['mtx']
        
        max_ht = maxPyrHt(self.image.shape, lofilt.shape)
        if height == 'auto':
            ht = max_ht
        elif height > max_ht:
            raise Exception("cannot build pyramid higher than %d levels." % (max_ht))
        else:
            ht = height

        nbands = bfilts.shape[1]

        self.pyr = []
        self.pyrSize = []
        for n in range((ht*nbands)+2):
            self.pyr.append([])
            self.pyrSize.append([])

        im = self.image
        im_sz = im.shape
        pyrCtr = 0

        hi0 = corrDn(image = im, filt = hi0filt, edges = edges);

        self.pyr[pyrCtr] = hi0
        self.pyrSize[pyrCtr] = hi0.shape

        pyrCtr += 1

        lo = corrDn(image = im, filt = lo0filt, edges = edges)
        for i in range(ht):
            lo_sz = lo.shape
            # assume square filters  -- start of buildSpyrLevs
            bfiltsz = int(math.floor(math.sqrt(bfilts.shape[0])))

            for b in range(bfilts.shape[1]):
                filt = bfilts[:,b].reshape(bfiltsz,bfiltsz).T
                band = corrDn(image = lo, filt = filt, edges = edges)
                self.pyr[pyrCtr] = numpy.array(band)
                self.pyrSize[pyrCtr] = (band.shape[0], band.shape[1])
                pyrCtr += 1

            lo = corrDn(image = lo, filt = lofilt, edges = edges, step = (2,2))

        self.pyr[pyrCtr] = numpy.array(lo)
        self.pyrSize[pyrCtr] = lo.shape
Esempio n. 11
0
    def __init__(self, *args):  # (image, height, order, twidth)
        self.pyr = []
        self.pyrSize = []
        self.pyrType = 'wavelet'

        if len(args) > 0:
            im = args[0]
        else:
            print "First argument (image) is required."
            return

        #------------------------------------------------
        # defaults:

        if len(args) > 2:
            filt = args[2]
        else:
            filt = "qmf9"
        if isinstance(filt, basestring):
            filt = namedFilter(filt)

        if len(filt.shape) != 1 and filt.shape[0] != 1 and filt.shape[1] != 1:
            print "Error: filter should be 1D (i.e., a vector)"
            return
        hfilt = modulateFlip(filt)

        if len(args) > 3:
            edges = args[3]
        else:
            edges = "reflect1"

        # Stagger sampling if filter is odd-length:
        if filt.shape[0] % 2 == 0:
            stag = 2
        else:
            stag = 1

        # if 1D filter, match to image dimensions
        if len(filt.shape) == 1 or filt.shape[1] == 1:
            if im.shape[0] == 1:
                filt = filt.reshape(1, filt.shape[0])
            elif im.shape[1] == 1:
                filt = filt.reshape(filt.shape[0], 1)

        max_ht = maxPyrHt(im.shape, filt.shape)

        if len(args) > 1:
            ht = args[1]
            if ht == 'auto':
                ht = max_ht
            elif (ht > max_ht):
                print "Error: cannot build pyramid higher than %d levels." % (
                    max_ht)
        else:
            ht = max_ht
        ht = int(ht)
        self.height = ht + 1  # used with showPyr() method
        for lev in range(ht):
            if len(im.shape) == 1 or im.shape[1] == 1:
                lolo = corrDn(image=im,
                              filt=filt,
                              edges=edges,
                              step=(2, 1),
                              start=(stag - 1, 0))
                hihi = corrDn(image=im,
                              filt=hfilt,
                              edges=edges,
                              step=(2, 1),
                              start=(1, 0))
            elif im.shape[0] == 1:
                lolo = corrDn(image=im,
                              filt=filt,
                              edges=edges,
                              step=(1, 2),
                              start=(0, stag - 1))
                hihi = corrDn(image=im,
                              filt=hfilt.T,
                              edges=edges,
                              step=(1, 2),
                              start=(0, 1))
            else:
                lo = corrDn(image=im,
                            filt=filt,
                            edges=edges,
                            step=(2, 1),
                            start=(stag - 1, 0))
                hi = corrDn(image=im,
                            filt=hfilt,
                            edges=edges,
                            step=(2, 1),
                            start=(1, 0))
                lolo = corrDn(image=lo,
                              filt=filt.T,
                              edges=edges,
                              step=(1, 2),
                              start=(0, stag - 1))
                lohi = corrDn(image=hi,
                              filt=filt.T,
                              edges=edges,
                              step=(1, 2),
                              start=(0, stag - 1))
                hilo = corrDn(image=lo,
                              filt=hfilt.T,
                              edges=edges,
                              step=(1, 2),
                              start=(0, 1))
                hihi = corrDn(image=hi,
                              filt=hfilt.T,
                              edges=edges,
                              step=(1, 2),
                              start=(0, 1))

            if im.shape[0] == 1 or im.shape[1] == 1:
                self.pyr.append(hihi)
                self.pyrSize.append(hihi.shape)
            else:
                self.pyr.append(lohi)
                self.pyrSize.append(lohi.shape)
                self.pyr.append(hilo)
                self.pyrSize.append(hilo.shape)
                self.pyr.append(hihi)
                self.pyrSize.append(hihi.shape)
            im = lolo.copy()
        self.pyr.append(lolo)
        self.pyrSize.append(lolo.shape)
Esempio n. 12
0
    def __init__(self, image, height='auto', filt='binom5', edges='reflect1'):
        self.pyrType = 'Gaussian'
        self.image = image

        if isinstance(filt, basestring):
            self.filt = namedFilter(filt)
        else:
            self.filt = filt
        if not (numpy.array(self.filt.shape) == 1).any():
            raise Exception("filt should be a 1D filter (i.e., a vector)")

        # when the first dimension of the image is 1, we need the filter to have shape (1, x)
        # instead of the normal (x, 1) or we get a segfault during corrDn / upConv. That's because
        # we need to match the filter to the image dimensions
        if self.image.shape[0] == 1:
            self.filt = self.filt.reshape(1, max(self.filt.shape))

        maxHeight = 1 + maxPyrHt(self.image.shape, self.filt.shape)

        if height == "auto":
            self.height = maxHeight
        else:
            self.height = height
            if self.height > maxHeight:
                raise Exception("Cannot build pyramid higher than %d levels" %
                                (maxHeight))

        self.edges = edges

        # make pyramid
        self.pyr = []
        self.pyrSize = []
        pyrCtr = 0
        im = numpy.array(self.image).astype(float)

        if len(im.shape) == 1:
            im = im.reshape(im.shape[0], 1)

        self.pyr.append(im.copy())
        self.pyrSize.append(im.shape)
        pyrCtr += 1

        for ht in range(self.height - 1, 0, -1):
            im_sz = im.shape
            filt_sz = self.filt.shape
            if im_sz[0] == 1:
                lo2 = corrDn(image=im, filt=self.filt, step=(1, 2))
                #lo2 = numpy.array(lo2)
            elif len(im_sz) == 1 or im_sz[1] == 1:
                lo2 = corrDn(image=im, filt=self.filt, step=(2, 1))
                #lo2 = numpy.array(lo2)
            else:
                lo = corrDn(image=im,
                            filt=self.filt.T,
                            step=(1, 2),
                            start=(0, 0))
                #lo = numpy.array(lo)
                lo2 = corrDn(image=lo,
                             filt=self.filt,
                             step=(2, 1),
                             start=(0, 0))
                #lo2 = numpy.array(lo2)

            self.pyr.append(lo2.copy())
            self.pyrSize.append(lo2.shape)

            pyrCtr += 1

            im = lo2
Esempio n. 13
0
    def __init__(self, *args):  # (image, height, filter, edges)
        self.pyrType = 'Gaussian'
        if len(args) < 1:
            print "pyr = Gpyr(image, height, filter, edges)"
            print "First argument (image) is required"
            return
        else:
            self.image = args[0]

        if len(args) > 2:
            filt = args[2]
            if not (filt.shape == 1).any():
                print "Error: filt should be a 1D filter (i.e., a vector)"
                return
        else:
            print "no filter set, so filter is binom5"
            filt = namedFilter('binom5')
            if self.image.shape[0] == 1:
                filt = filt.reshape(1, 5)
            else:
                filt = filt.reshape(5, 1)

        maxHeight = 1 + maxPyrHt(self.image.shape, filt.shape)

        if len(args) > 1:
            if args[1] is "auto":
                self.height = maxHeight
            else:
                self.height = args[1]
                if self.height > maxHeight:
                    print("Error: cannot build pyramid higher than %d levels" %
                          (maxHeight))
                    return
        else:
            self.height = maxHeight

        if len(args) > 3:
            edges = args[3]
        else:
            edges = "reflect1"

        # make pyramid
        self.pyr = []
        self.pyrSize = []
        pyrCtr = 0
        im = numpy.array(self.image).astype(float)

        if len(im.shape) == 1:
            im = im.reshape(im.shape[0], 1)

        self.pyr.append(im.copy())
        self.pyrSize.append(im.shape)
        pyrCtr += 1

        for ht in range(self.height - 1, 0, -1):
            im_sz = im.shape
            filt_sz = filt.shape
            if im_sz[0] == 1:
                lo2 = corrDn(image=im, filt=filt, step=(1, 2))
                #lo2 = numpy.array(lo2)
            elif len(im_sz) == 1 or im_sz[1] == 1:
                lo2 = corrDn(image=im, filt=filt, step=(2, 1))
                #lo2 = numpy.array(lo2)
            else:
                lo = corrDn(image=im, filt=filt.T, step=(1, 2), start=(0, 0))
                #lo = numpy.array(lo)
                lo2 = corrDn(image=lo, filt=filt, step=(2, 1), start=(0, 0))
                #lo2 = numpy.array(lo2)

            self.pyr.append(lo2.copy())
            self.pyrSize.append(lo2.shape)

            pyrCtr += 1

            im = lo2