Advertisement
sajid006

DFT0335

Nov 30th, 2021
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Nov 30 12:02:58 2021
  4.  
  5. @author: Sajid
  6. """
  7.  
  8. import numpy as np
  9. import cv2
  10. from matplotlib import pyplot as plt
  11.  
  12. def HighPassFilter(img,radius):
  13. # Circular HPF mask, center circle is 0, remaining all ones
  14. #Can be used for edge detection because low frequencies at center are blocked
  15. #and only high frequencies are allowed. Edges are high frequency components.
  16. #Amplifies noise.
  17.  
  18. rows, cols = img.shape
  19. crow, ccol = int(rows / 2), int(cols / 2)
  20.  
  21. mask = np.ones((rows, cols, 2), np.uint8)
  22. r = radius #80
  23. center = [crow, ccol]
  24. x, y = np.ogrid[:rows, :cols]
  25. mask_area = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= r*r
  26. mask[mask_area] = 0
  27.  
  28. '''
  29. mask = np.ones((rows,cols,2),np.uint8)
  30. mask[crow-30:crow+30, ccol-30:ccol+30] = 0
  31. '''
  32.  
  33. return mask
  34.  
  35.  
  36. def LowPassFilter(img,radius):
  37. # Circular LPF mask, center circle is 1, remaining all zeros
  38. # Only allows low frequency components - smooth regions
  39. #Can smooth out noise but blurs edges.
  40. rows, cols = img.shape
  41. crow, ccol = int(rows / 2), int(cols / 2)
  42. mask = np.zeros((rows, cols, 2), np.uint8)
  43. r = radius #100
  44. center = [crow, ccol]
  45. x, y = np.ogrid[:rows, :cols]
  46. mask_area = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= r*r
  47. mask[mask_area] = 1
  48.  
  49. return mask
  50.  
  51.  
  52. def BandPassFilter(img,innerRadius,outerRadius):
  53. # Band Pass Filter - Concentric circle mask, only the points living in concentric circle are ones
  54. rows, cols = img.shape
  55. crow, ccol = int(rows / 2), int(cols / 2)
  56. mask = np.zeros((rows, cols, 2), np.uint8)
  57. r_out = outerRadius #80
  58. r_in = innerRadius #10
  59. center = [crow, ccol]
  60. x, y = np.ogrid[:rows, :cols]
  61. mask_area = np.logical_and(((x - center[0]) ** 2 + (y - center[1]) ** 2 >= r_in ** 2),
  62. ((x - center[0]) ** 2 + (y - center[1]) ** 2 <= r_out ** 2))
  63. mask[mask_area] = 1
  64.  
  65. return mask
  66.  
  67.  
  68.  
  69. img = cv2.imread('lena.png',cv2.IMREAD_GRAYSCALE)
  70. cv2.imshow('input image',img)
  71.  
  72. #create dft from input image
  73. dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
  74.  
  75. #Without shifting the data would be centered around origin at the top left
  76. #Shifting it moves the origin to the center of the image.
  77. dft_shift = np.fft.fftshift(dft)
  78.  
  79. #Calculate magnitude spectrum from the DFT (Real part and imaginary part)
  80. magnitude_spectrum = np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
  81.  
  82. #fourier spectrum
  83. img1 =cv2.normalize(magnitude_spectrum, magnitude_spectrum, 0,255, cv2.NORM_MINMAX, cv2.CV_8UC1)
  84. cv2.imshow('magnitude_spectrum',img1)
  85.  
  86. '''
  87. fig = plt.figure(figsize=(12, 12))
  88. ax1 = fig.add_subplot(2,2,2)
  89. ax1.imshow(img)
  90. ax1.title.set_text('Input Image')
  91. ax2 = fig.add_subplot(2,2,1)
  92. ax2.imshow(dft_shift)
  93. ax2.title.set_text('FFT of image')
  94. plt.show()
  95. phaseimage = np.log(cv2.phase(dft_shift[:,:,0],dft_shift[:,:,1]))
  96. img2 =cv2.normalize(phaseimage, phaseimage, 0,255, cv2.NORM_MINMAX, cv2.CV_8UC1)
  97. cv2.imshow('phase image',img2)
  98. '''
  99.  
  100. # apply mask and inverse DFT: Multiply fourier transformed image (values)
  101. #with the mask values.
  102.  
  103. #edge detection
  104. mask1 = HighPassFilter(img, 80)
  105.  
  106.  
  107. fshift1 = dft_shift * mask1
  108.  
  109. f_ishift1 = np.fft.ifftshift(fshift1)
  110. img_back1 = cv2.idft(f_ishift1)
  111.  
  112.  
  113. edgeimage = cv2.magnitude(img_back1[:,:,0],img_back1[:,:,1])
  114. img11 =cv2.normalize(edgeimage, edgeimage, 0,255, cv2.NORM_MINMAX, cv2.CV_8UC1)
  115. cv2.imshow('Edge detection(HPF)',img11)
  116.  
  117. '''
  118. ph1 = cv2.phase(img_back[:,:,0],img_back[:,:,1])
  119. img12 =cv2.normalize(ph1, ph1, 0,255, cv2.NORM_MINMAX, cv2.CV_8UC1)
  120. cv2.imshow('ph1',img12)
  121. '''
  122.  
  123. #smoothing
  124. mask2 = LowPassFilter(img, 100)
  125.  
  126.  
  127. fshift2 = dft_shift * mask2
  128.  
  129. f_ishift2 = np.fft.ifftshift(fshift2)
  130. img_back2 = cv2.idft(f_ishift2)
  131.  
  132.  
  133. smoothimage = cv2.magnitude(img_back2[:,:,0],img_back2[:,:,1])
  134. img2 =cv2.normalize(smoothimage, smoothimage, 0,255, cv2.NORM_MINMAX, cv2.CV_8UC1)
  135. cv2.imshow('Smoothing(LPF)',img2)
  136.  
  137.  
  138.  
  139. #BPF
  140. mask3 = BandPassFilter(img, 10, 80)
  141.  
  142.  
  143. fshift3 = dft_shift * mask3
  144.  
  145. f_ishift3 = np.fft.ifftshift(fshift3)
  146. img_back3 = cv2.idft(f_ishift3)
  147.  
  148.  
  149. bpfimage = cv2.magnitude(img_back3[:,:,0],img_back3[:,:,1])
  150. img3 =cv2.normalize(bpfimage, bpfimage, 0,255, cv2.NORM_MINMAX, cv2.CV_8UC1)
  151. cv2.imshow('Band Pass Filter',img3)
  152.  
  153.  
  154. cv2.waitKey(0)
  155. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement