C++ - Difference Between - Edge Detection - and - Image Contours - Stack Overflow

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

x Dismiss

Join the Stack Overflow Community

Stack Overflow is a community of 6.9 million


programmers, just like you, helping each other.
Join them; it only takes a minute:

Sign up

Difference between “Edge Detection” and “Image Contours”

Please have a look at 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

//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

Contours Image
OK, 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.

Please help!

c++ image opencv image-processing artificial-intelligence

asked Jun 14 '13 at 7:56


PeakGen
6,496 36 109 227

I suggest you use cv::GaussianBlur() before cv::Canny(). This may get rid of most of the clutter while keeping
the main edges. – Bull Jun 14 '13 at 9:07

4 Answers

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.

answered Jun 14 '13 at 8:45


sansuiso
5,905 1 19 44

Thank you a lot for the reply. I really appreciate it :) – PeakGen Jun 15 '13 at 8:20

You're welcome :) – sansuiso Jun 16 '13 at 21:07


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.

answered Jun 14 '13 at 8:44


jnovacho
1,584 5 15 29

2 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. – Bull
Jun 14 '13 at 9:18

Thank you a lot for the reply, I already gave you a +1 – PeakGen Jun 15 '13 at 8:20

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, the contours is mostly used for object recognition, where
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.

answered Jun 14 '13 at 8:14


Nallath
1,697 12 32

since findContours() works on binary images, I would be extremely surprised if it used a Canny edge
detector. – Bull Jun 14 '13 at 9:15

Thank you a lot for the reply, I already gave you a +1 – PeakGen Jun 15 '13 at 8:22

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.

answered Jun 14 '13 at 11:46


William
3,474 14 29

Thank you a lot for the reply, I already gave you a +1 – PeakGen Jun 15 '13 at 8:23

You might also like