傅里叶变换
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('woman.tif',0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
s1 = np.log(np.abs(f))
s2 = np.log(np.abs(fshift))
ph_f = np.angle(f)
ph_fshift = np.angle(fshift)
plt.subplot(221),plt.imshow(s1,'gray'),plt.title('original magnitude')
plt.subplot(222),plt.imshow(s2,'gray'),plt.title('center magnitude')
plt.subplot(223),plt.imshow(ph_f,'gray'),plt.title('original phase')
plt.subplot(224),plt.imshow(ph_fshift,'gray'),plt.title('center phase')
plt.show();
傅里叶反变换
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('woman.tif',0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
s1 = np.log(np.abs(fshift))
plt.subplot(131),plt.imshow(img,'gray'),plt.title('original')
plt.xticks([]),plt.yticks([])
s1 = np.abs(fshift)
s1_angle = np.angle(fshift)
s1_real = s1*np.cos(s1_angle)
s1_imag = s1*np.sin(s1_angle)
s2 = np.zeros(img.shape,dtype=complex)
s2.real = np.array(s1_real)
s2.imag = np.array(s1_imag)
s3 = np.zeros(img.shape,dtype=complex)
s3.real = np.array(s1_real)
s3.imag = np.array(-s1_imag)
f2shift = np.fft.ifftshift(s2)
img_back = np.fft.ifft2(f2shift)
img_back = np.abs(img_back)
img_back = (img_back-np.amin(img_back))/(np.amax(img_back)-np.amin(img_back))
plt.subplot(132),plt.imshow(img_back,'gray'),plt.title('phase & amplitude')
plt.xticks([]),plt.yticks([])
f3shift = np.fft.ifftshift(s3)
img_back3 = np.fft.ifft2(f3shift)
img_back3 = np.abs(img_back3)
img_back3 = (img_back3-np.amin(img_back3))/(np.amax(img_back3)-np.amin(img_back3))
plt.subplot(133),plt.imshow(img_back3,'gray'),plt.title('conjugate')
plt.xticks([]),plt.yticks([])
plt.show();
低通滤波器
function [U, V] = dftuv(M, N)
u = 0:(M - 1);
v = 0:(N - 1);
idx = find(u > M/2);
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
[V, U] = meshgrid(v, u);
function H = lpfilter(type, M, N, D0, n)
%LPFILTER Computes frequency domain lowpass filters.
% H = LPFILTER(TYPE, M, N, D0, n) creates the transfer function of
% a lowpass filter, H, of the specified TYPE and size (M-by-N). To
% view the filter as an image or mesh plot, it should be centered
% using H = fftshift(H).
%
% Valid values for TYPE, D0, and n are:
%
% 'ideal' Ideal lowpass filter with cutoff frequency D0. n need
% not be supplied. D0 must be positive.
%
% 'btw' Butterworth lowpass filter of order n, and cutoff
% D0. The default value for n is 1.0. D0 must be
% positive.
%
% 'gaussian' Gaussian lowpass filter with cutoff (standard
% deviation) D0. n need not be supplied. D0 must be
% positive.
% Use function dftuv to set up the meshgrid arrays needed for
% computing the required distances.
[U, V] = dftuv(M, N);
% Compute the distances D(U, V).
D = sqrt(U.^2 + V.^2);
% Begin filter computations.
switch type
case 'ideal'
H = double(D <= D0);
case 'btw'
if nargin == 4
n = 1;
end
H = 1./(1 + (D./D0).^(2*n));
case 'gaussian'
H = exp(-(D.^2)./(2*(D0^2)));
otherwise
error('Unknown filter type.')
end
H1=fftshift(lpfilter(
H2=fftshift(lpfilter(
H3=fftshift(lpfilter(
subplot(1,3,1),mesh(H1(1:10:500,1:10:500));
subplot(1,3,2),mesh(H2(1:10:500,1:10:500));
subplot(1,3,3),mesh(H3(1:10:500,1:10:500));
axis([0 50 0 50 0 1]);
colormap([0 0 0])
axis off
grid off
view(-25,30);