dog 高斯差分边缘检测

1 篇文章 0 订阅

Applies two Gaussian blurs to the drawable, and subtracts the results.  This is robust and widely used method for detecting edges.

 

cv::Mat input;// source image
cv::Mat edge;// output image
cv::Mat blur1;
    cv::Mat blur2;
    int r1 = 1; // 奇数
    int r2 = 49;// 奇数
    float s1 =sqrt (-(r1 * r1) / (2 * log (1.0 / 255.0))) ;
    float s2 =sqrt (-(r2 * r2) / (2 * log (1.0 / 255.0))) ;
    // 两个不同半径的高斯滤波
    cv::GaussianBlur(input, blur1, cv::Size(r1,r1),s1);
    cv::GaussianBlur(input, blur2, cv::Size(r2,r2),s2);
    //作差 
    cv::subtract(blur1,blur2,edge);

    //反色
    cv::bitwise_not(edge,edge);

   

效果图

Gimp 插件高斯差分边缘检测 源码:

https://github.com/piksels-and-lines-orchestra/gimp/blob/master/plug-ins/common/edge-dog.c

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分算子(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() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值