Monday, April 20, 2015

2D FFT: Effects of Location & Thickness of Horizontal Lines on 2D FFT Magnitude & Phase: Part 01


Problem
  
This post begins a series of posts on a programmatic investigation of the effects of the location and thickness of horizontal, vertical, and diagonal lines on the magnitude and phase components of 2D FFT. In this post, we will discuss horizontal lines. We will assume, for the sake of simplicity, that the file is saved in C:\Users\Vladimir\audio_files. The next post is here.

Central Horizontal Line



We start with the image in Figure 1 that shows a horizontal line centrally positioned in the image.

Figure 1. Centrally positioned horizontal line (hor_line_01a.jpg)

We add the path, read the image, and display it. 
 
addpath C:\Users\Vladimir\research\images\
ImgHorLine_01a = imread('hor_line_01a', 'jpg');

figure;
imshow(ImgHorLine_01a);
title('HOR LINE 01a');


Here is how we compute the 2D FFT of the image. The image is converted into a 2D double matrix. The fftshift command positions the 0,0 origin to the center of the image. We compute the absolute magnitude of each value in the 2D matrix and take the log of each value. One is added to each value before taking the log, because log(0) is not defined.
 
%% ff2 magnitude
fftHorLine01a = fft2(double(ImgHorLine_01a));
fftHorLine01a = fftshift(fftHorLine01a);
fftHorLine01a = abs(fftHorLine01a);
fftHorLine01a = log(fftHorLine01a+1);
fftHorLine01a = mat2gray(fftHorLine01a);


The magnitude image is shown in Figure 2. As can be seen in Figure 2, the FFT magnitude shows a line orthogonal to the line in Figure 1.

figure;
imshow(fftHorLine01a);
colormap(gray);
title('HOR LINE 01a FFT2 Magnitude');


Figure 2. 2D FFT2 Magnitude of Figure 1

We compute the phase component and display it. The image is shown in Figure 3.

%% fft2 phase
fftHorLine01a_PHASE = fft2(double(ImgHorLine_01a));
fftHorLine01a_PHASE = fftshift(fftHorLine01a_PHASE);
fftHorLine01a_PHASE = angle(fftHorLine01a_PHASE);

 
figure;
imshow(fftHorLine01a_PHASE, [-pi, pi]);
colormap(gray);
title('HOR LINE 01a FFT2 Phase');


Figure 3. 2D FFT Phase of Figure 1


Horizontal Line Shifted Upward

The horizontal line is shifted upward without modifying its thickness. Figure 4 shows an image with a horizontal line shifted upward.

Figure 4. Horizontal line shifted upward (hor_line_01b.jpg)

We load this image, display it, and compute the 2D FFT magnitude and phase.

ImgHorLine_01b = imread('hor_line_01b', 'jpg');

figure;
imshow(ImgHorLine_01b);
title('HOR LINE 01b');

%% ff2 magnitude
fftHorLine01b = fft2(double(ImgHorLine_01b));
fftHorLine01b = fftshift(fftHorLine01b);
fftHorLine01b = abs(fftHorLine01b);
fftHorLine01b = log(fftHorLine01b+1);
fftHorLine01b = mat2gray(fftHorLine01b);

figure;
imshow(fftHorLine01b), colormap(gray);
title('HOR LINE 01b FFT2 Magnitude');

%% fft2 phase
fftHorLine01b_PHASE = fft2(double(ImgHorLine_01b));
fftHorLine01b_PHASE = fftshift(fftHorLine01b_PHASE);
fftHorLine01b_PHASE = angle(fftHorLine01b_PHASE);

figure;
imshow(fftHorLine01b_PHASE, [-pi, pi]), colormap(gray);
title('HOR LINE 01b FFT2 Phase');

Figure 5 shows the 2D FFT magnitude. The figure with the phase component is omitted for the sake of space but can be reproduced by running the MATLAB code below. The magnitude component is the same as for the centrally positioned horizontal line.


Figure 5. 2D FFT magnitude of Figure 4
The MATLAB source is given below. We will continue in the next post with shifting the horizontal line downward and changing its thickness. The next post is here.

MATLAB Source


%%% ==================================================================
%%% Programmatic Investigation of the Effects of Location and Thickness
%%% of horizontal lines on the magnitude and phase components of 2D FFT.
%%%
%%% Author: Vladimir Kulyukin
%%% ==================================================================


addpath C:\Users\Vladimir\research\images\

%%% ================== 2D FFT of HOR LINE 01a ============================

ImgHorLine_01a = imread('hor_line_01a', 'jpg');

figure;
imshow(ImgHorLine_01a);
title('HOR LINE 01a');

%% ff2 magnitude
fftHorLine01a = fft2(double(ImgHorLine_01a));
fftHorLine01a = fftshift(fftHorLine01a);
fftHorLine01a = abs(fftHorLine01a);
fftHorLine01a = log(fftHorLine01a+1);
fftHorLine01a = mat2gray(fftHorLine01a);

figure;
imshow(fftHorLine01a);
colormap(gray);
title('HOR LINE 01a FFT2 Magnitude');

%% fft2 phase
fftHorLine01a_PHASE = fft2(double(ImgHorLine_01a));
fftHorLine01a_PHASE = fftshift(fftHorLine01a_PHASE);
fftHorLine01a_PHASE = angle(fftHorLine01a_PHASE);

figure;
imshow(fftHorLine01a_PHASE, [-pi, pi]);
colormap(gray);
title('HOR LINE 01a FFT2 Phase');

%%% ================== 2D FFT of HOR LINE 01b ============================
ImgHorLine_01b = imread('hor_line_01b', 'jpg');

figure;
imshow(ImgHorLine_01b);
title('HOR LINE 01b');

%% ff2 magnitude
fftHorLine01b = fft2(double(ImgHorLine_01b));
fftHorLine01b = fftshift(fftHorLine01b);
fftHorLine01b = abs(fftHorLine01b);
fftHorLine01b = log(fftHorLine01b+1);
fftHorLine01b = mat2gray(fftHorLine01b);

figure;
imshow(fftHorLine01b), colormap(gray);
title('HOR LINE 01b FFT2 Magnitude');

%% fft2 phase
fftHorLine01b_PHASE = fft2(double(ImgHorLine_01b));
fftHorLine01b_PHASE = fftshift(fftHorLine01b_PHASE);
fftHorLine01b_PHASE = angle(fftHorLine01b_PHASE);

figure;
imshow(fftHorLine01b_PHASE, [-pi, pi]), colormap(gray);
title('HOR LINE 01b FFT2 Phase');

%%% ================== 2D FFT of HOR LINE 01c ============================

img_hor_line_01c = imread('hor_line_01c', 'jpg');

figure;
imshow(img_hor_line_01c);
title('HOR LINE 01c');

%% ff2 magnitude
fftHorLine01c = fft2(double(img_hor_line_01c));
fftHorLine01c = fftshift(fftHorLine01c);
fftHorLine01c = abs(fftHorLine01c);
fftHorLine01c = log(fftHorLine01c+1);
fftHorLine01c = mat2gray(fftHorLine01c);

figure, imshow(fftHorLine01c), colormap(gray);
title('HOR LINE 01c FFT2 Magnitude');

%% fft2 phase
fftHorLine01c_PHASE = fft2(double(img_hor_line_01c));
fftHorLine01c_PHASE = fftshift(fftHorLine01c_PHASE);
fftHorLine01c_PHASE = angle(fftHorLine01c_PHASE);

figure;
imshow(fftHorLine01c_PHASE, [-pi, pi]), colormap(gray);
title('HOR LINE 01c FFT2 Phase');

%%% ================== 2D FFT of HOR LINE 01d ============================
img_hor_line_01d = imread('hor_line_01d', 'jpg');

figure;
imshow(img_hor_line_01d);
title('HOR LINE 01d');

%% ff2 magnitude
fftHorLine01d = fft2(double(img_hor_line_01d));
fftHorLine01d = fftshift(fftHorLine01d);
fftHorLine01d = abs(fftHorLine01d);
fftHorLine01d = log(fftHorLine01d+1);
fftHorLine01d = mat2gray(fftHorLine01d);

figure;
imshow(fftHorLine01d), colormap(gray);
title('HOR LINE 01d FFT2 Magnitude');

%% fft2 phase
fftHorLine01d_PHASE = fft2(double(img_hor_line_01d));
fftHorLine01d_PHASE = fftshift(fftHorLine01d_PHASE);
fftHorLine01d_PHASE = angle(fftHorLine01d_PHASE);

figure;
imshow(fftHorLine01d_PHASE, [-pi, pi]), colormap(gray);
title('HOR LINE 01d FFT2 Phase');

%%% ================== 2D FFT of HOR LINE 01e ============================

img_hor_line_01e = imread('hor_line_01e', 'jpg');

figure;
imshow(img_hor_line_01e);
title('HOR LINE 01e');

%% ff2 magnitude
fftHorLine01e = fft2(double(img_hor_line_01e));
fftHorLine01e = fftshift(fftHorLine01e);
fftHorLine01e = abs(fftHorLine01e);
fftHorLine01e = log(fftHorLine01e+1);
fftHorLine01e = mat2gray(fftHorLine01e);

figure, imshow(fftHorLine01e), colormap(gray);
title('HOR LINE 01e FFT2 Magnitude');

%% fft2 phase
fftHorLine01e_PHASE = fft2(double(img_hor_line_01e));
fftHorLine01e_PHASE = fftshift(fftHorLine01e_PHASE);
fftHorLine01e_PHASE = angle(fftHorLine01e_PHASE);

figure;
imshow(fftHorLine01e_PHASE, [-pi, pi]), colormap(gray);
title('HOR LINE 01e FFT2 Phase');

%%% ================== 2D FFT of HOR LINE 01f ============================
img_hor_line_01f = imread('hor_line_01f', 'jpg');

figure;
imshow(img_hor_line_01f);
title('HOR LINE 01f');

%% ff2 magnitude
fftHorLine01f = fft2(double(img_hor_line_01f));
fftHorLine01f = fftshift(fftHorLine01f);
fftHorLine01f = abs(fftHorLine01f);
fftHorLine01f = log(fftHorLine01f+1);
fftHorLine01f = mat2gray(fftHorLine01f);

figure;
imshow(fftHorLine01e);
colormap(gray);
title('HOR LINE 01f FFT2 Magnitude');

%% fft2 phase
fftHorLine01f_PHASE = fft2(double(img_hor_line_01f));
fftHorLine01f_PHASE = fftshift(fftHorLine01f_PHASE);
fftHorLine01f_PHASE = angle(fftHorLine01f_PHASE);

figure;
imshow(fftHorLine01f_PHASE, [-pi, pi]), colormap(gray);
title('HOR LINE 01f FFT2 Phase');

%%% ======================================================================