Advertisement
akariya

[ECE558] Project02 problem 2 (dft2, idf2)

Oct 17th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. import cv2
  2. import argparse
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5. import ast
  6. import math
  7.  
  8. # function for dft2
  9. def dft2(img):
  10.     img = np.fft.fft(np.fft.fft(img,axis=0),axis=1)
  11.     return img
  12.  
  13. # function for idft2
  14. def idft2(fft):
  15.     y, x = fft.shape
  16.     return dft2(np.conjugate(fft))/(x*y)
  17.  
  18. # driver code
  19. parser = argparse.ArgumentParser(description='2D convolution')
  20. parser.add_argument('-i', action='store', dest='img_path',help='Enter image path (relative path)')
  21. args = parser.parse_args()
  22.  
  23. img_path = args.img_path
  24. img = cv2.imread(img_path)
  25. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  26.  
  27. # normalize input image
  28. y, x = img.shape
  29. modified = np.zeros([y,x])
  30. i = 0
  31. maxF = img.max()
  32. minF = img.min()
  33. # apply transformation
  34. while(i < y):
  35.     j = 0
  36.     while(j < x):
  37.         modified[i][j] = ((img[i][j]-minF)/(maxF-minF));
  38.         j += 1
  39.     i += 1
  40.  
  41. img = dft2(modified)
  42.  
  43. # display phase and magnitude spectrum
  44. fshift = np.fft.fftshift(img)
  45. magnitude_spectrum = 20*np.log(np.abs(fshift))
  46. phase =  np.angle(fshift)
  47. imgishift = np.fft.ifftshift(fshift)
  48. img = idft2(imgishift*255)
  49. cv2.imshow('Result Spectrum',np.uint8(magnitude_spectrum))
  50. cv2.imshow('Result Phase',np.uint8(phase))
  51. cv2.waitKey(0)
  52. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement