码迷,mamicode.com
首页 > 编程语言 > 详细

Marr-Hildreth 边缘检测 OpenCV C++实现

时间:2020-05-03 14:53:20      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:color   实现   ros   https   代码实现   term   pre   -o   kernel   

算法原理

技术图片

技术图片

技术图片

技术图片

技术图片


C++代码实现

 

 1 Mat MarrEdgeDetection(Mat src, int kernelDiameter, double sigma) {
 2     int kernel_size = kernelDiameter / 2;
 3     Mat kernel(kernelDiameter, kernelDiameter, CV_64FC1);
 4     for (int i = -kernel_size; i <= kernel_size; i++) {
 5         for (int j = -kernel_size; j <= kernel_size; j++) {
 6             kernel.at<double>(i + kernel_size, j + kernel_size) = exp(-((pow(j, 2) + pow(i, 2)) /
 7                 (pow(sigma, 2) * 2)))
 8                 * (((pow(j, 2) + pow(i, 2) - 2 *
 9                     pow(sigma, 2)) / (2 * pow(sigma, 4))));
10         }
11     }
12     Mat laplacian(src.rows - kernel_size * 2, src.cols - kernel_size * 2, CV_64FC1);
13     Mat dst = Mat::zeros(src.rows - kernel_size * 2, src.cols - kernel_size * 2, CV_8UC1);
14     for (int i = kernel_size; i < src.rows - kernel_size; i++) {
15         for (int j = kernel_size; j < src.cols - kernel_size; j++) {
16             double sum = 0;
17             for (int x = -kernel_size; x <= kernel_size; x++){
18                 for (int y = -kernel_size; y <= kernel_size; y++) {
19                     sum +=  src.at<uchar>(i + x, j + y) * kernel.at<double>(x + kernel_size, y + kernel_size);
20                 }
21             }
22             laplacian.at<double>(i - kernel_size, j - kernel_size) = sum;
23         }
24     }
25     for (int i = 1; i < dst.rows - 1; i++) {
26         for (int j = 1; j < dst.cols - 1; j++) {
27             if ((laplacian.at<double>(i - 1, j) * laplacian.at<double>(i + 1, j) < 0) || (laplacian.at<double>(i, j + 1) * laplacian.at<double>(i, j - 1) < 0) ||
28                 (laplacian.at<double>(i + 1, j - 1) * laplacian.at<double>(i - 1, j + 1) < 0) || (laplacian.at<double>(i - 1, j - 1) * laplacian.at <double> (i + 1, j + 1) < 0)) {
29                 dst.at<uchar>(i, j) = 255;
30             }
31         }
32     }
33     return dst;
34 }
35 //调用
36     Mat src = imread("F:\\1.jpg", 0);
37     Mat dst = MarrEdgeDetection(src, 9, 1.6);

效果

原图:
技术图片

结果图:
技术图片

Marr-Hildreth 边缘检测 OpenCV C++实现

标签:color   实现   ros   https   代码实现   term   pre   -o   kernel   

原文地址:https://www.cnblogs.com/ybqjymy/p/12821819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!