高斯差分边缘检测算子
时间: 2023-12-03 12:40:45 浏览: 55
高斯差分算子(Difference of Gaussian,DoG)是一种常用的边缘检测算法。它是通过对图像进行高斯滤波后,计算不同尺度下的高斯滤波结果之间的差异来实现的。具体步骤如下:
1. 对原始图像进行高斯滤波,得到不同尺度下的高斯模糊图像;
2. 计算相邻两个尺度下的高斯模糊图像之差,得到一组差分图像;
3. 对每个差分图像进行非极大值抑制,得到一组非极大值抑制图像;
4. 对所有非极大值抑制图像进行二值化处理,得到一组二值化图像;
5. 将所有二值化图像进行叠加,得到最终的边缘检测结果。
以下是Python实现高斯差分边缘检测算子的代码:
```python
import cv2
import numpy as np
# 读取图像并加上高斯噪声
img = cv2.imread('lena.jpg', 0)
img = cv2.GaussianBlur(img, (5, 5), 0)
img = img + np.random.normal(0, 25, img.shape)
# 定义高斯差分函数
def DoG(img, ksize, sigma1, sigma2):
g1 = cv2.GaussianBlur(img, ksize, sigma1)
g2 = cv2.GaussianBlur(img, ksize, sigma2)
return g1 - g2
# 计算高斯差分图像
dog1 = DoG(img, (5, 5), 1, 2)
dog2 = DoG(img, (5, 5), 2, 4)
dog3 = DoG(img, (5, 5), 4, 8)
# 非极大值抑制
def non_max_suppression(img):
h, w = img.shape
out = np.zeros((h, w), dtype=np.float32)
for y in range(1, h-1):
for x in range(1, w-1):
dx = img[y, x+1] - img[y, x-1]
dy = img[y+1, x] - img[y-1, x]
gradient = np.sqrt(dx**2 + dy**2)
if gradient == 0:
out[y, x] = 0
else:
angle = np.rad2deg(np.arctan(dy/dx))
if angle < 0:
angle += 180
if (angle <= 22.5) or (angle > 157.5):
if (img[y, x] >= img[y, x+1]) and (img[y, x] >= img[y, x-1]):
out[y, x] = img[y, x]
elif (22.5 < angle <= 67.5):
if (img[y, x] >= img[y-1, x+1]) and (img[y, x] >= img[y+1, x-1]):
out[y, x] = img[y, x]
elif (67.5 < angle <= 112.5):
if (img[y, x] >= img[y-1, x]) and (img[y, x] >= img[y+1, x]):
out[y, x] = img[y, x]
else:
if (img[y, x] >= img[y-1, x-1]) and (img[y, x] >= img[y+1, x+1]):
out[y, x] = img[y, x]
return out
# 非极大值抑制图像
nms1 = non_max_suppression(dog1)
nms2 = non_max_suppression(dog2)
nms3 = non_max_suppression(dog3)
# 二值化处理
th1 = cv2.threshold(nms1, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
th2 = cv2.threshold(nms2, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
th3 = cv2.threshold(nms3, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
# 叠加所有二值化图像
result = th1 + th2 + th3
# 显示结果
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](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)