python opencv图像滤波
时间: 2023-06-14 22:02:53 浏览: 154
在 OpenCV 中,图像滤波是图像处理中常用的操作之一,可以用于去噪、边缘检测和图像增强等应用。常见的图像滤波算法包括均值滤波、高斯滤波、中值滤波等。
以下是使用 Python OpenCV 库进行图像滤波的示例代码:
1. 均值滤波
```python
import cv2
img = cv2.imread('image.jpg')
blur = cv2.blur(img, (5, 5)) # kernel size 5x5
cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
2. 高斯滤波
```python
import cv2
img = cv2.imread('image.jpg')
blur = cv2.GaussianBlur(img, (5, 5), 0) # kernel size 5x5 and sigma 0
cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
3. 中值滤波
```python
import cv2
img = cv2.imread('image.jpg')
blur = cv2.medianBlur(img, 5) # kernel size 5x5
cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,`cv2.imread` 函数用于读取图像,`cv2.imshow` 函数用于显示图像,`cv2.waitKey` 函数用于等待键盘输入,`cv2.destroyAllWindows` 函数用于销毁所有窗口。
在以上示例代码中,`kernel size` 表示滤波器的大小,`sigma` 表示高斯滤波的标准差。可以根据具体需求调整这些参数。
阅读全文
相关推荐
















