使用python编写高斯滤波、中值滤波、均值滤波算法
时间: 2024-02-02 19:03:24 浏览: 104
以下是使用Python编写高斯滤波、中值滤波和均值滤波算法的示例代码:
1. 高斯滤波:
```python
import cv2
def gaussian_blur(image, kernel_size, sigma):
blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma)
return blurred_image
# 示例用法
image = cv2.imread('image.jpg')
blurred_image = gaussian_blur(image, 5, 0)
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
2. 中值滤波:
```python
import cv2
def median_blur(image, kernel_size):
blurred_image = cv2.medianBlur(image, kernel_size)
return blurred_image
# 示例用法
image = cv2.imread('image.jpg')
blurred_image = median_blur(image, 5)
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
3. 均值滤波:
```python
import cv2
def mean_blur(image, kernel_size):
blurred_image = cv2.blur(image, (kernel_size, kernel_size))
return blurred_image
# 示例用法
image = cv2.imread('image.jpg')
blurred_image = mean_blur(image, 5)
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文