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

2021-01-30 02:13

阅读:649

标签: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 ) {
 5         for (int j = -kernel_size; j ) {
 6             kernel.atdouble>(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 ) {
15         for (int j = kernel_size; j ) {
16             double sum = 0;
17             for (int x = -kernel_size; x ){
18                 for (int y = -kernel_size; y ) {
19                     sum +=  src.at(i + x, j + y) * kernel.atdouble>(x + kernel_size, y + kernel_size);
20                 }
21             }
22             laplacian.atdouble>(i - kernel_size, j - kernel_size) = sum;
23         }
24     }
25     for (int i = 1; i 1; i++) {
26         for (int j = 1; j 1; j++) {
27             if ((laplacian.atdouble>(i - 1, j) * laplacian.atdouble>(i + 1, j) 0) || (laplacian.atdouble>(i, j + 1) * laplacian.atdouble>(i, j - 1) 0) ||
28                 (laplacian.atdouble>(i + 1, j - 1) * laplacian.atdouble>(i - 1, j + 1) 0) || (laplacian.atdouble>(i - 1, j - 1) * laplacian.at double> (i + 1, j + 1) 0)) {
29                 dst.at(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


评论


亲,登录后才可以留言!