-
Notifications
You must be signed in to change notification settings - Fork 2
/
Transform_fft.py
82 lines (61 loc) · 2.01 KB
/
Transform_fft.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
74
75
76
77
78
import sys,os
import numpy
import matplotlib.pyplot
from PIL import Image
def rgb2gray(rgb):
return numpy.dot(rgb[...,:3], [0.299, 0.587, 0.114])
image_path = sys.argv[1]
show_fq_figure = 1
#######################################################################3
# read img from file
img_file = Image.open(image_path)
img = numpy.array(img_file)
print 'rgb matrix'
print img
# turn rbg to gray
gray = rgb2gray(img)
print 'gray matrix', gray.shape
print gray
#######################################################################3
# fft process
fft_img = numpy.fft.fft2(gray)
# do some change for frequency display
show_fft_img = fft_img
show_fft_img = numpy.fft.fftshift(show_fft_img)
show_fft_img = numpy.abs(show_fft_img)
show_fft_img = numpy.log(show_fft_img)
# show frequency domain figure
if show_fq_figure:
print 'fft show matrix', show_fft_img.shape
print show_fft_img
matplotlib.pyplot.imshow(show_fft_img, cmap=matplotlib.pyplot.get_cmap('gray'))
matplotlib.pyplot.show()
'''
# do some modify in frequency domain
print 'do something on frequency domain...'
print 'min', numpy.amin(fft_img)
print 'max', numpy.amax(fft_img)
#fft_img = fft_img.astype(int)
#show_fft_img = show_fft_img.astype(int)
ma = numpy.amax(fft_img)
mi = numpy.amin(fft_img)
for (x,y),v in numpy.ndenumerate(show_fft_img):
if not( abs(x-512/2) > 100 or abs(y-512/2) > 100):
show_fft_img[x][y] = mi
else:
show_fft_img[x][y] = numpy.float64(show_fft_img[x][y])
#print 'x,y,v',x,y,show_fft_img[x][y]
#matplotlib.pyplot.imshow(show_fft_img, cmap=matplotlib.pyplot.get_cmap('gray'))
#matplotlib.pyplot.show()
show_fft_img = numpy.fft.ifftshift(show_fft_img)
fft_img = show_fft_img
'''
#######################################################################3
# inverse fft process
ifft_img = numpy.fft.ifft2(fft_img)
ifft_img = numpy.abs(ifft_img)
# show new gray figure
print 'new gray matrix'
print ifft_img
matplotlib.pyplot.imshow(ifft_img, cmap=matplotlib.pyplot.get_cmap('gray'))
matplotlib.pyplot.show()