图像的离散傅里叶变换

close all, clear, clc
warning off all
img_w = 640; img_h = img_w;
xOfCenter = img_w / 2; yOfCenter = img_h / 2;
%% DFT of oblique rectangle
img_oblique_rect = zeros(img_h, img_w);
% create a oblique(45) rectangle in the matrix
x1 = int32(img_w / 5 * 2); x2 = int32(img_w / 5 * 3);
y1 = int32(img_h / 7);     y2 = int32(img_h / 7 * 6);
img_oblique_rect(y1:y2, x1:x2) = 1;
% Bilinear interpolation
img_oblique_rect = imrotate(img_oblique_rect, 45, 'bilinear','crop'); 
img_oblique_rect = imcomplement(img_oblique_rect);
% DFT using fft2 function
img_after_fft2   = fftshift(fft2(img_oblique_rect));
figure, imshow(img_oblique_rect)
figure, imshow(log(abs(img_after_fft2)), [-1 5])
%% DFT of circle
img_circle = ones(img_h, img_w);
% create a circle in the matrix
r = min(img_w / 6, img_h / 6);
x1 = int32(xOfCenter - r); x2 = int32(xOfCenter + r);
y1 = int32(yOfCenter - r); y2 = int32(yOfCenter + r);
for h = y1:y2
    for w = x1:x2
        if (h - yOfCenter).^2 + (w - xOfCenter).^2 <= r.^2
            img_circle(h, w) = 0;
        end
    end
end
% DFT using fft2 function
img_after_fft2 = fftshift(fft2(img_circle));
figure, imshow(img_circle)
figure, imshow(log(abs(img_after_fft2)), [-1 5])
%% DFT of cross
img_cross = zeros(img_h, img_w);
% create a cross in the matrix 
hemi_w = 2; hemi_l = min(img_w / 10, img_h / 10);
x11 = xOfCenter - hemi_l; x12 = xOfCenter + hemi_l;
y11 = yOfCenter - hemi_w; y12 = yOfCenter + hemi_w;
x21 = xOfCenter - hemi_w; x22 = xOfCenter + hemi_w;
y21 = yOfCenter - hemi_l; y22 = yOfCenter + hemi_l;
img_cross(y11:y12, x11:x12) = 1;
img_cross(y21:y22, x21:x22) = 1;
% Bilinear interpolation
img_cross = imrotate(img_cross, 45, 'bilinear','crop'); 
img_cross = imcomplement(img_cross);
% DFT using fft2 function
img_after_fft2 = fftshift(fft2(img_cross));
figure, imshow(img_cross)
figure, imshow(log(abs(img_after_fft2)), [-1 5])

下面的代码:

close all, clear, clc
warning off all
img_w = 640; img_h = img_w;
xOfCenter = img_w / 2; yOfCenter = img_h / 2;
%% DFT of oblique rectangle
img_oblique_rect = zeros(img_h, img_w);
% create a oblique(45) rectangle in the matrix
x1 = int32(img_w / 5 * 2); x2 = int32(img_w / 5 * 3);
y1 = int32(img_h / 7);     y2 = int32(img_h / 7 * 6);
img_oblique_rect(y1:y2, x1:x2) = 1;
% Bilinear interpolation
img_oblique_rect = imrotate(img_oblique_rect, 45, 'bilinear','crop'); 
img_oblique_rect = imcomplement(img_oblique_rect);
% DFT using fft2 function
img_after_fft2   = fftshift(fft2(img_oblique_rect));
figure, imshow(img_oblique_rect)
figure, imshow(log(abs(img_after_fft2)), [-1 5])
%% DFT of circle
img_circle = ones(img_h, img_w);
% create a circle in the matrix
[f1, f2] = freqspace(min(img_w, img_h), 'meshgrid');
r = sqrt(f1 .^ 2 + f2 .^ 2);
img_circle( r < -0.3 | r > 0.3) = 0;
img_circle = imcomplement(img_circle);
% DFT using fft2 function
img_after_fft2 = fftshift(fft2(img_circle));
figure, imshow(img_circle)
figure, imshow(log(abs(img_after_fft2)), [-1 5])
%% DFT of cross
img_cross = zeros(img_h, img_w);
% create a cross in the matrix 
hemi_w = 2; hemi_l = min(img_w / 10, img_h / 10);
x11 = xOfCenter - hemi_l; x12 = xOfCenter + hemi_l;
y11 = yOfCenter - hemi_w; y12 = yOfCenter + hemi_w;
x21 = xOfCenter - hemi_w; x22 = xOfCenter + hemi_w;
y21 = yOfCenter - hemi_l; y22 = yOfCenter + hemi_l;
img_cross(y11:y12, x11:x12) = 1;
img_cross(y21:y22, x21:x22) = 1;
% Bilinear interpolation
img_cross = imrotate(img_cross, 45, 'bilinear','crop'); 
img_cross = imcomplement(img_cross);
% DFT using fft2 function
img_after_fft2 = fftshift(fft2(img_cross));
figure, imshow(img_cross)
figure, imshow(log(abs(img_after_fft2)), [-1 5])


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值