/
frequency_filter.py
81 lines (63 loc) · 2.47 KB
/
frequency_filter.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
79
80
81
"""
PYTHON METHOD DEFINITION
Jesse Jurman (jrj2703)
"""
import numpy as np
import cv2
def frequency_filter(im, frequencyFilter, delta=0):
"""
Function to apply a filter to an image.
Args:
im (array): source image to be filtered
if the image is color, it will be converted to grayscale
frequencyFilter (int): the frequency to filter the images on
delta (optional[int]): value to increase the digital counts by
Return:
the image with the applied filter, as numpy.complex128 data types
"""
if(len(im.shape) == 3):
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# shift image to line up with fourier transformation
filterShift = np.fft.fftshift(frequencyFilter)
# compute the fourier transform using fft
fourierTransform = np.fft.fft2(im)
# multiple the resulting fourier transform by the shifted filter
appliedFilter = filterShift * fourierTransform
# compute the inverse transform of the fourier transform
filteredImage = np.fft.ifft2(appliedFilter)
# display the magnitude to see the enhanced image
magnitude = 20*np.log(np.abs(filteredImage))
return magnitude
"""
PYTHON TEST HARNESS
"""
if __name__ == '__main__':
import cv2
import ipcv
import numpy
import os.path
import time
home = os.path.expanduser('~')
filename = home + os.path.sep + 'src/python/examples/data/giza.jpg'
filename = home + os.path.sep + 'src/python/examples/data/checkerboard.tif'
filename = home + os.path.sep + 'src/python/examples/data/lenna.tif'
im = cv2.imread(filename)
frequencyFilter = ipcv.filter_bandpass(im,
32,
10,
filterShape=ipcv.IPCV_GAUSSIAN)
startTime = time.clock()
offset = 0
filteredImage = ipcv.frequency_filter(im, frequencyFilter, delta=offset)
filteredImage = numpy.abs(filteredImage)
filteredImage = filteredImage.astype(dtype=numpy.uint8)
elapsedTime = time.clock() - startTime
print('Elapsed time (frequency_filter)= {0} [s]'.format(elapsedTime))
cv2.namedWindow(filename, cv2.WINDOW_AUTOSIZE)
cv2.imshow(filename, im)
cv2.imshow(filename, ipcv.histogram_enhancement(im))
filterName = 'Filtered (' + filename + ')'
cv2.namedWindow(filterName, cv2.WINDOW_AUTOSIZE)
cv2.imshow(filterName, filteredImage)
cv2.imshow(filterName, ipcv.histogram_enhancement(filteredImage))
ipcv.flush()