OpenCV Development Notes (thirty-eight): 8 minutes with a fat red-depth understanding of your high-end Canny operator edge detection (easy to understand illustrations + + program source code)

If the text is the original article, shall not be reproduced without permission
of the original bloggers blog address: https://blog.csdn.net/qq21497936
original bloggers blog Navigation: https://blog.csdn.net/qq21497936/article/details / 102 478 062
This article blog address: https://blog.csdn.net/qq21497936/article/details/105284544
you readers, have poor human knowledge and infinite, or demand change, or find professionals, or their own research

table of Contents

Foreword

Demo

Canny edge detection operator

Canny high-end usage

Outline

Gaussian filtering function prototypes

Popular adaptive filter function prototypes

Filter function

Canny detection function prototypes

Source Demo

Project templates: corresponds to the version number v1.33.0


OpenCV development Box

 

    OpenCV Development Notes (thirty-eight): 8 minutes with a fat red-depth understanding of your high-end Canny operator edge detection (easy to understand illustrations + + program source code)

 

Foreword

      Red fat man to be! ! !

      This chapter began to explain the high-order Canny detection, in fact, is actually a high-end detection Canny Canny extra for detection of some processing

 

Demo

      Demo schematic image position Results:

 

Canny edge detection operator

      Please refer to "OpenCV development notes (37%): 8 minutes with a fat red-depth understanding of your Canny operator edge detection and edge detection (easy to understand illustrations + + program source code)" (click the portal) .

 

Canny high-end usage

Outline

      Canny high-end usage is actually artificial to be handled a certain noise reduction, such as the first use of a noise reduction filtering them, here we use a Gaussian filter, use a contrib in ximgproc processing module pop filter first noise reduction processing on the image, and then filtered.

Picture -> grayscale -> Gaussian (click the portal) -> Canny edge detection

Picture -> grayscale -> adaptive filter Popular (click the portal) -> Canny edge detection

High-end use, in fact, mentioned before, due to the different algorithms developers, a typical case of different ways of thinking.

 

Gaussian filtering function prototypes

void GaussianBlur( InputArray src, 
                OutputArray dst,
                Size ksize, 
                double sigmaX,
                double sigmaY = 0,
                int borderType = BORDER_DEFAULT );
  • A parameter : InputArray type generally cv :: Mat, and can handle any number of image channels. Note, however, the depth of the picture should be treated as CV_8U , CV_16U , CV_16S , CV_32F , CV_64F one;
  • Parameter Two ; OutputArray type, output target image, you need the original picture and have the same size and type;
  • Three parameters : Size type ksize, the size of the sub-accounts. General use Size (w, h) to indicate the size of the sub-accounting, Size (3,3) to represent different sub-accounts of sizes 3x3, w and h may be size;
  • Four parameters : double type sigmaX, represents the standard deviation of the Gaussian kernel function of the X-direction;
  • Five parameters : double type sigmaY, represents the standard deviation of the Gaussian kernel in the Y direction;

Popular adaptive filter function prototypes

Ptr<AdaptiveManifoldFilter> createAMFilter(double sigma_s, double sigma_r, bool adjust_outliers = false);
  • A parameter: spatial standard deviation, the range must be greater than 0;
  • Two parameters: the color space standard deviation, sigma bilateral filter which is similar in the color space, the range must be greater than 0 and less than 1;
  • Three parameters: adjusting outliers optional, specifies whether to use the random number generator calculates a feature vector (based on visual results, we did not see the actual difference);

Filter function

void filter(InputArray src,
            OutputArray dst,
            InputArray joint = noArray());
  • A parameter: Enter cv :: Mat;
  • Two parameters: Output cv :: Mat, the same size as the input channel;
  • Three parameters: an input coupled to FIG;

Create and use the sample:

// 使用自适应流形应用高维滤波。
cv::Ptr<cv::ximgproc::AdaptiveManifoldFilter> pAdaptiveManifoldFilter
          = cv::ximgproc::createAMFilter(16.0, 0.1, true);
pAdaptiveManifoldFilter->filter(srcMat, dstMat, srcMat2);

Canny detection function prototypes

void Canny( InputArray image,
          OutputArray edges,
          double threshold1,
          double threshold2,
          int apertureSize = 3,
          bool L2gradient = false );
  • A parameter: InputArray type of image, generally cv :: Mat, must be single-channel type;
  • Two parameters: outputArray Edges type; FIG edge of the output edge; single-channel 8-bit image, the input image is the same size;
  • Three parameters: Double type threshold1, a first threshold value with time delay;
  • Four parameters: threshold2, the second threshold value with time delay double type;
  • Five parameters: the amount of type int apertureSize, Sobel operator pore size, default is 3;
  • Six parameters: BOOL type L2gradient, calculated gradient magnitude of the operation, the default is false;

 

Source Demo

void OpenCVManager::testHighCanny()
{
    QString fileName1 = "E:/qtProject/openCVDemo/openCVDemo/modules/openCVManager/images/1.jpg";
    cv::Mat srcMat = cv::imread(fileName1.toStdString());

    if(!srcMat.data)
    {
        qDebug() << __FILE__ << __LINE__
                 << "Failed to load image:" << fileName1;
        return;
    }

    int width = 300;
    int height = 200;
    cv::resize(srcMat, srcMat, cv::Size(width, height));

    cv::String windowName = _windowTitle.toStdString();
    cvui::init(windowName);


    cv::Mat dstMat;
    dstMat = cv::Mat::zeros(srcMat.size(), srcMat.type());
    cv::Mat windowMat = cv::Mat(cv::Size(dstMat.cols * 3, dstMat.rows * 3),
                                srcMat.type());
    int ksize = 1;   // 核心大小
    int sigmaX = 0;  // x方向的标准偏差
    int sigmaY = 0;  // y方向的标准偏差

    int threshold1 = 200;
    int threshold2 = 100;

    int sigmaS = 160;
    int sigmaR = 2;

    cvui::window(windowMat, dstMat.cols, 0, dstMat.cols, dstMat.rows, "settings");
    cv::Mat grayMat;
    cv::Mat grayMat3Channels;
    cv::Mat mat;
    cv::cvtColor(srcMat, grayMat, CV_BGR2GRAY);
    cv::cvtColor(grayMat, grayMat3Channels, CV_GRAY2BGR);
    while(true)
    {
        windowMat = cv::Scalar(0, 0, 0);
        // 原图先copy到左边
        cv::Mat leftMat = windowMat(cv::Range(0, srcMat.rows),
                                    cv::Range(0, srcMat.cols));
        cv::addWeighted(leftMat, 1.0f, srcMat, 1.0f, 0.0f, leftMat);
        // 中间为调整滤波参数的相关设置
        cvui::printf(windowMat, 375, 20, "ksize = size *  2 + 1");
        cvui::trackbar(windowMat, 375, 30, 165, &ksize, 0, 10);

        cvui::printf(windowMat, 375, 80, "sigmaX");
        cvui::trackbar(windowMat, 375, 90, 165, &sigmaX, 0, 100);

        cvui::printf(windowMat, 375, 140, "sigmaY");
        cvui::trackbar(windowMat, 375, 150, 165, &sigmaY, 0, 100);


        // 复制灰度图像
        {
            cv::Mat rightMat = windowMat(cv::Range(srcMat.rows * 1, srcMat.rows * 2),
                                         cv::Range(srcMat.cols * 0, srcMat.cols * 1));
            cv::addWeighted(rightMat, 0.0f, grayMat3Channels, 1.0f, 0.0f, rightMat);
        }

        {
            // 高斯滤波
            cv::Mat mat;
            cv::GaussianBlur(grayMat3Channels, mat, cv::Size(ksize * 2 + 1, ksize * 2 + 1), sigmaX / 10.f, sigmaY / 10.f);

            // 效果图copy到右边
            // 注意:rang从位置1到位置2,不是位置1+宽度
            cv::Mat rightMat = windowMat(cv::Range(0, srcMat.rows),
                                         cv::Range(srcMat.cols * 2, srcMat.cols * 3));
            cv::addWeighted(rightMat, 0.0f, mat, 1.0f, 0.0f, rightMat);
            // 高斯滤波后进行边缘检测
            // 使用边缘检测
            cv::Canny(mat, dstMat, threshold1, threshold2, 3);
            cv::Mat rightMat2 = windowMat(cv::Range(srcMat.rows * 1, srcMat.rows * 2),
                                         cv::Range(srcMat.cols * 2, srcMat.cols * 3));
            cv::cvtColor(dstMat, dstMat, CV_GRAY2BGR);
            cv::addWeighted(rightMat2, 0.0f, dstMat, 1.0f, 0.0f, rightMat2);

        }
        {
            cvui::printf(windowMat,
                         srcMat.cols * 1 + 75,
                         srcMat.rows * 1 + 20,
                         "threshold1");
            cvui::trackbar(windowMat,
                           srcMat.cols * 1 + 75,
                           srcMat.rows * 1 + 50,
                           165,
                           &threshold1,
                           0,
                           255);
            cvui::printf(windowMat,
                         srcMat.cols * 1 + 75,
                         srcMat.rows * 1 + 100, "threshold2");
            cvui::trackbar(windowMat,
                           srcMat.cols * 1 + 75,
                           srcMat.rows * 1 + 130,
                           165,
                           &threshold2,
                           0,
                           255);
            // 使用边缘检测
            cv::Canny(srcMat, dstMat, threshold1, threshold2, 3);
            // copy
            cv::Mat temp;
            mat = windowMat(cv::Range(srcMat.rows * 2, srcMat.rows * 3),
                            cv::Range(srcMat.cols * 0, srcMat.cols * 1));

            cv::cvtColor(dstMat, temp, CV_GRAY2BGR);
            cv::addWeighted(mat, 0.0f, temp, 1.0f, 0.0f, mat);
        }
        {
            cvui::printf(windowMat, 75 + width * 1, height * 2 + 20, "sigmaS");
            cvui::trackbar(windowMat, 75 + width * 1, height * 2 + 50, 165, &sigmaS, 101, 10000);
            cvui::printf(windowMat, 75 + width * 1, height * 2 + 90, "sigmaR");
            cvui::trackbar(windowMat, 75 + width * 1, height * 2 + 120, 165, &sigmaR, 1, 100);

            // 使用自适应流形应用高维滤波。
            cv::Mat tempMat;
            cv::Ptr<cv::ximgproc::AdaptiveManifoldFilter> pAdaptiveManifoldFilter
                    = cv::ximgproc::createAMFilter(sigmaS/100.0f, sigmaR/100.0f, true);
            pAdaptiveManifoldFilter->filter(grayMat, tempMat);
            // 使用边缘检测
            cv::Canny(tempMat, tempMat, threshold1, threshold2, 3);
            // copy
            mat = windowMat(cv::Range(srcMat.rows * 2, srcMat.rows * 3),
                            cv::Range(srcMat.cols * 2, srcMat.cols * 3));
            cv::cvtColor(tempMat, dstMat, CV_GRAY2BGR);
            cv::addWeighted(mat, 0.0f, dstMat, 1.0f, 0.0f, mat);

        }
        // 更新
        cvui::update();
        // 显示
        cv::imshow(windowName, windowMat);
        // esc键退出
        if(cv::waitKey(25) == 27)
        {
            break;
        }
    }
}

 

Project templates: corresponds to the version number v1.33.0

      The corresponding version number v1.33.0

 

The original bloggers blog address: https://blog.csdn.net/qq21497936
original bloggers blog Navigation: https://blog.csdn.net/qq21497936/article/details/102478062
This article blog address: HTTPS: // Blog .csdn.net / qq21497936 / article / details / 105284544

Published 255 original articles · won praise 370 · views 500 000 +

Guess you like

Origin blog.csdn.net/qq21497936/article/details/105284544