Advertisement
sajid006

DFT1

Nov 30th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Nov 29 12:02:11 2021
  4.  
  5. @author: Sajid
  6. """
  7.  
  8.  
  9.  
  10. import cv2
  11. import numpy as np
  12. from matplotlib import pyplot as plt
  13.  
  14. def gauss_low_pass_filter(row,col,sigma):
  15.  
  16. row = int(row) // 2
  17. col = int(col) // 2
  18.  
  19. x, y = np.mgrid[-row:row, -col:col]
  20. normal = 1 / (2.0 * np.pi * sigma**2)
  21. g = np.exp(-((x**2 + y**2) / (2.0*sigma**2))) * normal
  22. factor = 255/ g.max()
  23. gaussian_image = g*factor
  24. print(gaussian_image)
  25. cv2.imshow('Gaussian Smoothed Image',gaussian_image)
  26. plt.plot(gaussian_image)
  27. return g
  28.  
  29. img = cv2.imread('download.jpg',0)
  30.  
  31.  
  32. f = np.fft.fft2(img)
  33. fshift = np.fft.fftshift(f)
  34. magnitude_spectrum = 20*np.log(np.abs(fshift))
  35.  
  36.  
  37. mag = np.abs(fshift)
  38.  
  39. print(mag)
  40. ang = np.angle(fshift)
  41.  
  42. sigma = float(input('Enter Cut Off: '))
  43. mask = gauss_low_pass_filter(img.shape[0],img.shape[1],sigma)
  44.  
  45. combined = np.multiply(mag*mask, np.exp(1j*ang))
  46.  
  47. imgCombined = np.real(np.fft.ifft2(np.fft.ifftshift(combined)))
  48.  
  49.  
  50. plt.subplot(121),plt.imshow(img, cmap = 'gray')
  51. plt.title('Input Image'), plt.xticks([]), plt.yticks([])
  52.  
  53. plt.subplot(122),plt.imshow(imgCombined, cmap = 'gray')
  54. plt.title('Output image'), plt.xticks([]), plt.yticks([])
  55.  
  56. #plt.imshow(imgCombined, cmap='gray')
  57.  
  58.  
  59. plt.show()
  60. cv2.waitKey(0)
  61. cv2.destroyAllWindows()
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement