創作不易,如果對您有所幫助,請幫忙點贊,感謝!
一. 傅里葉變換簡介:
在數字圖像處理中,有兩個經典的變換被廣泛使用——傅里葉變換和霍夫變換。傅里葉變換是將時間域上的信號轉變為頻率域上的信號,進而進行圖像去噪、圖像增強等處理。
傅里葉變換(Fourier Transform,FT)后,對同一事物的觀看角度隨之改變,可以從頻域里發現一些從時域里不易察覺的特征。某些在時域內不好處理的地方,在頻域內可以容易地處理。
傅里葉定理:“ 任何連續周期信號都可以表示成(或者無限逼近)一系列正弦信號的疊加。”
一維傅里葉公式如下:

我們知道,灰度圖像是由二維的離散的點構成的。二維離散傅里葉變換(Two-Dimensional Discrete Fourier Transform)常用於圖像處理中,對圖像進行傅里葉變換后得到其頻譜圖。頻譜圖中頻率高低表征圖像中灰度變化的劇烈程度。圖像中邊緣和噪聲往往是高頻信號,而圖像背景往往是低頻信號。我們在頻率域內可以很方便地對圖像的高頻或低頻信息進行操作,完成圖像去噪,圖像增強,圖像邊緣提取等操作。
對二維圖像進行傅里葉變換用如下式子進行:

對二維圖像進行傅里葉逆變換式子如下:

二. python實現二維圖像的傅里葉變換原理
1 import cv2 2 import numpy as np 3 4 # DFT 5 def dft(img): 6 H, W, channel = img.shape 7 8 # Prepare DFT coefficient 9 G = np.zeros((H, W, channel), dtype=np.complex) 10 # prepare processed index corresponding to original image positions 11 x = np.tile(np.arange(W), (H, 1)) 12 y = np.arange(H).repeat(W).reshape(H, -1) 13 14 # dft 15 for c in range(channel): 16 for v in range(H): 17 for u in range(W): 18 G[v, u, c] = np.sum(img[..., c] * np.exp(-2j * np.pi * (x * u / W + y * v / H))) / np.sqrt(H * W) 19 20 return G 21 22 # IDFT 23 def idft(G): 24 # prepare out image 25 H, W, channel = G.shape 26 out = np.zeros((H, W, channel), dtype=np.float32) 27 28 # prepare processed index corresponding to original image positions 29 x = np.tile(np.arange(W), (H, 1)) 30 y = np.arange(H).repeat(W).reshape(H, -1) 31 32 # idft 33 for c in range(channel): 34 for v in range(H): 35 for u in range(W): 36 out[v, u, c] = np.abs(np.sum(G[..., c] * np.exp(2j * np.pi * (x * u / W + y * v / H)))) / np.sqrt(W * H) 37 38 # clipping 39 out = np.clip(out, 0, 255) 40 out = out.astype(np.uint8) 41 42 return out 43 44 45 # Read image 46 img = cv2.imread("../head.png").astype(np.float32) 47 48 # DFT 49 G = dft(img) 50 51 # write poser spectal to image 52 ps = (np.abs(G) / np.abs(G).max() * 255).astype(np.uint8) 53 cv2.imwrite("out_ps.jpg", ps) 54 55 # IDFT 56 out = idft(G) 57 58 # Save result 59 cv2.imshow("result", out) 60 cv2.imwrite("out.jpg", out) 61 cv2.waitKey(0) 62 cv2.destroyAllWindows()
三. 實驗結果:


四. C語言實現圖像傅里葉變換:
1 // 傅里葉變換 2 3 void fre_spectrum(short **in_array, short **out_array, long height, long width) 4 5 { 6 7 double re, im, temp; 8 9 int move; 10 11 for (int i = 0; i < height; i++){ 12 13 for (int j = 0; j < width; j++){ 14 15 re = 0; 16 17 im = 0; 18 19 for (int x = 0; x < height; x++){ 20 21 for (int y = 0; y < width; y++){ 22 23 temp = (double)i * x / (double)height + 24 25 (double)j * y / (double)width; 26 27 move = (x + y) % 2 == 0 ? 1 : -1; 28 29 re += in_array[x][y] * cos(-2 * pi * temp) * move; 30 31 im += in_array[x][y] * sin(-2 * pi * temp) * move; 32 33 } 34 35 } 36 37 38 39 out_array[i][j] = (short)(sqrt(re*re + im*im) / sqrt(width*height)); 40 41 if (out_array[i][j] > 0xff) 42 43 out_array[i][j] = 0xff; 44 45 else if (out_array[i][j] < 0) 46 47 out_array[i][j] = 0; 48 49 } 50 51 } 52 53 } 54 55 // 傅里葉反變換 56 57 void idft(double** re_array, double** im_array, short** out_array, long height, long width) 58 59 { 60 61 double real, temp; 62 63 for (int i = 0; i < height; i++){ 64 65 for (int j = 0; j < width; j++){ 66 67 real = 0; 68 69 for (int x = 0; x < height; x++){ 70 71 for (int y = 0; y < width; y++){ 72 73 temp = (double)i * x / (double)height + 74 75 (double)j * y / (double)width; 76 77 real += re_array[x][y] * cos(2 * pi * temp) - 78 79 im_array[x][y] * sin(2 * pi * temp); 80 81 } 82 83 } 84 85 86 87 out_array[i][j] = (short)(real / sqrt(width*height)); 88 89 if (out_array[i][j] > 0xff) 90 91 out_array[i][j] = 0xff; 92 93 else if (out_array[i][j] < 0) 94 95 out_array[i][j] = 0; 96 97 } 98 99 } 100 101 printf("idft done\n"); 102 103 }
五. 參考內容:
https://www.jianshu.com/p/835a7a68d0ee
六. 版權聲明:
未經作者允許,請勿隨意轉載抄襲,抄襲情節嚴重者,作者將考慮追究其法律責任,創作不易,感謝您的理解和配合!