def calc_log_avg_fft(x): if len(x.shape) == 2: return np.log( fftshift( np.abs(fftpack.fft2(x.reshape(x.shape[0], 32, 32))).mean(axis=0))) else: return np.log(fftshift(np.abs(fftpack.fft2(x.reshape(32, 32)))))
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 __init__(self): self.fig = plt.figure() originalImage = Image.open('Fourier.jpg') (ow, oh) = originalImage.size if ow > maxWidth: monoImageArray = numpy.asarray( originalImage.convert('L').resize( (maxWidth, oh * maxWidth // ow))) else: monoImageArray = numpy.asarray(originalImage.convert('L')) (self.imageHeight, self.imageWidth) = monoImageArray.shape self.samples = numpy.zeros((self.imageHeight, self.imageWidth), dtype=complex) self.samplePoints = numpy.zeros((self.imageHeight, self.imageWidth, 4)) self.fftImage = fftpack.fft2(monoImageArray) self.fftImageForPlot = numpy.roll(numpy.roll(numpy.real(self.fftImage), self.imageHeight // 2, axis=0), self.imageWidth // 2, axis=1) self.fftMean = numpy.mean(self.fftImageForPlot) self.fftStd = numpy.std(self.fftImageForPlot) self.axes1 = plt.subplot(2, 3, 1) plt.imshow(monoImageArray, cmap='gray') self.axes2 = plt.subplot(2, 3, 2) p = plt.imshow(self.fftImageForPlot, cmap='gray') p.set_clim(self.fftMean - self.fftStd, self.fftMean + self.fftStd) self.axes3 = plt.subplot(2, 3, 4) self.axes3.set_aspect('equal') self.axes3.set_xlim(0, self.imageWidth) self.axes3.set_ylim(self.imageHeight, 0) self.axes4 = plt.subplot(2, 3, 5) self.axes4.set_aspect('equal') self.axes4.set_xlim(0, self.imageWidth) self.axes4.set_ylim(self.imageHeight, 0) self.axes5 = plt.subplot(2, 3, 6) self.axes5.set_aspect('equal') self.axes5.set_xlim(0, self.imageWidth) self.axes5.set_ylim(self.imageHeight, 0) self.bMousePressed = False self.mouseButton = 0 self.bCtrlPressed = False self.fig.canvas.mpl_connect('motion_notify_event', self.onMove) self.fig.canvas.mpl_connect('button_press_event', self.onButtonPress) self.fig.canvas.mpl_connect('button_release_event', self.onButtonRelease) self.fig.canvas.mpl_connect('key_press_event', self.onKeyPress) self.fig.canvas.mpl_connect('key_release_event', self.onKeyRelease) plt.show()
def power_spectrum(pref, peak_val=1.0): """ Computes the FFT power spectrum of the orientation preference. """ fft_spectrum = abs(fftshift(fft2(pref-0.5, s=None, axes=(-2,-1)))) fft_spectrum = 1 - fft_spectrum # Inverted spectrum by convention zero_min_spectrum = fft_spectrum - fft_spectrum.min() spectrum_range = fft_spectrum.max() - fft_spectrum.min() normalized_spectrum = (peak_val * zero_min_spectrum) / spectrum_range return normalized_spectrum
def power_spectrum(pref, peak_val=1.0): """ Computes the FFT power spectrum of the orientation preference. """ fft_spectrum = abs(fftshift(fft2(pref - 0.5, s=None, axes=(-2, -1)))) fft_spectrum = 1 - fft_spectrum # Inverted spectrum by convention zero_min_spectrum = fft_spectrum - fft_spectrum.min() spectrum_range = fft_spectrum.max() - fft_spectrum.min() normalized_spectrum = (peak_val * zero_min_spectrum) / spectrum_range return normalized_spectrum
def _process(self, sheetview): cr = sheetview.cyclic_range data = sheetview.data if cr is None else sheetview.data/cr fft_spectrum = abs(fftshift(fft2(data - 0.5, s=None, axes=(-2, -1)))) fft_spectrum = 1 - fft_spectrum # Inverted spectrum by convention zero_min_spectrum = fft_spectrum - fft_spectrum.min() spectrum_range = fft_spectrum.max() - fft_spectrum.min() normalized_spectrum = (self.p.peak_val * zero_min_spectrum) / spectrum_range l, b, r, t = sheetview.bounds.lbrt() density = sheetview.xdensity bb = BoundingBox(radius=(density/2)/(r-l)) return [SheetView(normalized_spectrum, bb, metadata=sheetview.metadata, label=sheetview.label+' FFT Power Spectrum')]
def OR_power_spectrum(image, toarray=True, peak_val=1.0): """ Taken from current topographica code. Applies FFT power spectrum to hue channel of OR maps (ie orientation). Accepts RGB images or arrays.""" # Duplicates the line of code in command/pylabplot.py and tkgui. # Unfortunately, there is no sensible way to reuse the existing code if not toarray: peak_val = 255 if not image.shape[0] % 2: hue = image[:-1, :-1] else: hue = image fft_spectrum = abs(fftshift(fft2(hue - 0.5, s=None, axes=(-2, -1)))) fft_spectrum = 1 - fft_spectrum # Inverted spectrum by convention zero_min_spectrum = fft_spectrum - fft_spectrum.min() spectrum_range = fft_spectrum.max() - fft_spectrum.min() normalized_spectrum = (peak_val * zero_min_spectrum) / spectrum_range if not toarray: return Image.fromarray(normalized_spectrum.astype('uint8'), mode='L') return normalized_spectrum
def __call__(self, data, **params): p = ParamOverrides(self, params) fft_plot = 1 - np.abs(fftshift(fft2(data - 0.5, s=None, axes=(-2, -1)))) return super(fftplot, self).__call__(fft_plot, **p)
def get_angle(x): return np.angle(fftpack.fft2(x.reshape(32, 32)))
def get_abs(x): return np.abs(fftpack.fft2(x.reshape(32, 32)))
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