Difference between "Edge Detection" and "Image Contours"

45,353

Solution 1

Edges are computed as points that are extrema of the image gradient in the direction of the gradient. if it helps, you can think of them as the min and max points in a 1D function. The point is, edge pixels are a local notion: they just point out a significant difference between neighbouring pixels.

Contours are often obtained from edges, but they are aimed at being object contours. Thus, they need to be closed curves. You can think of them as boundaries (some Image Processing algorithms & librarires call them like that). When they are obtained from edges, you need to connect the edges in order to obtain a closed contour.

Solution 2

The main difference between finding edges and countours is that if you run finding edges the output is new image. In this new (edge image) image you will have highlighted edges. There are many algorithms for detecting edges look at wiki see also.

For example Sobel operator gives smooth "foggy" results. In your particular case, the catch is that you are using Canny edge detector. This one makes few steps further than other detectors. It actually runs further edge refinement steps. Output of the Canny detector is thus binary image, with 1 px wide lines in place of edges.

On the other hand Contours algorithm processes arbitrary binary image. So if you put in white filled square on black background. After running Contours algorithm, you would get white empty square, just the borders.

Other added bonus of contour detection is, it actually returns set of points! That's great, because you can use these points further on for some processing.

In your particular case, it's only coincidence that both images match. It not rule, and in your case, it's because of unique property of Canny algorithm.

Solution 3

Contours can actually do a bit more than "just" detect edges. The algorithm does indeed find edges of images, but also puts them in a hierarchy. This means that you can request outer borders of objects detected in your images. Such a thing would not be (directly) possible if you only check for edges.

As can be read in the documentation, detecting contours is mostly used for object recognition, whereas the canny edge detector is a more "global" operation. I wouldn't be surprised if the contour algorithm uses some sort of canny edge detection.

Solution 4

The notion of contours is used as a tool to work on edge data. Not all edges are the same. But in many cases, e.g. objects with unimodal color distribution (i.e. one color), edges are the actual contours (outline,shape).

  1. Detect not only curves, but anything connected on the edge map. (connected component analysis)[1]
  2. Useful for objects with unimodal color distribution (a foreground mask is easily found with a simple threshold). Your sample image is not suitable.

[1]Topological Structural Analysis of Digitized Binary Images by Border Following by Satoshi Suzuki, 1985.

Share:
45,353
PeakGen
Author by

PeakGen

CTO

Updated on April 10, 2020

Comments

  • PeakGen
    PeakGen about 4 years

    I am working on the following code:

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

    Theoretically, Contours means detecting curves. Edge detection means detecting Edges. In my above code, I have done edge detection using Canny and curve detection by findContours(). Following are the resulting images

    Canny Image

    enter image description here

    Contours Image

    enter image description here

    So now, as you can see, there is no difference! So, what is the actual difference between these 2? In OpenCV tutorials, only the code is given. I found an explanation about what is 'Contours' but it is not addressing this issue.

  • Bull
    Bull almost 11 years
    since findContours() works on binary images, I would be extremely surprised if it used a Canny edge detector.
  • Bull
    Bull almost 11 years
    Sobel isn't really an edge detector, it just gives the gradient. Canny however finds the maximal gradient, i.e. the peaks in the gradient. The OpenCV implementation of Canny() in fact uses Sobel() in its front end.
  • PeakGen
    PeakGen almost 11 years
    Thank you a lot for the reply. I really appreciate it :)
  • Shuvo Sarker
    Shuvo Sarker about 6 years
    So, contours always end up where they begin? Is that the difference?
  • sansuiso
    sansuiso about 6 years
    Yes, contours are closed while edges may be (polygonal) lines.
  • Scott
    Scott about 4 years
    Then, why these 2 results are almost identical? Or, is this image just bad to show the difference between edges and contours?
  • theprogrammer
    theprogrammer over 2 years
    So would it be right to say all contours are edges but not all edges are contours since edges need not be closed?