An example on Discrete Fourier transform (DFT) in python.
1
2
3'''
4sample for discrete fourier transform (dft)
5
6USAGE:
7 dft.py <image_file>
8'''
9
10
11
12from __future__ import print_function
13
14import numpy as np
15import cv2 as cv
16
17import sys
18
19
20def shift_dft(src, dst=None):
21 '''
22 Rearrange the quadrants of Fourier image so that the origin is at
23 the image center. Swaps quadrant 1 with 3, and 2 with 4.
24
25 src and dst arrays must be equal size & type
26 '''
27
28 if dst is None:
29 dst = np.empty(src.shape, src.dtype)
30 elif src.shape != dst.shape:
31 raise ValueError("src and dst must have equal sizes")
32 elif src.dtype != dst.dtype:
33 raise TypeError("src and dst must have equal types")
34
35 if src is dst:
36 ret = np.empty(src.shape, src.dtype)
37 else:
38 ret = dst
39
40 h, w = src.shape[:2]
41
42 cx1 = cx2 = w // 2
43 cy1 = cy2 = h // 2
44
45
46 if w % 2 != 0:
47 cx2 += 1
48 if h % 2 != 0:
49 cy2 += 1
50
51
52
53
54 ret[h-cy1:, w-cx1:] = src[0:cy1 , 0:cx1 ]
55 ret[0:cy2 , 0:cx2 ] = src[h-cy2:, w-cx2:]
56
57
58 ret[0:cy2 , w-cx2:] = src[h-cy2:, 0:cx2 ]
59 ret[h-cy1:, 0:cx1 ] = src[0:cy1 , w-cx1:]
60
61 if src is dst:
62 dst[:,:] = ret
63
64 return dst
65
66
68 if len(sys.argv) > 1:
69 fname = sys.argv[1]
70 else:
71 fname = 'baboon.jpg'
72 print("usage : python dft.py <image_file>")
73
75
76
78 h, w = im.shape[:2]
79
80 realInput = im.astype(np.float64)
81
82
85
86
87 dft_A = np.zeros((dft_N, dft_M, 2), dtype=np.float64)
88 dft_A[:h, :w, 0] = realInput
89
90
91
92 cv.dft(dft_A, dst=dft_A, nonzeroRows=h)
93
95
96
98
99
100 magnitude =
cv.sqrt(image_Re**2.0 + image_Im**2.0)
101
102
103 log_spectrum =
cv.log(1.0 + magnitude)
104
105
106
107 shift_dft(log_spectrum, log_spectrum)
108
109
110 cv.normalize(log_spectrum, log_spectrum, 0.0, 1.0, cv.NORM_MINMAX)
112
114 print('Done')
115
116
117if __name__ == '__main__':
118 print(__doc__)
void split(const Mat &src, Mat *mvbegin)
Divides a multi-channel array into several single-channel arrays.
void sqrt(InputArray src, OutputArray dst)
Calculates a square root of array elements.
int getOptimalDFTSize(int vecsize)
Returns the optimal DFT size for a given vector size.
void normalize(InputArray src, InputOutputArray dst, double alpha=1, double beta=0, int norm_type=NORM_L2, int dtype=-1, InputArray mask=noArray())
Normalizes the norm or value range of an array.
void log(InputArray src, OutputArray dst)
Calculates the natural logarithm of every array element.
void dft(InputArray src, OutputArray dst, int flags=0, int nonzeroRows=0)
Performs a forward or inverse Discrete Fourier transform of a 1D or 2D floating-point array.
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void destroyAllWindows()
Destroys all of the HighGUI windows.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3