Professional Documents
Culture Documents
Punto1 2
Punto1 2
#importing packages
import numpy as np
import cv2
from matplotlib import pyplot as plt
# Transform the image to improve the speed in the fourier transform calculation
rows, cols = img.shape
optimalRows = cv2.getOptimalDFTSize(rows)
optimalCols = cv2.getOptimalDFTSize(cols)
optimalImg = np.zeros((optimalRows, optimalCols))
optimalImg[:rows, :cols] = img
# output of CV2.dft() function will be 3-D numpy arry, for 2-D Output, 2D DFT as
two part complex and real part.
f_complex = dft_shift[:,:,0] + 1j*dft_shift[:,:,1]
f_abs = np.abs(f_complex) + 1 # lie between 1 and 1e6
f_bounded = 20 * np.log(f_abs)
f_img = 255 * f_bounded / np.max(f_bounded)
f_img = f_img.astype(np.uint8)
# #Dispaying input image, Gray Scale image, DFT of the Input Image
images = [optimalImg, f_img, result]
imageTitles = ['imagen', ' DFT ', ' image Reconstruida ']
for i in range(len(images)):
plt.subplot(1, 3, i + 1)
plt.imshow(images[i], cmap='gray')
plt.title(imageTitles[i])
plt.xticks([])
plt.yticks([])
plt.show()
#1.2
im = rgb2gray(imread('image.jpg'))
#im = imread('image.jpg')
kernel = np.outer(signal.gaussian(im.shape[0], 10), signal.gaussian(im.shape[1],
10))
freq = fp.fft2(im)
assert(freq.shape == kernel.shape)
freq_kernel = fp.fft2(fp.ifftshift(kernel))
convolved = freq*freq_kernel # by the Convolution theorem
im_blur = fp.ifft2(convolved).real
im_blur = 255 * im_blur / np.max(im_blur)
plt.figure(figsize=(25,25))
plt.imshow(im, cmap='gray')
plt.show()