-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
73 lines (61 loc) · 1.97 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import matplotlib.pyplot as plt
from skimage import img_as_float, io
'''
Changes the given Coour Image into GreyScale
'''
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
'''
Returns the Fourier Transform for the given Image
'''
def fourier(img):
gray_img = rgb2gray(img)
four = np.fft.fft2(gray_img)
shifted_fourier = np.fft.fftshift(four)
return shifted_fourier
'''
Plots the Fourier Transform Represented by the given Matrix
'''
def plotFourier(fourier):
psd2D = np.log(np.abs(fourier)**2+1)
(height,width) = psd2D.shape
plt.figure(figsize=(10,10*height/width),facecolor='white')
plt.clf()
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.imshow( psd2D, cmap='Greys_r',extent=[-np.pi,np.pi,-np.pi,np.pi],aspect='auto')
plt.show()
'''
Reads Image from the given path
'''
def read_image_from_path(path):
if path.split('.')[0] == 'png':
original_image = plt.imread(path).astype(float)
else:
original_image = img_as_float(io.imread(path))
return original_image
'''
Saves the Image at the given path
'''
def save_img_at_path(img, path):
plt.imsave(path, img)
'''
Given the intensity the Blur Effect is Applied using Gaussian filter
'''
def gaussian_filter(img, blur_intensity):
# Prepare an Gaussian convolution kernel
# First a 1-D Gaussian
t = np.linspace(-10, 10, 30)
bump = np.exp(-blur_intensity*t**2)
bump /= np.trapz(bump) # normalize the integral to 1
# make a 2-D kernel out of it
kernel = bump[:, np.newaxis] * bump[np.newaxis, :]
# Implement convolution via FFT
# Padded fourier transform, with the same shape as the image
kernel_ft = np.fft.fftn(kernel, s=img.shape[:2], axes=(0, 1))
img_ft = np.fft.fftn(img, axes=(0, 1))
# the 'newaxis' is to match to color direction
img2_ft = kernel_ft[:, :, np.newaxis] * img_ft
img2 = np.fft.ifft2(img2_ft, axes=(0, 1)).real
return img2