数字图像的空间域滤波和频率域滤波

傅里叶变换

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)  # 默认结果中心点位置是在左上角,转移到中间位置
#取绝对值:将复数变化成实数
#取对数的目的为了将数据变化到较小的范围(比如0-255)
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
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)
%DFTUV Computes meshgrid frequency matrices.
%   [U, V] = DFTUV(M, N) computes meshgrid frequency matrices U and
%   V.  U and V are useful for computing frequency-domain filter
%   functions that can be used with DFTFILT.  U and V are both
%   M-by-N.

% Set up range of variables.
u = 0:(M - 1);
v = 0:(N - 1);

% Compute the indices for use in meshgrid.
idx = find(u > M/2);
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;

% Compute the meshgrid arrays.
[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('ideal',500,500,50));
H2=fftshift(lpfilter('btw',500,500,50));
H3=fftshift(lpfilter('gaussian',500,500,50));
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);

你可能感兴趣的:(#,数字图像处理)