自适应中值滤波实现canny边缘检测python
时间: 2023-09-28 14:06:45 浏览: 127
adapt_mid_filter.zip_mid_filter_中值滤波_滤波_自适应中值滤波
实现自适应中值滤波的代码如下:
```python
import cv2
def adaptive_median_filter(img, max_size):
for size in range(3, max_size+1, 2):
img_median = cv2.medianBlur(img, size)
img_median_diff = cv2.absdiff(img, img_median)
img_median_diff_binary = cv2.threshold(img_median_diff, 0, 255, cv2.THRESH_BINARY)[1]
if cv2.countNonZero(img_median_diff_binary) > 0:
continue
else:
return img_median
return img
```
然后使用canny边缘检测算法进行边缘检测,代码如下:
```python
import cv2
def canny_edge_detection(img, threshold1, threshold2):
img_blur = cv2.GaussianBlur(img, (5,5), 0)
img_canny = cv2.Canny(img_blur, threshold1, threshold2)
return img_canny
```
最后,将两个函数组合起来,即可实现自适应中值滤波后的canny边缘检测:
```python
import cv2
def adaptive_median_canny(img, max_size, threshold1, threshold2):
img_adaptive_median = adaptive_median_filter(img, max_size)
img_canny = canny_edge_detection(img_adaptive_median, threshold1, threshold2)
return img_canny
```
阅读全文