zoukankan      html  css  js  c++  java
  • Marr-Hildreth 边缘检测 OpenCV C++实现

    算法原理

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述


    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);

    效果

    原图:
    在这里插入图片描述

    结果图:
    在这里插入图片描述

  • 相关阅读:
    passenger中的设置ssl
    memcached在rails中的使用介绍
    redis相关
    复制linode镜像
    memcached启动 安装
    A Short History of Character Sets
    du h df h 磁盘空间满处理办法
    硬盘2T 现在也才不到1000了,变得感觉太便宜了
    最近爬虫心得
    20110205网站更新部署过程记录
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/12821819.html
Copyright © 2011-2022 走看看