日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

opencv图像傅里叶变换

發布時間:2023/12/29 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opencv图像傅里叶变换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

傅里葉變換可以用來分析不同濾波器的頻率特性。

?numpy中的傅里葉變換

numpy 中的FFT包可以實現快速傅里葉變換。np.fft.fft2()可以對信號進行頻率轉換。

""" 函數 np.fft.fft2() 可以對信號頻率轉換 輸出結果是一個復雜的數組。 第一個參數是 輸入圖像 圖像是灰度格式。 第二個參數是可選的, 決定輸出數組的大小。 輸出數組的大小和輸入圖像大小一樣。如果輸出結果比輸入圖像大 輸入圖像就需要在進行 FFT 前補0。如果輸出結果比輸入圖像小的話,輸入圖像就會被切割。 """ import cv2 import numpy as np from matplotlib import pyplot as pltimg = cv2.imread('img1.png', 0) #進行傅里葉變換,頻率為0的部分(直流分量)在輸出圖像的左上角,可以通過np.fft.fftshift()可以將頻率為0的部分平移到圖像的中心 f = np.fft.fft2(img) #進行平移,將直流分量平移到圖像的中心 fshift = np.fft.fftshift(f) # 振幅圖 magnitude_spectrum = 20 * np.log(np.abs(fshift))plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show()

?中心部分更白,這說明低頻分量更多。

import cv2 import numpy as np from matplotlib import pyplot as pltimg=cv2.imread('img2.png',0) #進行傅立葉變換,并顯示結果 fft2 = np.fft.fft2(img) #將圖像變換的原點移動到頻域矩形的中心,并顯示效果 shift2center = np.fft.fftshift(fft2) #對傅立葉變換的結果進行對數變換,并顯示效果 log_fft2 = np.log(1 + np.abs(fft2)) #對中心化后的結果進行對數變換,并顯示結果 log_shift2center = np.log(1 + np.abs(shift2center)) #進行逆平移 f_image = np.fft.ifftshift(shift2center) #逆變換 image_new = np.fft.ifft2(f_image) #反變換的結果是復數 image_new = np.abs(image_new) plt.subplot(2,3,1),plt.imshow(img,'gray'),plt.title('origin') plt.subplot(2,3,2),plt.imshow(np.abs(fft2),'gray'),plt.title('fft2') plt.subplot(2,3,3),plt.imshow(np.abs(shift2center),'gray'),plt.title('shift2center') plt.subplot(2,3,4),plt.imshow(log_fft2,'gray'),plt.title('log_fft2') plt.subplot(2,3,5),plt.imshow(log_shift2center,'gray'),plt.title('log_shift2center') plt.subplot(2,3,6),plt.imshow(image_new,'gray'),plt.title('log_shift2center') plt.show()

?

高通濾波

使用60*60的窗口進行掩膜進行去除低頻分量

import cv2 import numpy as np from matplotlib import pyplot as pltimg = cv2.imread('img1.png', 0) ##對圖像進行傅里葉變換 f = np.fft.fft2(img) ##將低頻分量平移到圖像的中心 fshift = np.fft.fftshift(f)rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) ##將低頻部分置為0。 定義了一個60*60的矩形窗口進行掩膜操作去除低頻分量 i=30 fshift[crow - i:crow + i, ccol - i:ccol + i] = 0 ##然后將低頻分量逆平移部分 f_ishift = np.fft.ifftshift(fshift) ##進行fft逆變換 img_back = np.fft.ifft2(f_ishift) # 取絕對值 img_back = np.abs(img_back)#原始的圖像 plt.subplot(131), plt.imshow(img, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) ##變換的圖像 plt.subplot(132), plt.imshow(img_back, cmap='gray') plt.title('Image after HPF'), plt.xticks([]), plt.yticks([]) plt.subplot(133), plt.imshow(img_back) plt.title('Result in JET'), plt.xticks([]), plt.yticks([]) plt.show()

?低通濾波

import cv2 import numpy as np from matplotlib import pyplot as pltimg = cv2.imread('img1.png', 0) ##對圖像進行傅里葉變換 f = np.fft.fft2(img) rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) ##這時高頻部分都在圖片的中心,將高頻部分置為0,實現低通濾波。 定義了一個60*60的矩形窗口進行掩膜操作去除低頻分量 i=30 f[crow - i:crow + i, ccol - i:ccol + i] = 0 ##進行fft逆變換 img_back = np.fft.ifft2(f) # 取絕對值 img_back = np.abs(img_back) #原始的圖像 plt.subplot(131), plt.imshow(img, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) ##變換的圖像 plt.subplot(132), plt.imshow(img_back, cmap='gray') plt.title('Image after LPF'), plt.xticks([]), plt.yticks([]) plt.subplot(133), plt.imshow(img_back) plt.title('Result in JET'), plt.xticks([]), plt.yticks([]) plt.show()

?

?opencv傅里葉變換

opencv中相應的函數cv2.dft()? cv2.idft()。輸入圖像可以首先轉換為np.float32格式。輸出圖像是雙通道的,分別為實數部分、虛數部分

""" OpenCV 中相應的函數是 cv2.dft() 和 cv2.idft()。和前面出的結果 一樣 但是是雙通道的。 第一個通道是結果的實數部 分 第二個通道是結果的虛數部分。 輸入圖像先 換成 np.float32 格式 使用函數 cv2.cartToPolar() 它會同時返回幅度和相位。 """ import numpy as np import cv2 from matplotlib import pyplot as pltimg = cv2.imread('img1.png', 0)##輸出是雙通道,分別為實數部分虛數部分 dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show()

?低通濾波

將高頻部分去除。就是對圖像進行模糊操作。首先構建了一個掩膜,低頻區域的地方設置為1,高頻區域設置為0.

import numpy as np import cv2 from matplotlib import pyplot as pltimg = cv2.imread('img1.png', 0)dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)dft_shift = np.fft.fftshift(dft)rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2)# create a mask first, center square is 1, remaining all zeros mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1# apply mask and inverse DFT fshift = dft_shift * mask f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(img_back, cmap='gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show()

import cv2 import numpy as np from matplotlib import pyplot as plt# 均值濾波器 mean_filter = np.ones((3, 3)) # 創建高斯濾波器 x = cv2.getGaussianKernel(5, 10)gaussian = x * x.T # 不同的邊緣檢測濾波器 # scharr in x-direction scharr = np.array([[-3, 0, 3],[-10, 0, 10],[-3, 0, 3]]) # sobel in x direction sobel_x = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]]) # sobel in y direction sobel_y = np.array([[-1, -2, -1],[0, 0, 0],[1, 2, 1]]) # laplacian laplacian = np.array([[0, 1, 0],[1, -4, 1],[0, 1, 0]]) filters = [mean_filter, gaussian, laplacian, sobel_x, sobel_y, scharr] filter_name = ['mean_filter', 'gaussian', 'laplacian', 'sobel_x', 'sobel_y', 'scharr_x']fft_filters = [np.fft.fft2(x) for x in filters] fft_shift = [np.fft.fftshift(y) for y in fft_filters] mag_spectrum = [np.log(np.abs(z) + 1) for z in fft_shift]for i in range(6):plt.subplot(2, 3, i + 1), plt.imshow(mag_spectrum[i], cmap='gray')plt.title(filter_name[i]), plt.xticks([]), plt.yticks([]) plt.show()

?

import numpy as np from matplotlib import pyplot as plt# 均值濾波器 mean_filter = np.ones((3, 3))##原來的頻譜圖 fft_filters = np.fft.fft2(mean_filter) ##經過平移之后的頻譜圖 fft_shift = np.fft.fftshift(fft_filters) mag_spectrum = np.log(np.abs(fft_shift) + 1) hh=np.log(np.abs(fft_filters)+1) plt.subplot(121) plt.imshow(hh, cmap='gray') plt.title('origin mean_filter'), plt.xticks([]), plt.yticks([]) plt.subplot(122) plt.imshow(mag_spectrum, cmap='gray') plt.title('平移之后的mean_filter'), plt.xticks([]), plt.yticks([]) plt.show()

?

上面的圖像是均值濾波器的幅度頻譜圖。中間部分最亮?

?

?

?

總結

以上是生活随笔為你收集整理的opencv图像傅里叶变换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。