October 15 2017 0Comment
Canny edge detection Opencv C++

Canny edge detection in Opencv C++

(Last Updated On: October 15, 2017)

Canny edge detection in Opencv C++ :

Canny edge detection give us the structural information of the different type of objects found in frame. Canny consider an edge from frame when it gets a color difference in accumulator array for specific range. Canny Edge detection has following main properties:

  • It detects edges with a very low error rate which means a edge detected by this technique is 99% is edge.
  • Edge points detected by canny edge detection method are mostly real and accurate edges.
  • Noise ratio in canny edge detection is almost zero so we get true edges.

Canny edge detection function in OpenCV take 5 parameters. First parameter is frames input which is always a Mat object. Second parameter is also Mat object which is out and save the output edges. The next two 3rd and 4th parameters are about threshold value tell minimum and maximum thresh value. The last parameter used in canny edge detection is kernel size which is always 3 because we use sobel kernel. After edges extraction we create a vector object for line store and calculations.

How Canny edge detection algorithm Works:

Apply filters to avoid noise in frames

  • Find intensity gradient in image
  • Low intense gradient are removed only high intense are used for further process
  • Apply Double thresh hold to find potential and actual edges
  • Differentiate the edges from intensity and pick the strong edges

Along with different benefits of canny edges there are few limitations of canny edge technique:

  • Too much parameters
  • As a result we get a lot of edges from which we have to decide the actual shape by connecting edges
  • due to binary result we cannot find qualified and disqualified edges


Below is the code of Canny edge detection in Opencv C++


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

int main(){

cv :: Mat img= cv::imread("image.jpg");
 cv :: Mat contours;
 cv :: Mat gray_img;

cvtColor( img, gray_img, CV_RGB2GRAY );

cv :: Canny(img,contours,10,350);

cv :: namedWindow("Image");
 cv ::imshow("Image",img);

cv ::namedWindow("Gray");
 cv ::imshow("Gray",gray_img);





Write a Reply or Comment