def _gabor(self, FT): """ Helper function to apply Gabor filter in frequensy domain. """ if self._sf > self._size / 2: msg = ('Base frequency for Gabor ' 'noise is too high (exceeds Nyquist limit).') raise Warning(msg) localf = self._sf / self._size linbw = 2 ** self.noiseBW lowf = 2.0 * localf / (linbw + 1.0) highf = linbw * lowf FWF = highf - lowf sigmaF = FWF/(2*numpy.sqrt(2*numpy.log(2))) FWO = 2.0*localf*numpy.tan(numpy.pi*self.noiseBWO/360.0) sigmaO = FWO/(2*numpy.sqrt(2*numpy.log(2))) yy, xx = numpy.mgrid[0:self._size, 0:self._size] xx = (0.5 - 1.0 / self._size * xx) yy = (0.5 - 1.0 / self._size * yy) filter=filters.make2DGauss(xx,yy,mean=(localf,0), sd=(sigmaF,sigmaO)) filter=filter+filters.make2DGauss(xx,yy, mean=(-localf,0), sd=(sigmaF,sigmaO)) filter = numpy.array( Image.fromarray(filter).rotate( self.noiseOri, Image.BICUBIC ) ) return FT*filter
def _gabor(self, FT): """ Helper function to apply Gabor filter in frequensy domain. """ if self._sf > self._size / 2: msg = ('Base frequency for Gabor ' 'noise is too high (exceeds Nyquist limit).') raise Warning(msg) localf = self._sf / self._size linbw = 2 ** self.noiseBW lowf = 2.0 * localf / (linbw + 1.0) highf = linbw * lowf FWF = highf - lowf sigmaF = FWF/(2*numpy.sqrt(2*numpy.log(2))) FWO = 2.0*localf*numpy.tan(numpy.pi*self.noiseBWO/360.0) sigmaO = FWO/(2*numpy.sqrt(2*numpy.log(2))) yy, xx = numpy.mgrid[0:self._size, 0:self._size] xx = (0.5 - 1.0 / self._size * xx) yy = (0.5 - 1.0 / self._size * yy) filter=filters.make2DGauss(xx,yy,mean=(localf,0), sd=(sigmaF,sigmaO)) filter=filter+filters.make2DGauss(xx,yy, mean=(-localf,0), sd=(sigmaF,sigmaO)) filter=imrotate(filter, self.noiseOri, interp='bicubic') return FT*filter
def buildNoise(self): """build a new noise sample. Required to act on changes to any noise parameters or texRes. """ if self.units == 'pix': if not (self.noiseType in ['Binary','binary','Normal','normal','uniform','Uniform']): mysize = numpy.max(self.size) else: mysize = self.size sampleSize = self.noiseElementSize mysf = self.__dict__['noiseBaseSf']*mysize lowsf = self.noiseFilterLower*mysize upsf = self.noiseFilterUpper*mysize else: mysize = self.texRes pixSize = self.size/self.texRes sampleSize = self.noiseElementSize/pixSize mysf = self.size[0]*self.noiseBaseSf lowsf = self.size[0]*self.noiseFilterLower upsf = self.size[0]*self.noiseFilterUpper self._size = mysize # store for use by updateNoise() self._sf = mysf if self.noiseType in ['binary','Binary','normal','Normal','uniform','Uniform']: self._sideLength = numpy.round(mysize/sampleSize) # dummy side length for use when unpacking noise samples in updateNoise() self._sideLength.astype(int) if ((self._sideLength[0] < 2) and (self._sideLength[1] < 2)): msg=('Noise sample size ' 'must result in more than ' '1 sample per image dimension.') raise ValueError(msg) totalSamples = self._sideLength[0]*self._sideLength[1] if self.noiseType in ['binary','Binary']: self.noiseTex=numpy.append(numpy.ones(int(numpy.round(totalSamples/2.0))),-1*numpy.ones(int(numpy.round(totalSamples/2.0)))) elif self.noiseType in ['White','white']: self.noiseTex = numpy.ones((int(mysize),int(mysize))) self.noiseTex[0][0] = 0 #elif self.noiseType in ['Coloured','coloured']: # pin=filters.makeRadialMatrix(matrixSize=mysize, center=(0,0), radius=1.0) # self.noiseTex=numpy.multiply(numpy.ones((int(mysize),int(mysize))),(pin)**self.noiseFractalPower) # self.noiseTex=fftshift(self.noiseTex) # self.noiseTex[0][0]=0 elif self.noiseType in ['Isotropic','isotropic']: if mysf > mysize/2: msg = ('Base frequency for isotropic ' 'noise is definitely too high.') raise Warning(msg) localf = mysf/mysize linbw = 2**self.noiseBW lowf = 2.0*localf/(linbw+1.0) highf = linbw*lowf FWF = highf-lowf sigmaF = FWF/(2*numpy.sqrt(2*numpy.log(2))) self.noiseTex = numpy.zeros(int(mysize**2)) self.noiseTex = numpy.reshape(self.noiseTex,(int(mysize),int(mysize))) pin = filters.makeRadialMatrix(matrixSize=mysize, center=(0,0), radius=2) self.noiseTex = filters.makeGauss(pin, mean=localf, sd=sigmaF) self.noiseTex = fftshift(self.noiseTex) self.noiseTex[0][0] = 0 elif self.noiseType in ['Gabor','gabor']: if mysf > mysize/2: msg = ('Base frequency for Gabor ' 'noise is definitely too high.') raise Warning(msg) localf = mysf/mysize linbw = 2**self.noiseBW lowf = 2.0*localf/(linbw+1.0) highf = linbw*lowf FWF = highf-lowf sigmaF = FWF/(2*numpy.sqrt(2*numpy.log(2))) FWO = 2.0*localf*numpy.tan(numpy.pi*self.noiseBWO/360.0) sigmaO = FWO/(2*numpy.sqrt(2*numpy.log(2))) self.noiseTex=numpy.zeros(int(mysize**2)) self.noiseTex=numpy.reshape(self.noiseTex,(int(mysize),int(mysize))) yy, xx = numpy.mgrid[0:mysize, 0:mysize] xx = (0.5 - 1.0 / mysize * xx) yy = (0.5 - 1.0 / mysize * yy) self.noiseTex=filters.make2DGauss(xx,yy,mean=(localf,0), sd=(sigmaF,sigmaO)) self.noiseTex=self.noiseTex+filters.make2DGauss(xx,yy, mean=(-localf,0), sd=(sigmaF,sigmaO)) self.noiseTex=fftshift(self.noiseTex) self.noiseTex[0][0]=0 elif self.noiseType in ['Image','image']: if not(self.noiseImage in ['None','none']): im = Image.open(self.noiseImage) im = im.transpose(Image.FLIP_TOP_BOTTOM) im = im.convert("L") # FORCE TO LUMINANCE intensity = numpy.array(im).astype( numpy.float32) * 0.0078431372549019607 - 1.0 self.noiseTex = numpy.absolute(fft2(intensity)) else: self.noiseTex = numpy.ones((int(mysize),int(mysize))) # if image is 'None' will make white noise as tempary measure self.noiseTex[0][0]=0 elif self.noiseType in ['filtered','Filtered']: pin=filters.makeRadialMatrix(matrixSize=mysize, center=(0,0), radius=1.0) self.noiseTex = numpy.multiply(numpy.ones((int(mysize),int(mysize))),(pin)**self.noiseFractalPower) if lowsf > mysize/2: msg = ('Lower cut off frequency for filtered ' 'noise is definitely too high.') raise Warning(msg) if self.noiseFilterOrder > 0.01: if upsf<(mysize/2.0): filter = filters.butter2d_lp_elliptic(size=[mysize,mysize], cutoff_x=upsf/mysize, cutoff_y=upsf/mysize, n=self.noiseFilterOrder, alpha=0, offset_x=2/(mysize-1),offset_y=2/(mysize-1)) else: filter = numpy.ones((int(mysize),int(mysize))) if lowsf>0: filter = filter-filters.butter2d_lp_elliptic(size=[mysize,mysize], cutoff_x=lowsf/mysize, cutoff_y=lowsf/mysize, n=self.noiseFilterOrder, alpha=0, offset_x=2/(mysize-1),offset_y=2/(mysize-1)) self.noiseTex = self.noiseTex*filter self.noiseTex = fftshift(self.noiseTex) self.noiseTex[0][0] = 0 else: raise ValueError('Noise type not recognised.') self._needBuild = False # prevent noise from being re-built at next draw() unless a parameter is chnaged in the mean time. self.updateNoise() # now choose the initial random sample.
def buildNoise(self): """build a new noise sample. Required to act on changes to any noise parameters or texRes. """ if self.units == 'pix': if not (self.noiseType in ['Binary','binary','Normal','normal','uniform','Uniform']): mysize = numpy.max(self.size) else: mysize = self.size sampleSize = self.noiseElementSize mysf = self.__dict__['noiseBaseSf']*mysize lowsf = self.noiseFilterLower*mysize upsf = self.noiseFilterUpper*mysize else: mysize = self.texRes pixSize = self.size/self.texRes sampleSize = self.noiseElementSize/pixSize mysf = self.size[0]*self.noiseBaseSf lowsf = self.size[0]*self.noiseFilterLower upsf = self.size[0]*self.noiseFilterUpper self._size = mysize # store for use by updateNoise() self._sf = mysf if self.noiseType in ['binary','Binary','normal','Normal','uniform','Uniform']: self._sideLength = numpy.round(mysize/sampleSize) # dummy side length for use when unpacking noise samples in updateNoise() self._sideLength.astype(int) if ((self._sideLength[0] < 2) and (self._sideLength[1] < 2)): msg=('Noise sample size ' 'must result in more than ' '1 sample per image dimension.') raise ValueError(msg) totalSamples = self._sideLength[0]*self._sideLength[1] if self.noiseType in ['binary','Binary']: self.noiseTex=numpy.append(numpy.ones(int(numpy.round(totalSamples/2.0))),-1*numpy.ones(int(numpy.round(totalSamples/2.0)))) elif self.noiseType in ['White','white']: self.noiseTex = numpy.ones((int(mysize),int(mysize))) self.noiseTex[0][0] = 0 #elif self.noiseType in ['Coloured','coloured']: # pin=filters.makeRadialMatrix(matrixSize=mysize, center=(0,0), radius=1.0) # self.noiseTex=numpy.multiply(numpy.ones((int(mysize),int(mysize))),(pin)**self.noiseFractalPower) # self.noiseTex=fftshift(self.noiseTex) # self.noiseTex[0][0]=0 elif self.noiseType in ['Isotropic','isotropic']: if mysf > mysize/2: msg = ('Base frequency for isotropic ' 'noise is definitely too high.') raise Warning(msg) localf = mysf/mysize linbw = 2**self.noiseBW lowf = 2.0*localf/(linbw+1.0) highf = linbw*lowf FWF = highf-lowf sigmaF = FWF/(2*numpy.sqrt(2*numpy.log(2))) self.noiseTex = numpy.zeros(int(mysize**2)) self.noiseTex = numpy.reshape(self.noiseTex,(int(mysize),int(mysize))) pin = filters.makeRadialMatrix(matrixSize=mysize, center=(0,0), radius=2) self.noiseTex = filters.makeGauss(pin, mean=localf, sd=sigmaF) self.noiseTex = fftshift(self.noiseTex) self.noiseTex[0][0] = 0 elif self.noiseType in ['Gabor','gabor']: if mysf > mysize/2: msg = ('Base frequency for Gabor ' 'noise is definitely too high.') raise Warning(msg) localf = mysf/mysize linbw = 2**self.noiseBW lowf = 2.0*localf/(linbw+1.0) highf = linbw*lowf FWF = highf-lowf sigmaF = FWF/(2*numpy.sqrt(2*numpy.log(2))) FWO = 2.0*localf*numpy.tan(numpy.pi*self.noiseBWO/360.0) sigmaO = FWO/(2*numpy.sqrt(2*numpy.log(2))) self.noiseTex=numpy.zeros(int(mysize**2)) self.noiseTex=numpy.reshape(self.noiseTex,(int(mysize),int(mysize))) yy, xx = numpy.mgrid[0:mysize, 0:mysize] xx = (0.5 - 1.0 / mysize * xx) yy = (0.5 - 1.0 / mysize * yy) self.noiseTex=filters.make2DGauss(xx,yy,mean=(localf,0), sd=(sigmaF,sigmaO)) self.noiseTex=self.noiseTex+filters.make2DGauss(xx,yy, mean=(-localf,0), sd=(sigmaF,sigmaO)) self.noiseTex=fftshift(self.noiseTex) self.noiseTex[0][0]=0 elif self.noiseType in ['Image','image']: if not(self.noiseImage in ['None','none']): im = Image.open(self.noiseImage) im = im.transpose(Image.FLIP_TOP_BOTTOM) im = im.convert("L") # FORCE TO LUMINANCE intensity = numpy.array(im).astype( numpy.float32) * 0.0078431372549019607 - 1.0 self.noiseTex = numpy.absolute(fft2(intensity)) else: self.noiseTex = numpy.ones((int(mysize),int(mysize))) # if image is 'None' will make white noise as tempary measure self.noiseTex[0][0]=0 elif self.noiseType in ['filtered','Filtered']: pin=filters.makeRadialMatrix(matrixSize=mysize, center=(0,0), radius=1.0) self.noiseTex = numpy.multiply(numpy.ones((int(mysize),int(mysize))),(pin)**self.noiseFractalPower) if lowsf > mysize/2: msg = ('Lower cut off frequency for filtered ' 'noise is definitely too high.') raise Warning(msg) if self.noiseFilterOrder > 0.01: if upsf<(mysize/2.0): filter = filters.butter2d_lp_elliptic(size=[mysize,mysize], cutoff_x=upsf/mysize, cutoff_y=upsf/mysize, n=self.noiseFilterOrder, alpha=0, offset_x=2/(mysize-1),offset_y=2/(mysize-1)) else: filter = numpy.ones((int(mysize),int(mysize))) if lowsf>0: filter = filter-filters.butter2d_lp_elliptic(size=[mysize,mysize], cutoff_x=lowsf/mysize, cutoff_y=lowsf/mysize, n=self.noiseFilterOrder, alpha=0, offset_x=2/(mysize-1),offset_y=2/(mysize-1)) self.noiseTex = self.noiseTex*filter self.noiseTex = fftshift(self.noiseTex) self.noiseTex[0][0] = 0 else: raise ValueError('Noise type not recognised.') self._needBuild = False # prevent noise from being re-built at next draw() unless a parameter is chnaged in the mean time. self.updateNoise() # now choose the inital random sample.