def update(self, event): if event.inaxes != self.axes4: return if event.xdata != None: x = (int(event.xdata)+self.imageWidth//2)%self.imageWidth y = (int(event.ydata)+self.imageHeight//2)%self.imageHeight plt.sca(self.axes5) plt.cla() waveImg = numpy.zeros((self.imageHeight,self.imageWidth)) waveImg[y,x] = 1 plt.imshow(numpy.real(fftpack.ifft2(waveImg)), cmap='gray') if not self.bCtrlPressed: bNeedUpdate = False if self.samples[y,x] != self.fftImage[y,x] and self.mouseButton == 1: #left button bNeedUpdate = True self.samples[y,x] = self.fftImage[y,x] self.samplePoints[(y-self.imageHeight//2)%self.imageHeight,(x-self.imageWidth//2)%self.imageWidth,0] = 1 self.samplePoints[(y-self.imageHeight//2)%self.imageHeight,(x-self.imageWidth//2)%self.imageWidth,3] = 1 if bNeedUpdate: plt.sca(self.axes4) plt.cla() p = plt.imshow(self.fftImageForPlot, cmap='gray') p.set_clim(self.fftMean-self.fftStd,self.fftMean+self.fftStd) plt.imshow(self.samplePoints) plt.sca(self.axes3) plt.cla() plt.imshow(numpy.real(fftpack.ifft2(self.samples)), cmap='gray') else: for xi in range(x-self.imageWidth//32, x+self.imageWidth//32): for yi in range(y-self.imageWidth//32, y+self.imageWidth//32): if xi>=self.imageWidth: xx = xi-self.imageWidth else: xx = xi if yi>=self.imageHeight: yy = yi-self.imageHeight else: yy = yi if self.mouseButton == 1: #left button self.samples[yy,xx] = self.fftImage[yy,xx] self.samplePoints[(yy-self.imageHeight//2)%self.imageHeight,(xx-self.imageWidth//2)%self.imageWidth,0] = 1 self.samplePoints[(yy-self.imageHeight//2)%self.imageHeight,(xx-self.imageWidth//2)%self.imageWidth,3] = 0.7 plt.sca(self.axes4) plt.cla() plt.imshow(self.samplePoints) plt.sca(self.axes3) plt.cla() plt.imshow(numpy.real(fftpack.ifft2(self.samples)), cmap='gray') self.fig.canvas.draw()
def update(self, event): if event.inaxes != self.axes4: return if event.xdata != None: x = (int(event.xdata) + self.imageWidth // 2) % self.imageWidth y = (int(event.ydata) + self.imageHeight // 2) % self.imageHeight plt.sca(self.axes5) plt.cla() waveImg = numpy.zeros((self.imageHeight, self.imageWidth)) waveImg[y, x] = 1 plt.title('wave image') plt.imshow(numpy.real(fftpack.ifft2(waveImg)), cmap='gray') for xi in range(x - self.imageWidth // 64, x + self.imageWidth // 64): for yi in range(y - self.imageWidth // 64, y + self.imageWidth // 64): if xi >= self.imageWidth: xx = xi - self.imageWidth else: xx = xi if yi >= self.imageHeight: yy = yi - self.imageHeight else: yy = yi if self.mouseButton == 1: #left button self.samples[yy, xx] = self.fftImage[yy, xx] self.samplePoints[(yy - self.imageHeight // 2) % self.imageHeight, (xx - self.imageWidth // 2) % self.imageWidth, 0] = 0 self.samplePoints[(yy - self.imageHeight // 2) % self.imageHeight, (xx - self.imageWidth // 2) % self.imageWidth, 3] = 1 plt.sca(self.axes4) plt.cla() plt.title('mask image') plt.imshow(self.samplePoints) plt.sca(self.axes3) plt.cla() plt.title('ifft image') plt.imshow(numpy.real(fftpack.ifft2(self.samples)), cmap='gray') self.fig.canvas.draw()
def wiener_filter(img, kernel, K = 10): temp_img = np.copy(img) kernel = np.pad(kernel, [(0, temp_img.shape[0] - kernel.shape[0]), (0, temp_img.shape[1] - kernel.shape[1])], 'constant') # Fourier Transform temp_img = fft2(temp_img) kernel = fft2(kernel) kernel = np.conj(kernel) / (np.abs(kernel) ** 2 + K) temp_img = temp_img * kernel temp_img = np.abs(ifft2(temp_img)) return np.uint8(temp_img)
def fft_convolve(im, filt): t1 = time.clock() shp_w = im.shape[0] + filt.shape[0] shp_h = im.shape[1] + filt.shape[1] f_c = fftpack.fft2(filt[::-1, ::-1], s=(shp_w, shp_h)) f_im = fftpack.fft2(im, s=(shp_w, shp_h)) res = np.real(fftpack.ifft2(f_c * f_im)) b = (filt.shape[0] - 1) // 2 res = res[b:-b - 1, b:-b - 1] t2 = time.clock() tt = t2 - t1 print("Elapsed time fft_convolve %fs" % tt) return res, tt
def terrain(res, lin, exp): print "Creating noise" realnoise = rand(res, res) imagnoise = rand(res, res) print "Normalizing noise" realnoise = [[2*_-1 for _ in __] for __ in realnoise] imagnoise = [[2*_-1 for _ in __] for __ in imagnoise] complexnoise = [[complex(0) for _ in range(res)] for __ in range(res)] for i in range(res): for j in range(res): complexnoise[i][j] = complex(realnoise[i][j], imagnoise[i][j]) x = (i+0.5)/res-0.5 y = (j+0.5)/res-0.5 dist = x*x+y*y nf = (lin*dist+0.1)**(-exp) complexnoise[i][j] = complex(realnoise[i][j]*nf, imagnoise[i][j]*nf) print "Performing backwards FFT2" fourier = ifft2(complexnoise) fourier = absolute(fourier) fourier -= fourier.min() fourier /= fourier.max() return fourier.tolist()
mixarr.flat[0].matshow(cat_x.reshape(32, 32), cmap="gray") mixarr.flat[0].axis('off') mixarr.flat[0].set_title("cat") mixarr.flat[1].matshow(auto_x.reshape(32, 32), cmap="gray") mixarr.flat[1].axis('off') mixarr.flat[1].set_title("auto") mixarr.flat[2].matshow(calc_log_avg_fft(cat_x), cmap="gray") mixarr.flat[2].axis('off') mixarr.flat[2].set_title("cat") mixarr.flat[3].matshow(calc_log_avg_fft(auto_x), cmap="gray") mixarr.flat[3].axis('off') mixarr.flat[3].set_title("automobile") abs_cat = get_abs(cat_x) angle_cat = get_angle(cat_x) abs_auto = get_abs(auto_x) angle_auto = get_angle(auto_x) rec_cat = np.real(fftpack.ifft2(abs_cat * np.exp(1j * angle_auto))) rec_auto = np.real(fftpack.ifft2(abs_auto * np.exp(1j * angle_cat))) mixarr.flat[4].matshow(rec_cat, cmap="gray") mixarr.flat[4].axis('off') mixarr.flat[4].set_title("cat+automobile_reconstruction") mixarr.flat[5].matshow(rec_auto, cmap="gray") mixarr.flat[5].axis('off') mixarr.flat[5].set_title("automobile+cat_reconstruction") plt.show()
from matplotlib.pyplot import contour, show, contourf, pcolor from numpy.fft.fftpack import ifft2 from numpy.ma.core import absolute from numpy.random.mtrand import rand res = 128 print "Creating noise" realnoise = rand(res, res) imagnoise = rand(res, res) print "Normalizing noise" realnoise = [[2*_-1 for _ in __] for __ in realnoise] imagnoise = [[2*_-1 for _ in __] for __ in imagnoise] complexnoise = [[complex(0) for _ in range(res)] for __ in range(res)] for i in range(res): for j in range(res): complexnoise[i][j] = complex(realnoise[i][j], imagnoise[i][j]) for i in range(res): for j in range(res): x = (i+0.5)/res-0.5 y = (j+0.5)/res-0.5 dist = x*x+y*y nf = (10000*dist+0.1)**(-1.2) complexnoise[i][j] = complex(realnoise[i][j]*nf, imagnoise[i][j]*nf) print "Performing backwards FFT2" fourier = ifft2(complexnoise) pcolor(absolute(fourier)) show()
def phasecong100(im, nscale=2, norient=2, minWavelength=7, mult=2, sigmaOnf=0.65): # # im # Input image # nscale = 2; # Number of wavelet scales. # norient = 2; # Number of filter orientations. # minWaveLength = 7; # Wavelength of smallest scale filter. # mult = 2; # Scaling factor between successive filters. # sigmaOnf = 0.65; # Ratio of the standard deviation of the # # Gaussian describing the log Gabor filter's # # transfer function in the frequency domain # # to the filter center frequency. rows, cols = im.shape imagefft = fft2(im) zero = np.zeros(shape=(rows, cols)) EO = dict() EnergyV = np.zeros((rows, cols, 3)) x_range = np.linspace(-0.5, 0.5, num=cols, endpoint=True) y_range = np.linspace(-0.5, 0.5, num=rows, endpoint=True) x, y = np.meshgrid(x_range, y_range) radius = np.sqrt(x**2 + y**2) theta = np.arctan2(-y, x) radius = ifftshift(radius) theta = ifftshift(theta) radius[0, 0] = 1. sintheta = np.sin(theta) costheta = np.cos(theta) lp = lowpass_filter((rows, cols), 0.45, 15) logGabor = [] for s in range(1, nscale + 1): wavelength = minWavelength * mult**(s - 1.) fo = 1.0 / wavelength logGabor.append( np.exp((-(np.log(radius / fo))**2) / (2 * np.log(sigmaOnf)**2))) logGabor[-1] *= lp logGabor[-1][0, 0] = 0 # The main loop... for o in range(1, norient + 1): angl = (o - 1.) * np.pi / norient ds = sintheta * np.cos(angl) - costheta * np.sin(angl) dc = costheta * np.cos(angl) + sintheta * np.sin(angl) dtheta = np.abs(np.arctan2(ds, dc)) dtheta = np.minimum(dtheta * norient / 2., np.pi) spread = (np.cos(dtheta) + 1.) / 2. sumE_ThisOrient = zero.copy() sumO_ThisOrient = zero.copy() for s in range(0, nscale): filter_ = logGabor[s] * spread EO[(s, o)] = ifft2(imagefft * filter_) sumE_ThisOrient = sumE_ThisOrient + np.real(EO[(s, o)]) sumO_ThisOrient = sumO_ThisOrient + np.imag(EO[(s, o)]) EnergyV[:, :, 0] = EnergyV[:, :, 0] + sumE_ThisOrient EnergyV[:, :, 1] = EnergyV[:, :, 1] + np.cos(angl) * sumO_ThisOrient EnergyV[:, :, 2] = EnergyV[:, :, 2] + np.sin(angl) * sumO_ThisOrient OddV = np.sqrt(EnergyV[:, :, 0]**2 + EnergyV[:, :, 1]**2) featType = np.arctan2(EnergyV[:, :, 0], OddV) return featType