Breaking

Be Strong Let's Try!

Monday, 30 August 2021

How To Do Image Analysis in Frequency Domain |Digital Image Processing | Python Coding

Objective: 

The objective of this topic is to understand Fourier Transform, apply it on images and understand the results. 

Description:

 Fourier Series tells us that any function can be represented as a sum of sines/cosines of different frequencies multiplied by a different coefficient. Similarly, non periodic functions can also be represented as the integral of sines/cosines multiplied by weighing function. The Discrete Fourier Transform of f(x, y), for x = 0, 1, 2…M-1 and y = 0,1,2…N-1, denoted by F(u, v)

How To Do Image Analysis in Frequency Domain |Digital Image Processing | Python Coding



 for u = 0, 1, 2…M-1 and v = 0, 1, 2…N-1. It is really important to note that the Fourier transform is completely reversible .The inverse DFT is given by:

How To Do Image Analysis in Frequency Domain |Digital Image Processing | Python Coding

for x = 0, 1, 2…M-1 and y = 0, 1, 2…N-1

The filtering in frequency domain consists of following steps: 

 1. Compute F (u, v) the DFT of the image 

2. Multiply F (u, v) by a filter function H (u, v) Compute the inverse DFT of the result .

How To Do Image Analysis in Frequency Domain |Digital Image Processing | Python Coding

 

Some Useful Commands: 

1. To obtain the Fourier Transform of an image: my_transformed_image = numpy.fft.fft2(my_image) 

2. To obtain the Inverse Fourier Transform of an image: my_inverse_image = numpy.fft.ifft2(my_image) 

3. To shift the DC component of a Fourier Transformed Image to center: my_shifted_image = numpy.fft.fftshift(my_transformed_image) 

4. To shift the DC component back to the top left corner: my_inverse_shifted_image = np.fft.ifftshift(my_shifted_image) 

5. To calculate absolute of a value: my_absolute = numpy.abs(my_image) 

6. To calculate the exponential of an element: my_exponential = numpy.exp(my_input) 

7. To use the value of pi: numpy.pi 

8. To denote a complex number: simply put j after it e.g. -1j 

9. To multiply two matrices point by point: my_result = numpy.multiply(my_image, my_filter) 

 10. To take log transformation of the image: my_result = numpy.log(my_image) 11. To use math function: import math, x = math.sqrt(25)

Steps: 

1. Take Fourier Transform of the image 

2. Shift its Dc component to center 

3. Obtain absolute values of the output 

4. Brighten up the image using log transformation 

5. Normalize the image to obtain High Contrast (min, max) ->(0, 255) 

Some of the practical example will help us to understand this topic:

1: Use the following figures to compute the FFT and display the results.


How To Do Image Analysis in Frequency Domain |Digital Image Processing | Python Coding


How To Do Image Analysis in Frequency Domain |Digital Image Processing | Python Coding
With the help of these two fig1 and fig 2 :

Code

import cv2 as cv
import numpy as np
img = cv.imread(‘
Freestudystrom.tif',0) # read input image
ret,img = cv.threshold(img,127,255,cv.THRESH_BINARY)    #binary image
dims = np.shape(img)    #image dimensions
rows = dims[0]          #rows of image
cols = dims[1]          #columns of image
FFT_img = np.fft.fft2(img)              #FFT of image
FFT_shift = np.fft.fftshift(FFT_img)    #Shift the centre
FFT_shift_abs = np.abs(FFT_shift)       #Take absolute of pixel values
normalizedImg = np.zeros((rows,cols),dtype=np.float32) #initialize output image
normalizedImg = cv.normalize(FFT_shift_abs,
0, 255, cv.NORM_MINMAX)    #normalize the image
cv.imshow('Output Image ', normalizedImg)         #show output image
cv.waitKey(0)

Output

How To Do Image Analysis in Frequency Domain |Digital Image Processing | Python Coding

01(b)

Code

import cv2 as cv
import numpy as np
img = cv.imread(
'Freestudystrom.tif',0) # read input image
dims = np.shape(img)    #image dimensions
rows = dims[0]          #rows of image
cols = dims[1]          #columns of image
FFT_img = np.fft.fft2(img)              #FFT of image
FFT_shift = np.fft.fftshift(FFT_img)    #Shift the centre
FFT_shift_abs = np.abs(FFT_shift)       #Take absolute of pixel values

normalizedImg = np.zeros((rows,cols),dtype=np.float32)    #initialize output image
normalizedImg = cv.normalize(FFT_shift_abs,
0, 255, cv.NORM_MINMAX)    #normalize the image
cv.imshow('Output Image ', normalizedImg)         #show output image
cv.waitKey(0)

                                             Output:


2:
Apply smoothing effect on the given image (Fig 3) using Fourier transform technique. You will apply rectangular shape low pass filter with cut off frequency 30 on given image. Display input image, its magnitude spectrum and output image.


Code

import cv2 as cv
import numpy as np
def LPF(img,c):
    cutoff =c
    dims = np.shape(img) 
# image dimensions
   
rows = dims[0# rows of image
   
cols = dims[1# columns of image
   
Map = np.zeros((rows,cols),dtype=np.int16);
    R_center = np.int(rows/
2);
    C_center = np.int(cols/
2);
   
print(R_center,C_center)
   
for i in range(R_center-cutoff,C_center+cutoff):
       
for j in range(R_center-cutoff,C_center+cutoff):
            Map[i][j] =
1;
   
return Map
img = cv.imread(
Freestudystrom.tif',0) # read input image
dims = np.shape(img)    #image dimensions
rows = dims[0]          #rows of image
cols = dims[1]          #columns of image
print(dims)
FFT_img = np.fft.fft2(img)             
#FFT of image
FFT_shift = np.fft.fftshift(FFT_img)    #Shift the centre
FFT_shift_abs = np.abs(FFT_shift)       #Take absolute of pixel values
normalizedImg = np.zeros((rows,cols),dtype=np.float32)
normalizedImg = cv.normalize(FFT_shift_abs,
0, 255, cv.NORM_MINMAX)    #normalize the image
Mapp = LPF(normalizedImg,30)       #LOW PASS FILTER
Product = Mapp * normalizedImg;   #MULTIPLY FFT WITH FILTER
IFFT_shift = np.fft.ifftshift(Product);     #SHIFT CENTRES TO CORNERS
IFFT_img = np.fft.ifft2(IFFT_shift);        #INVERSE FOURIER
Final = np.zeros((rows,cols),dtype=np.float32)
Final = np.abs(IFFT_img);                  
#ABSOLUTE OF PIXELS
cv.imshow('Original Image ', img)
cv.imshow(
'FFT of Image', normalizedImg)
cv.imshow(
'Smooth Image ', Final)         #show output image
cv.waitKey(0)

 Output:



3: Find magnitude gradient of image by applying high pass filter of rectangular shape with cut off frequency 30. Display input image its magnitude spectrum and magnitude gradient.


Code

import cv2 as cv
import numpy as np
def HPF(img,c):
    cutoff =c
    dims = np.shape(img) 
# image dimensions
   
rows = dims[0# rows of image
   
cols = dims[1# columns of image
   
Map = np.ones((rows,cols),dtype=np.int16);
    R_center = np.int(rows/
2);
    C_center = np.int(cols/
2);
   
print(R_center,C_center)
   
for i in range(R_center-cutoff,C_center+cutoff):
       
for j in range(R_center-cutoff,C_center+cutoff):
            Map[i][j] =
0;
   
return Map
img = cv.imread(‘
Freestudystrom.tif',0) # read input image
dims = np.shape(img)    #image dimensions
rows = dims[0]          #rows of image
cols = dims[1]          #columns of image
print(dims)
FFT_img = np.fft.fft2(img)             
#FFT of image
FFT_shift = np.fft.fftshift(FFT_img)    #Shift the centre
FFT_shift_abs = np.abs(FFT_shift)       #Take absolute of pixel values
normalizedImg = np.zeros((rows,cols),dtype=np.float32)
normalizedImg = cv.normalize(FFT_shift_abs,
0, 255, cv.NORM_MINMAX)    #normalize the image
Mapp = HPF(normalizedImg,30)       #HIGH PASS FILTER
Product = Mapp * normalizedImg;   #MULTIPLY FFT WITH FILTER
IFFT_shift = np.fft.ifftshift(Product);     #SHIFT CENTRES TO CORNERS
IFFT_img = np.fft.ifft2(IFFT_shift);        #INVERSE FOURIER
Final = np.zeros((rows,cols),dtype=np.float32)
Final = np.abs(IFFT_img);                  
#ABSOLUTE OF PIXELS
cv.imshow('Original Image ', img)
cv.imshow(
'FFT of Image', normalizedImg)
cv.imshow(
'Sharp Image ', Final)         #show output image
cv.waitKey(0)

 Output:




No comments:

Post a Comment

Pages