opencv fft 低通滤波
时间: 2023-12-22 16:28:24 浏览: 43
下面是使用OpenCV进行FFT低通滤波的步骤:
1.读取图像并将其转换为灰度图像。
2.对图像进行傅里叶变换。
```python
import cv2
import numpy as np
# 读取图像并转换为灰度图像
img = cv2.imread('image.jpg', 0)
# 进行傅里叶变换
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
```
3.构建低通滤波器。
```python
# 构建低通滤波器
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
mask = np.zeros((rows, cols), np.uint8)
r = 80
center = [crow, ccol]
x, y = np.ogrid[:rows, :cols]
mask_area = (x - center[0])**2 + (y - center[1])**2 <= r*r
mask[mask_area] = 1
```
4.将低通滤波器应用于傅里叶变换后的图像。
```python
# 将低通滤波器应用于傅里叶变换后的图像
fshift = fshift * mask
```
5.进行傅里叶逆变换并将结果转换为可视化图像。
```python
# 进行傅里叶逆变换
ishift = np.fft.ifftshift(fshift)
iimg = np.fft.ifft2(ishift)
result = np.abs(iimg)
# 将结果转换为可视化图像
result = (result - np.min(result)) / (np.max(result) - np.min(result))
result = np.uint8(result * 255)
```
完整代码如下:
```python
import cv2
import numpy as np
# 读取图像并转换为灰度图像
img = cv2.imread('image.jpg', 0)
# 进行傅里叶变换
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
# 构建低通滤波器
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
mask = np.zeros((rows, cols), np.uint8)
r = 80
center = [crow, ccol]
x, y = np.ogrid[:rows, :cols]
mask_area = (x - center[0])**2 + (y - center[1])**2 <= r*r
mask[mask_area] = 1
# 将低通滤波器应用于傅里叶变换后的图像
fshift = fshift * mask
# 进行傅里叶逆变换
ishift = np.fft.ifftshift(fshift)
iimg = np.fft.ifft2(ishift)
result = np.abs(iimg)
# 将结果转换为可视化图像
result = (result - np.min(result)) / (np.max(result) - np.min(result))
result = np.uint8(result * 255)
# 显示结果
cv2.imshow('Original Image', img)
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)