Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import argparse
- import numpy as np
- from matplotlib import pyplot as plt
- import ast
- import math
- # function for dft2
- def dft2(img):
- img = np.fft.fft(np.fft.fft(img,axis=0),axis=1)
- return img
- # function for idft2
- def idft2(fft):
- y, x = fft.shape
- return dft2(np.conjugate(fft))/(x*y)
- # driver code
- parser = argparse.ArgumentParser(description='2D convolution')
- parser.add_argument('-i', action='store', dest='img_path',help='Enter image path (relative path)')
- args = parser.parse_args()
- img_path = args.img_path
- img = cv2.imread(img_path)
- img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- # normalize input image
- y, x = img.shape
- modified = np.zeros([y,x])
- i = 0
- maxF = img.max()
- minF = img.min()
- # apply transformation
- while(i < y):
- j = 0
- while(j < x):
- modified[i][j] = ((img[i][j]-minF)/(maxF-minF));
- j += 1
- i += 1
- img = dft2(modified)
- # display phase and magnitude spectrum
- fshift = np.fft.fftshift(img)
- magnitude_spectrum = 20*np.log(np.abs(fshift))
- phase = np.angle(fshift)
- imgishift = np.fft.ifftshift(fshift)
- img = idft2(imgishift*255)
- cv2.imshow('Result Spectrum',np.uint8(magnitude_spectrum))
- cv2.imshow('Result Phase',np.uint8(phase))
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement