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
def reconPyr(self, *args): # defaults if len(args) > 0: if args[0] == 'sp0Filters': filters = sp0Filters() elif args[0] == 'sp1Filters': filters = sp1Filters() elif args[0] == 'sp3Filters': filters = sp3Filters() elif args[0] == 'sp5Filters': filters = sp5Filters() elif os.path.isfile(args[0]): print "Filter files not supported yet" return else: print "filter %s not supported" % (args[0]) return else: filters = sp1Filters() lo0filt = filters['lo0filt'] hi0filt = filters['hi0filt'] lofilt = filters['lofilt'] bfilts = filters['bfilts'] steermtx = filters['mtx'] # assume square filters -- start of buildSpyrLevs bfiltsz = int(math.floor(math.sqrt(bfilts.shape[0]))) if len(args) > 1: edges = args[1] else: edges = 'reflect1' if len(args) > 2: levs = args[2] else: levs = 'all' if len(args) > 3: bands = args[3] else: bands = 'all' #--------------------------------------------------------- maxLev = 2 + self.spyrHt() if levs == 'all': levs = numpy.array(range(maxLev)) else: levs = numpy.array(levs) if (levs < 0).any() or (levs >= maxLev).any(): print "Error: level numbers must be in the range [0, %d]." % (maxLev-1) return else: levs = numpy.array(levs) if len(levs) > 1 and levs[0] < levs[1]: levs = levs[::-1] # we want smallest first if bands == 'all': bands = numpy.array(range(self.numBands())) else: bands = numpy.array(bands) if (bands < 0).any() or (bands > bfilts.shape[1]).any(): print "Error: band numbers must be in the range [0, %d]." % (self.numBands()-1) return else: bands = numpy.array(bands) # make a list of all pyramid layers to be used in reconstruction Nlevs = self.spyrHt() Nbands = self.numBands() reconList = [] # pyr indices used in reconstruction for lev in levs: if lev == 0: reconList.append(0) elif lev == Nlevs+1: # number of levels times number of bands + top and bottom # minus 1 for 0 starting index reconList.append( (Nlevs*Nbands) + 2 - 1) else: for band in bands: reconList.append( ((lev-1) * Nbands) + band + 1) reconList = numpy.sort(reconList)[::-1] # deepest level first # initialize reconstruction if len(self.pyr)-1 in reconList: recon = numpy.array(self.pyr[len(self.pyrSize)-1]) else: recon = numpy.zeros(self.pyr[len(self.pyrSize)-1].shape) # recursive subsystem # we need to loop over recursive subsystem pairs for level in range(Nlevs): maxLevIdx = ((maxLev-2) * Nbands) + 1 resSzIdx = maxLevIdx - (level * Nbands) - 1 recon = upConv(image = recon, filt = lofilt, edges = edges, step = (2,2), start = (0,0), stop = self.pyrSize[resSzIdx]) bandImageIdx = 1 + (((Nlevs-1)-level) * Nbands) for band in range(Nbands-1,-1,-1): if bandImageIdx in reconList: filt = bfilts[:,(Nbands-1)-band].reshape(bfiltsz, bfiltsz, order='F') recon = upConv(image = self.pyr[bandImageIdx], filt = filt, edges = edges, stop = (self.pyrSize[bandImageIdx][0], self.pyrSize[bandImageIdx][1]), result = recon) bandImageIdx += 1 # apply lo0filt sz = recon.shape recon = upConv(image = recon, filt = lo0filt, edges = edges, stop = sz) # apply hi0filt if needed if 0 in reconList: recon = upConv(image = self.pyr[0], filt = hi0filt, edges = edges, start = (0,0), step = (1,1), stop = recon.shape, result = recon) return recon
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
def reconPyr(self, *args): # defaults if len(args) > 0: if args[0] == 'sp0Filters': filters = sp0Filters() elif args[0] == 'sp1Filters': filters = sp1Filters() elif args[0] == 'sp3Filters': filters = sp3Filters() elif args[0] == 'sp5Filters': filters = sp5Filters() elif os.path.isfile(args[0]): print "Filter files not supported yet" return else: print "filter %s not supported" % (args[0]) return else: filters = sp1Filters() lo0filt = filters['lo0filt'] hi0filt = filters['hi0filt'] lofilt = filters['lofilt'] bfilts = filters['bfilts'] steermtx = filters['mtx'] # assume square filters -- start of buildSpyrLevs bfiltsz = int(math.floor(math.sqrt(bfilts.shape[0]))) if len(args) > 1: edges = args[1] else: edges = 'reflect1' if len(args) > 2: levs = args[2] else: levs = 'all' if len(args) > 3: bands = args[3] else: bands = 'all' #--------------------------------------------------------- maxLev = 2 + self.spyrHt() if levs == 'all': levs = numpy.array(range(maxLev)) else: levs = numpy.array(levs) if (levs < 0).any() or (levs >= maxLev).any(): raise Exception("level numbers must be in the range [0, %d]." % (maxLev - 1)) else: levs = numpy.array(levs) if len(levs) > 1 and levs[0] < levs[1]: levs = levs[::-1] # we want smallest first if bands == 'all': bands = numpy.array(range(self.numBands())) else: bands = numpy.array(bands) if (bands < 0).any() or (bands > bfilts.shape[1]).any(): raise Exception("band numbers must be in the range [0, %d]." % (self.numBands() - 1)) else: bands = numpy.array(bands) # make a list of all pyramid layers to be used in reconstruction Nlevs = self.spyrHt() Nbands = self.numBands() reconList = [] # pyr indices used in reconstruction for lev in levs: if lev == 0: reconList.append(0) elif lev == Nlevs + 1: # number of levels times number of bands + top and bottom # minus 1 for 0 starting index reconList.append((Nlevs * Nbands) + 2 - 1) else: for band in bands: reconList.append(((lev - 1) * Nbands) + band + 1) reconList = numpy.sort(reconList)[::-1] # deepest level first # initialize reconstruction if len(self.pyr) - 1 in reconList: recon = numpy.array(self.pyr[len(self.pyrSize) - 1]) else: recon = numpy.zeros(self.pyr[len(self.pyrSize) - 1].shape) # recursive subsystem # we need to loop over recursive subsystem pairs for level in range(Nlevs): maxLevIdx = ((maxLev - 2) * Nbands) + 1 resSzIdx = maxLevIdx - (level * Nbands) - 1 recon = upConv(image=recon, filt=lofilt, edges=edges, step=(2, 2), start=(0, 0), stop=self.pyrSize[resSzIdx]) bandImageIdx = 1 + (((Nlevs - 1) - level) * Nbands) for band in range(Nbands - 1, -1, -1): if bandImageIdx in reconList: filt = bfilts[:, (Nbands - 1) - band].reshape(bfiltsz, bfiltsz, order='F') recon = upConv(image=self.pyr[bandImageIdx], filt=filt, edges=edges, stop=(self.pyrSize[bandImageIdx][0], self.pyrSize[bandImageIdx][1]), result=recon) bandImageIdx += 1 # apply lo0filt sz = recon.shape recon = upConv(image=recon, filt=lo0filt, edges=edges, stop=sz) # apply hi0filt if needed if 0 in reconList: recon = upConv(image=self.pyr[0], filt=hi0filt, edges=edges, start=(0, 0), step=(1, 1), stop=recon.shape, result=recon) return recon
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
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