I need help explaining this MATLAB code in detail please. %---------------------
ID: 2266571 • Letter: I
Question
I need help explaining this MATLAB code in detail please.
%--------------------------------------------------------------------------
% 6.1 Frequency Domain Imaging
%--------------------------------------------------------------------------
function bell_lab6_solution_6_1()
disp(' ');
disp(' --- Lab 6.1 -------------------------------');
% -------------- Part 1 --------------
n = 256; n2 = fix(n/2 + 1);
img_sd = complex(zeros(n,n),10*eps*ones(n,n));
nfr = 11;
nfc = 11;
nfr_lo = n2 - fix((nfr-1)/2);
nfr_hi = n2 + fix((nfr-1)/2);
nfc_lo = n2 - fix((nfc-1)/2);
nfc_hi = n2 + fix((nfc-1)/2);
img_sd(nfr_lo:nfr_hi,nfc_lo:nfc_hi) = 1.0;
img_sfd1 = sd_to_sfd(img_sd, 'corner');
img_sfd2 = sd_to_sfd(img_sd, 'center');
figure('Name','6.1: Frequency Domain Imaging')
subplot(2,2,1), imagesc(abs(img_sd)); axis image; title('6.1: SD Image');
subplot(2,2,2), imagesc(log(1+abs(img_sfd1))); axis image; title('6.1: SFD Image, corner');
subplot(2,2,3), imagesc(log(1+abs(img_sfd2))); axis image; title('6.1: SFD Image, center, shift');
subplot(2,2,4), image(angle(img_sfd2)); axis image; title('6.1: SFD Image, center, phase');
colormap gray(256);
img_sd1 = sfd_to_sd(img_sfd1, 'corner');
img_sd2 = sfd_to_sd(img_sfd2, 'center');
err1 = img_sd1 - img_sd;
err2 = img_sd2 - img_sd;
disp(max(abs(err1(:))));
disp(max(abs(err2(:))));
% -------------- Part 2 --------------
n = 256; n2 = fix(n/2 + 1);
img_sd = complex(zeros(n,n),10*eps*ones(n,n));
nfr = 11;
nfc = 51;
nfr_lo = n2 - fix((nfr-1)/2);
nfr_hi = n2 + fix((nfr-1)/2);
nfc_lo = n2 - fix((nfc-1)/2);
nfc_hi = n2 + fix((nfc-1)/2);
img_sd(nfr_lo:nfr_hi,nfc_lo:nfc_hi) = 1.0;
img_sfd1 = sd_to_sfd(img_sd, 'corner');
img_sfd2 = sd_to_sfd(img_sd, 'center');
figure('Name','6.1: Frequency Domain Imaging')
subplot(2,2,1), imagesc(abs(img_sd)); axis image; title('6.1: SD Image');
subplot(2,2,2), imagesc(log(1+abs(img_sfd1))); axis image; title('6.1: SFD Image, corner');
subplot(2,2,3), imagesc(log(1+abs(img_sfd2))); axis image; title('6.1: SFD Image, center, shift');
subplot(2,2,4), image(angle(img_sfd2)); axis image; title('6.1: SFD Image, center, phase');
colormap gray(256);
img_sd1 = sfd_to_sd(img_sfd1, 'corner');
img_sd2 = sfd_to_sd(img_sfd2, 'center');
err1 = img_sd1 - img_sd;
err2 = img_sd2 - img_sd;
disp(max(abs(err1(:))));
disp(max(abs(err2(:))));
% -------------- Part 3 --------------
n = 256; n2 = fix(n/2 + 1);
img_sd = complex(zeros(n,n),10*eps*ones(n,n));
nfr = 11;
nfc = 51;
nfr_lo = 0.75*n - fix((nfr-1)/2);
nfr_hi = 0.75*n + fix((nfr-1)/2);
nfc_lo = 0.75*n - fix((nfc-1)/2);
nfc_hi = 0.75*n + fix((nfc-1)/2);
img_sd(nfr_lo:nfr_hi,nfc_lo:nfc_hi) = 1.0;
img_sfd1 = sd_to_sfd(img_sd, 'corner');
img_sfd2 = sd_to_sfd(img_sd, 'center');
figure('Name','6.1: Frequency Domain Imaging')
subplot(2,2,1), imagesc(abs(img_sd)); axis image; title('6.1: SD Image');
subplot(2,2,2), imagesc(log(1+abs(img_sfd1))); axis image; title('6.1: SFD Image, corner');
subplot(2,2,3), imagesc(log(1+abs(img_sfd2))); axis image; title('6.1: SFD Image, center, shift');
subplot(2,2,4), image(angle(img_sfd2)); axis image; title('6.1: SFD Image, center, phase');
colormap gray(256);
img_sd1 = sfd_to_sd(img_sfd1, 'corner');
img_sd2 = sfd_to_sd(img_sfd2, 'center');
err1 = img_sd1 - img_sd;
err2 = img_sd2 - img_sd;
disp(max(abs(err1(:))));
disp(max(abs(err2(:))));
% -------------- Part 4 --------------
n = 256; n2 = fix(n/2 + 1);
img_sd = complex(zeros(n,n),10*eps*ones(n,n));
radius = 15;
r2 = radius^2;
for r=1:size(img_sd,1)
for c=1:size(img_sd,2)
dist2 = (r-n2)^2 + (c-n2)^2;
if (dist2 < r2)
img_sd(r,c) = complex(1.0,0.0);
end
end
end
img_sfd1 = sd_to_sfd(img_sd, 'corner');
img_sfd2 = sd_to_sfd(img_sd, 'center');
figure('Name','6.1: Frequency Domain Imaging')
subplot(2,2,1), imagesc(abs(img_sd)); axis image; title('6.1: SD Image');
subplot(2,2,2), imagesc(log(1+abs(img_sfd1))); axis image; title('6.1: SFD Image, corner');
subplot(2,2,3), imagesc(log(1+abs(img_sfd2))); axis image; title('6.1: SFD Image, center, shift');
subplot(2,2,4), image(angle(img_sfd2)); axis image; title('6.1: SFD Image, center, phase');
colormap gray(256);
img_sd1 = sfd_to_sd(img_sfd1, 'corner');
img_sd2 = sfd_to_sd(img_sfd2, 'center');
err1 = img_sd1 - img_sd;
err2 = img_sd2 - img_sd;
disp(max(abs(err1(:))));
disp(max(abs(err2(:))));
% -------------- Part 5 --------------
n = 256; n2 = fix(n/2 + 1);
img_sd = complex(zeros(n,n),10*eps*ones(n,n));
nfr = 21;
nfc = 51;
nfr_lo = n2 - fix((nfr-1)/2);
nfr_hi = n2 + fix((nfr-1)/2);
nfc_lo = n2 - fix((nfc-1)/2);
nfc_hi = n2 + fix((nfc-1)/2);
img_sd(nfr_lo:nfr_hi,nfc_lo:nfc_hi) = 1.0;
img_sd = image_rotate(img_sd, 30, 'bilinear');
img_sfd1 = sd_to_sfd(img_sd, 'corner');
img_sfd2 = sd_to_sfd(img_sd, 'center');
figure('Name','6.1: Frequency Domain Imaging')
subplot(2,2,1), imagesc(abs(img_sd)); axis image; title('6.1: SD Image');
subplot(2,2,2), imagesc(log(1+abs(img_sfd1))); axis image; title('6.1: SFD Image, corner');
subplot(2,2,3), imagesc(log(1+abs(img_sfd2))); axis image; title('6.1: SFD Image, center, shift');
subplot(2,2,4), image(angle(img_sfd2)); axis image; title('6.1: SFD Image, center, phase');
colormap gray(256);
img_sd1 = sfd_to_sd(img_sfd1, 'corner');
img_sd2 = sfd_to_sd(img_sfd2, 'center');
err1 = img_sd1 - double(img_sd);
err2 = img_sd2 - double(img_sd);
disp(max(abs(err1(:))));
disp(max(abs(err2(:))));
% -------------- Part 6 --------------
% img_sd = imread('cameraman.tif');
img_sd = imread('blown_ic.tif');
% img_sd = imread('test_pattern.tif');
img_sfd1 = sd_to_sfd(img_sd, 'corner');
img_sfd2 = sd_to_sfd(img_sd, 'center');
figure('Name','6.1: Frequency Domain Imaging')
subplot(2,2,1), imagesc(abs(img_sd)); axis image; title('6.1: SD Image');
subplot(2,2,2), imagesc(log(1+abs(img_sfd1))); axis image; title('6.1: SFD Image, corner');
subplot(2,2,3), imagesc(log(1+abs(img_sfd2))); axis image; title('6.1: SFD Image, center, shift');
subplot(2,2,4), image(angle(img_sfd2)); axis image; title('6.1: SFD Image, center, phase');
colormap gray(256);
img_sd1 = sfd_to_sd(img_sfd1, 'corner');
img_sd2 = sfd_to_sd(img_sfd2, 'center');
err1 = img_sd1 - double(img_sd);
err2 = img_sd2 - double(img_sd);
disp(max(abs(err1(:))));
disp(max(abs(err2(:))));
avg_val = mean(img_sd(:));
dc_val = img_sfd1(1,1) / (size(img_sd,1)*size(img_sd,2));
disp([' Avg. value = ' num2str(avg_val)]);
disp([' DC value = ' num2str(dc_val)]);
end
This was the question asked of us:
Frequency Domain Imaging
Write two Matlab functions that perform Fourier transformation of an image going from spatial domain to spatial frequency domain (SD-to-SFD) and the inverse Fourier transformation of an image going from spatial frequency domain to spatial domain transformation (SFD-to-SD). You may use the Matlab functions, fft2(.) and ifft2(.). Give your function the option of placing the DC component of the image at (0,0) or (N/2+1, N/2+1). Use the Matlab function fftshift(.) and ifftshift(.) to shift the DC point.
Test your transformation function by processing a:
256x256 image with an 11x11 square located at the image center,
256x256 image with an 11x51 rectangle located at the image center,
256x256 image with an 11x51 rectangle located at the ¾ N,
256x256 image with a circle of radius 15 located at the image center,
256x256 image with an 11x51 rectangle rotated counterclockwise by 30 degrees.
The grayscale images, ‘cameraman.tif’, ‘blown_ic.tif’, and ‘test_pattern.tif’. Note and comment on the differences in the Fourier Spectrum.
Display the magnitude and phase of the transformed image and ensure that you are able to recover your original input. Demonstrate each optional input on the 1st test image. Let the input image be the real portion of the image and the imaginary portion of the image be 0 or eps.
Calculate the average value of ‘cameraman.tif’. Take it’s DFT and divide the DC-point pixel value by the number of pixels in the image. Explain what each value represents and their relation to one another.
Explanation / Answer
In the given code,
Each of the subsections labelled as follows
% -------------- Part X -------------- (X = 1,2,3,4,5,6)
performs the fourier transformation of the corresponding spatial domain image and the subsequent inverse transformation from frequency domain to spatial domain as asked in the problem statement.
In Part 1 to 5,
n = 256; n2 = fix(n/2 + 1);
img_sd = complex(zeros(n,n),10*eps*ones(n,n));
A complex matrix (img_sd) is defined of size 256*256 and each element is initialised to values = ( 0 + i*0 )
img_sd(nfr_lo:nfr_hi,nfc_lo:nfc_hi) = 1.0;
Then, as per the problem asks, the proper elements in the matrix are assigned values of 1.0 to generate the rectangle of appropriate size at proper location.
nfr_lo and nfr_hi are appropriate variables to define the rectangles.
img_sfd1 = sd_to_sfd(img_sd, 'corner');
img_sfd2 = sd_to_sfd(img_sd, 'center');
sd_to_sfd are functions used to determine the fourier transform of the images. However, the sd_to_sfd has not been explicitly defined. In the first case, the transform is done on the original image while in the second case it is done on the center-shifted image.
figure('Name','6.1: Frequency Domain Imaging')
subplot(2,2,1), imagesc(abs(img_sd)); axis image; title('6.1: SD Image');
subplot(2,2,2), imagesc(log(1+abs(img_sfd1))); axis image; title('6.1: SFD Image, corner');
subplot(2,2,3), imagesc(log(1+abs(img_sfd2))); axis image; title('6.1: SFD Image, center, shift');
subplot(2,2,4), image(angle(img_sfd2)); axis image; title('6.1: SFD Image, center, phase');
The above code lines plots the absolute values of the spatial image as well as the transformed image for both original as well as center shifted image.
img_sd1 = sfd_to_sd(img_sfd1, 'corner');
img_sd2 = sfd_to_sd(img_sfd2, 'center');
In the above part, inverse transform of the images is done but again the corresponding function are not defined. In the first case, the inverse transform is done on the fourier transform of the original image while in the second case it is done on the fourier transform of the center-shifted image.
err1 = img_sd1 - img_sd;
err2 = img_sd2 - img_sd;
disp(max(abs(err1(:))));
disp(max(abs(err2(:))));
In this part, the difference between the original image and the recovered image through inverse transformation are calculated and stored in the err1 and err2 matrix respectively for the original image and center shifted image. The error is displayed using disp function.
In Part 6,
% img_sd = imread('cameraman.tif');
img_sd = imread('blown_ic.tif');
% img_sd = imread('test_pattern.tif');
The respective images of ‘cameraman.tiff’ , ‘blown_ic.tiff’, ‘test_pattern.tiff’ are read using imread function as shown above.
img_sfd1 = sd_to_sfd(img_sd, 'corner');
img_sfd2 = sd_to_sfd(img_sd, 'center');
sd_to_sfd are functions used to determine the fourier transform of the images. However, the sd_to_sfd has not been explicitly defined. In the first case, the transform is done on the original image while in the second case it is done on the center-shifted image.
figure('Name','6.1: Frequency Domain Imaging')
subplot(2,2,1), imagesc(abs(img_sd)); axis image; title('6.1: SD Image');
subplot(2,2,2), imagesc(log(1+abs(img_sfd1))); axis image; title('6.1: SFD Image, corner');
subplot(2,2,3), imagesc(log(1+abs(img_sfd2))); axis image; title('6.1: SFD Image, center, shift');
subplot(2,2,4), image(angle(img_sfd2)); axis image; title('6.1: SFD Image, center, phase');
The above code lines plots the absolute values of the spatial image as well as the transformed image for both original as well as center shifted image.
img_sd1 = sfd_to_sd(img_sfd1, 'corner');
img_sd2 = sfd_to_sd(img_sfd2, 'center');
In the above part, inverse transform of the images is done but again the corresponding function are not defined. In the first case, the inverse transform is done on the fourier transform of the original image while in the second case it is done on the fourier transform of the center-shifted image.
err1 = img_sd1 - img_sd;
err2 = img_sd2 - img_sd;
disp(max(abs(err1(:))));
disp(max(abs(err2(:))));
In this part, the difference between the original image and the recovered image through inverse transformation are calculated and stored in the err1 and err2 matrix respectively for the original image and center shifted image. The error is displayed using disp function.
avg_val = mean(img_sd(:));
dc_val = img_sfd1(1,1) / (size(img_sd,1)*size(img_sd,2));
disp([' Avg. value = ' num2str(avg_val)]);
disp([' DC value = ' num2str(dc_val)]);
Lastly, average value of the all the pixels from the original image is computed and compared with the DC value of the fourier transform.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.