c++ - "Edge Detection"和 "Image Contours"之间的区别

标签 c++ image opencv image-processing artificial-intelligence

我正在编写以下代码:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

Mat src, grey;
int thresh = 10;

const char* windowName = "Contours";

void detectContours(int,void*);

int main()
{
    src = imread("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");

    //Convert to grey scale
    cvtColor(src,grey,CV_BGR2GRAY);

    //Remove the noise
    cv::GaussianBlur(grey,grey,Size(3,3),0);

    //Create the window
    namedWindow(windowName);

    //Display the original image
    namedWindow("Original");
    imshow("Original",src);

    //Create the trackbar
    cv::createTrackbar("Thresholding",windowName,&thresh,255,detectContours);

    detectContours(0,0);
    waitKey(0);
    return 0;

}

void detectContours(int,void*)
{
    Mat canny_output,drawing;

    vector<vector<Point>> contours;
    vector<Vec4i>heirachy;

    //Detect edges using canny
    cv::Canny(grey,canny_output,thresh,2*thresh);

    namedWindow("Canny");
    imshow("Canny",canny_output);

    //Find contours
    cv::findContours(canny_output,contours,heirachy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,Point(0,0));

    //Setup the output into black
    drawing = Mat::zeros(canny_output.size(),CV_8UC3);



    //Draw contours
    for(int i=0;i<contours.size();i++)
    {
        cv::drawContours(drawing,contours,i,Scalar(255,255,255),1,8,heirachy,0,Point());
    }

    imshow(windowName,drawing);

}

理论上,Contours的意思是检测曲线。 边缘检测 表示检测边缘。在上面的代码中,我使用 Canny 进行了边缘检测,并通过 findContours() 进行了曲线检测。以下是生成的图像

Canny 图片

enter image description here

轮廓图像

enter image description here

所以现在,如您所见,没有区别!那么,这两者之间的实际区别是什么?在 OpenCV 教程中,只给出了代码。我找到了关于什么是“轮廓”的解释,但它没有解决这个问题。

最佳答案

边缘被计算为图像梯度在梯度方向上的极值点。 如果有帮助,您可以将它们视为一维函数中的最小值和最大值。 关键是,边缘像素是一个局部概念:它们只是指出相邻像素之间的显着差异。

轮廓通常是从边缘获得的,但它们的目的是作为对象轮廓。 因此,它们需要是闭合曲线。 您可以将它们视为边界(一些图像处理算法和库这样调用它们)。 当它们从边缘获得时,您需要连接边缘以获得闭合轮廓。

关于c++ - "Edge Detection"和 "Image Contours"之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17103735/

相关文章:

c++ - 使用排序和函数对象,但函数对象不能修改成员变量

c++ - 错误 : no matching function for call to 'begin(int*&)' c++

image - Twitter 元图像未在 Twitter 上呈现,因为它是 "may be restricted by the site' s robots.txt 文件”

html - 我需要一个 iOS 工具来编码 HTML?

c++ - OpenCV copyTo断言错误

opencv - 在SURF中更改描述符大小

c++ - 在 C++ 中管理全局数据库连接

c++ - 将 QImage 与 QQuickImageProvider 一起使用

android - 在 ListView 末尾叠加图像

opencv - TBB parallel_for,线程数更少