标签:color 实现 ros https 代码实现 term pre -o kernel
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