1.频域滤波
% 构建
I = zeros(256, 256);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('高通巴特沃斯滤波');