OpenCV半小時(shí)掌握基本操作之傅里葉變換
概述
OpenCV 是一個(gè)跨平臺(tái)的計(jì)算機(jī)視覺(jué)庫(kù), 支持多語(yǔ)言, 功能強(qiáng)大. 今天小白就帶大家一起攜手走進(jìn) OpenCV 的世界.
高頻 vs 低頻
高頻 vs 低頻:
- 高頻: 變換劇烈的灰度分量, 例如邊界
- 低頻: 變換緩慢的灰度分量, 例如一片大海
濾波:
- 低通濾波器: 只保留低頻, 會(huì)使得圖像模糊
- 高通濾波器: 只保留高頻, 會(huì)使得圖像細(xì)節(jié)增強(qiáng)
傅里葉變換
傅里葉變化 (Fourier Transform) 是一種分析信號(hào)的方法. 傅里葉變化可分析信號(hào)的成分, 也可以用這些成分合成信號(hào).
效果:
傅里葉變換:
傅里葉逆變換:
在 OpenCV 中實(shí)現(xiàn)傅里葉變換的函數(shù)是cv2.dft()
和cv2.idft()
(傅里葉逆變化)
代碼詳解
輸入轉(zhuǎn)換
傅里葉變換支持的輸入格式是np.float32
, 所以我們需要先把圖像轉(zhuǎn)換到要求的格式.
代碼實(shí)現(xiàn):
import numpy as np import cv2 # 讀取圖片, 并轉(zhuǎn)換成灰度圖 img = cv2.imread("Mona_Lisa.jpg", cv2.IMREAD_GRAYSCALE) print(img.dtype) # unit8數(shù)據(jù)類型 # 轉(zhuǎn)換成np.float32 img_float32 = np.float32(img) print(img_float32.dtype) # float32數(shù)據(jù)類型
輸出結(jié)果:
uint8
float32
傅里葉變換
格式:
cv2.dft(src, dst=None, flags=None, nonzeroRows=None)
參數(shù):
- src: 輸入圖像
- dst: 輸出圖像, 默認(rèn)為 None
- flags: 轉(zhuǎn)換標(biāo)志 (5 種)
- nonezeroRows: 要處理的 dst 行數(shù), 默認(rèn)為 None
返回值:
- 實(shí)部和虛部 (雙通道)
- 實(shí)部: 代表所有的偶函數(shù) (余弦函數(shù)) 的部分
- 虛部: 代表所有的奇函數(shù) (正弦函數(shù)) 的部分
代碼實(shí)現(xiàn):
# 傅里葉變換 dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT) # 中心轉(zhuǎn)換, 將低頻挪到中心 dft_shift = np.fft.fftshift(dft)
獲取幅度譜
幅度譜 (Magnitude Spectrum), 即從構(gòu)成波形的頻率側(cè)面看過(guò)去, 每一個(gè)頻率分量都會(huì)在側(cè)面的投影, 如圖:
通過(guò)```cv2.magnitude``我們可以極端二維矢量的幅值.
格式:
cv2.magnitude(x, y, magnitude=None)
參數(shù):
- x: 實(shí)部
- y: 虛部
代碼實(shí)現(xiàn):
# 獲取幅度譜, 映射到灰度空間 [0, 255] magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) # 幅度譜展示 combine = np.hstack((img, magnitude_spectrum.astype(np.uint8))) cv2.imshow("combine", combine) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
傅里葉逆變換
格式:
cv2.idft(src, dst=None, flags=None, nonzeroRows=None)
參數(shù):
- src: 輸入圖像
- dst: 輸出圖像, 默認(rèn)為 None
- flags: 轉(zhuǎn)換標(biāo)志 (5 種)
- nonezeroRows: 要處理的 dst 行數(shù), 默認(rèn)為 None
返回值:
- 實(shí)部和虛部 (雙通道)
- 實(shí)部: 代表所有的偶函數(shù) (余弦函數(shù)) 的部分
- 虛部: 代表所有的奇函數(shù) (正弦函數(shù)) 的部分
代碼實(shí)現(xiàn):
# 獲取中心位置 rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) # 低通濾波 mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1 # 傅里葉逆變換 fshidt = dft_shift * mask f_ishift = np.fft.ifftshift(fshidt) img_back = cv2.idft(f_ishift)
獲取低頻
import numpy as np import cv2 # 讀取圖片, 并轉(zhuǎn)換成灰度圖 img = cv2.imread("Mona_Lisa.jpg", cv2.IMREAD_GRAYSCALE) print(img.dtype) # unit8數(shù)據(jù)類型 # 轉(zhuǎn)換成np.float32 img_float32 = np.float32(img) print(img_float32.dtype) # float32數(shù)據(jù)類型 # 傅里葉變換 dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT) # 中心轉(zhuǎn)換, 將低頻挪到中心 dft_shift = np.fft.fftshift(dft) # 獲取幅度譜 magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) # 幅度譜展示 combine = np.hstack((img, magnitude_spectrum.astype(np.uint8))) cv2.imshow("combine", combine) cv2.waitKey(0) cv2.destroyAllWindows() # 獲取中心位置 rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) # 低通濾波 mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1 fshidt = dft_shift * mask f_ishift = np.fft.ifftshift(fshidt) # 傅里葉逆變換, 獲取低頻圖像 img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]) # 結(jié)果展示 img_back = 255 * cv2.normalize(img_back, None, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) # 標(biāo)準(zhǔn)化 result = np.hstack((img, img_back.astype(np.uint8))) cv2.imshow("result", result) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
獲取高頻
import numpy as np import cv2 # 讀取圖片, 并轉(zhuǎn)換成灰度圖 img = cv2.imread("Mona_Lisa.jpg", cv2.IMREAD_GRAYSCALE) print(img.dtype) # unit8數(shù)據(jù)類型 # 轉(zhuǎn)換成np.float32 img_float32 = np.float32(img) print(img_float32.dtype) # float32數(shù)據(jù)類型 # 傅里葉變換 dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT) # 中心轉(zhuǎn)換, 將低頻挪到中心 dft_shift = np.fft.fftshift(dft) # 獲取幅度譜 magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) # 幅度譜展示 combine = np.hstack((img, magnitude_spectrum.astype(np.uint8))) cv2.imshow("combine", combine) cv2.waitKey(0) cv2.destroyAllWindows() # 獲取中心位置 rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) # 高通濾波 mask = np.ones((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 0 fshidt = dft_shift * mask f_ishift = np.fft.ifftshift(fshidt) # 傅里葉逆變換, 獲取高頻圖像 img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]) # 結(jié)果展示 img_back = 255 * cv2.normalize(img_back, None, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) # 標(biāo)準(zhǔn)化 result = np.hstack((img, img_back.astype(np.uint8))) cv2.imshow("result", result) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
到此這篇關(guān)于OpenCV半小時(shí)掌握基本操作之傅里葉變換的文章就介紹到這了,更多相關(guān)OpenCV傅里葉變換內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。