图像频域滤波(二)

1.频域滤波

% 构建

I = zeros(256, 256);
% 增加中间显示
I(128-15:128+15, 128-10:128+10) = 1;
% 二值化变换
I = logical(I);figure; imshow(I); title('原图像');
% 类型变换
J = double(I);
% 傅里叶变换
Jf = fft2(J);
% 幅度
Jf = abs(Jf);figure; imshow(Jf, []); title('直接显示');
% 能量集中
Pf = fftshift(Jf);figure; imshow(Pf, []); title('中间显示');
% log延伸

Lf = log(Pf);figure; imshow(Lf, []); title('延伸显示');


2.频域滤波增强

f = imread('gantrycrane.png');
% 灰度化
f = rgb2gray(f);
% 时域
h = fspecial('sobel');fg = double(f);s = imfilter(fg, h, 'replicate');
% 频域
H = freqz2(h, size(f, 2), size(f, 1));H = ifftshift(abs(H));F = fft2(fg);r = real(ifft2(H.*F));
figure; imshow(f, []); title('原图像');
figure; imshow(mat2gray(s), []); title('时域');
figure; imshow(mat2gray(r), []); title('频域');



3.字母定位

% 原图
I = imread('text.png'); 
% 提取a
a = I(28:47, 85:100); 
figure;subplot(2, 2, 1); imshow(I, []); title('原图');subplot(2, 2, 2); imshow(a, []); title('a图')
a1 = rot90(a, 2);subplot(2, 2, 3); imshow(a1, []); title('旋转180°')
C = real(ifft2(fft2(I) .* fft2(a1, 256, 256)));subplot(2, 2, 4); imshow(C, []); title('fft变换后')
% 阈值
bw = C > 60; 
[r, c] = find(bw == 1);
figure;subplot(1, 2, 1); imshow(bw, []); title('阈值分割');subplot(1, 2, 2); imshow(I, []); title('定位分析');
rate = 0.8;
for i = 1 : length(c)
    startx = c(i)-size(a, 2)*rate;
    starty = r(i)-size(a, 1)*rate;
    rectangle('position', [startx, starty, size(a, 2)*rate, size(a, 1)*rate], ...
        'EdgeColor', 'r', 'LineWidth', 3);
end



4.几种频域滤波器比较

f = imread('gantrycrane.png');
if ndims(f) == 3
    f = rgb2gray(f);
end
% f = imnoise(f, 'gaussian');
% 数据类型
fg = double(f);
F = fft2(fg);
% 计算滤波器
M = size(f, 1);N = size(f, 2);
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);D = sqrt(U.^2 + V.^2);D0 = 0.1 * size(f, 1);
n = 2;H1 = double(D <= D0);H2 = exp(-(D.^2)./(2*(D0^2)));H3 = 1./(1 + (D./D0).^(2*n));
% 滤波
r1 = real(ifft2(H1.*F));r2 = real(ifft2(H2.*F));r3 = real(ifft2(H3.*F));
figure; imshow(f, []); title('原图像');
figure; imshow(mat2gray(r1), []); title('理想滤波');
figure; imshow(mat2gray(r2), []); title('高斯滤波');
figure; imshow(mat2gray(r3), []); title('巴特沃斯滤波');
% 高通滤波
s1 = real(ifft2((1-H1).*F));
s2 = real(ifft2((1-H2).*F));
s3 = real(ifft2((1-H3).*F));
figure; imshow(f, []); title('原图像');
figure; imshow(mat2gray(s1), []); title('高通理想滤波');
figure; imshow(mat2gray(s2), []); title('高通高斯滤波');
figure; imshow(mat2gray(s3), []); title('高通巴特沃斯滤波');

Python中可以使用numpy和opencv库进行图像频域滤波。 1.使用numpy库实现图像频域滤波 频域滤波的步骤是: (1)读入图像 (2)将图像转换为灰度图 (3)进行傅里叶变换,得到频域图像 (4)设计滤波器 (5)对频域图像进行滤波操作 (6)进行傅里叶逆变换,得到滤波后的图像 代码示例: ```python import cv2 import numpy as np # 读入图像 img = cv2.imread('img.jpg') # 将图像转换为灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 进行傅里叶变换,得到频域图像 f = np.fft.fft2(gray) fshift = np.fft.fftshift(f) # 构建高通滤波器 rows, cols = gray.shape crow, ccol = rows // 2, cols // 2 mask = np.ones((rows, cols), np.uint8) mask[crow - 30: crow + 30, ccol - 30: ccol + 30] = 0 # 对频域图像进行滤波操作 fshift = fshift * mask # 进行傅里叶逆变换,得到滤波后的图像 ishift = np.fft.ifftshift(fshift) i = np.fft.ifft2(ishift) result = np.abs(i) # 显示原图和滤波后的图像 cv2.imshow('Original', gray) cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() ``` 2.使用opencv库实现图像频域滤波 OpenCV提供了cv2.dft()和cv2.idft()函数,可以方便地实现图像的傅里叶变换和逆变换。与numpy库相比,opencv库的实现更加简单。频域滤波的步骤与上面的步骤相同。 代码示例: ```python import cv2 import numpy as np # 读入图像 img = cv2.imread('img.jpg') # 将图像转换为灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 进行傅里叶变换,得到频域图像 dft = cv2.dft(np.float32(gray), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) # 构建高通滤波器 rows, cols = gray.shape crow, ccol = rows // 2, cols // 2 mask = np.ones((rows, cols, 2), np.uint8) mask[crow - 30: crow + 30, ccol - 30: ccol + 30] = 0 # 对频域图像进行滤波操作 fshift = dft_shift * mask # 进行傅里叶逆变换,得到滤波后的图像 ishift = np.fft.ifftshift(fshift) i = cv2.idft(ishift) result = cv2.magnitude(i[:, :, 0], i[:, :, 1]) # 显示原图和滤波后的图像 cv2.imshow('Original', gray) cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值