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])