| OpenCV 5.0.0alpha Open Source Computer Vision

Loading...
Searching...
No Matches
Canny Edge Detector

Prev Tutorial: Laplace Operator
Next Tutorial: Hough Line Transform

Original author | Ana Huamán
Compatibility | OpenCV >= 3.0

# Goal

In this tutorial you will learn how to:

- Use the OpenCV function cv::Canny to implement the Canny Edge Detector.

# Theory

The Canny Edge detector [48] was developed by John F. Canny in 1986. Also known to many as the optimal detector, the Canny algorithm aims to satisfy three main criteria:

- Low error rate: Meaning a good detection of only existent edges.
- Good localization: The distance between edge pixels detected and real edge pixels have to be minimized.
- Minimal response: Only one detector response per edge.

## Steps

1. Filter out any noise. The Gaussian filter is used for this purpose. An example of a Gaussian kernel of \(size = 5\) that might be used is shown below:

   \[K = \dfrac{1}{159}\begin{bmatrix} 2 & 4 & 5 & 4 & 2 \\ 4 & 9 & 12 & 9 & 4 \\ 5 & 12 & 15 & 12 & 5 \\ 4 & 9 & 12 & 9 & 4 \\ 2 & 4 & 5 & 4 & 2 \end{bmatrix}\]

2. Find the intensity gradient of the image. For this, we follow a procedure analogous to Sobel:
   1. Apply a pair of convolution masks (in \(x\) and \(y\) directions:

      \[G_{x} = \begin{bmatrix} -1 & 0 & +1 \\ -2 & 0 & +2 \\ -1 & 0 & +1 \end{bmatrix}\]

      \[G_{y} = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ +1 & +2 & +1 \end{bmatrix}\]

   2. Find the gradient strength and direction with:

      \[\begin{array}{l} G = \sqrt{ G_{x}^{2} + G_{y}^{2} } \\ \theta = \arctan(\dfrac{ G_{y} }{ G_{x} }) \end{array}\]

      The direction is rounded to one of four possible angles (namely 0, 45, 90 or 135)
3. Non-maximum suppression is applied. This removes pixels that are not considered to be part of an edge. Hence, only thin lines (candidate edges) will remain.

4. Hysteresis: The final step. Canny does use two thresholds (upper and lower):

   1. If a pixel gradient is higher than the upper threshold, the pixel is accepted as an edge
   2. If a pixel gradient value is below the lower threshold, then it is rejected.
   3. If the pixel gradient is between the two thresholds, then it will be accepted only if it is connected to a pixel that is above the upper threshold.

   Canny recommended a upper:lower ratio between 2:1 and 3:1.

5. For more details, you can always consult your favorite Computer Vision book.

# Code

C++

- The tutorial code's is shown lines below. You can also download it from here
  #include "opencv2/imgproc.hpp"
  #include "opencv2/highgui.hpp"
  #include <iostream>
  using namespace cv;
  Mat src, src_gray;
  Mat dst, detected_edges;
  int lowThreshold = 0;
  const int max_lowThreshold = 100;
  const int ratio = 3;
  const int kernel_size = 3;
  const char* window_name = "Edge Map";
  static void CannyThreshold(int, void*)
  {
  blur( src_gray, detected_edges, Size(3,3) );
  Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
  dst = Scalar::all(0);
  src.copyTo( dst, detected_edges);
  imshow( window_name, dst );
  }
  int main( int argc, char** argv )
  {
  CommandLineParser parser( argc, argv, "{@input | fruits.jpg | input image}" );
  src = imread( samples::findFile( parser.get<String>( "@input" ) ), IMREAD_COLOR ); // Load an image
  if( src.empty() )
  {
  std::cout << "Could not open or find the image!\n" << std::endl;
  std::cout << "Usage: " << argv[0] << " <Input image>" << std::endl;
  return -1;
  }
  dst.create( src.size(), src.type() );
  cvtColor( src, src_gray, COLOR_BGR2GRAY );
  namedWindow( window_name, WINDOW_AUTOSIZE );
  createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
  CannyThreshold(0, 0);
  waitKey(0);
  return 0;
  }
  cv::CommandLineParser
  Designed for command line parsing.
  Definition utility.hpp:890
  cv::Mat
  n-dimensional dense array class
  Definition mat.hpp:951
  cv::Mat::size
  MatSize size
  Definition mat.hpp:2448
  cv::Mat::copyTo
  void copyTo(OutputArray m) const
  Copies the matrix to another one.
  cv::Mat::create
  void create(int rows, int cols, int type)
  Allocates new array data if needed.
  cv::Mat::empty
  bool empty() const
  Returns true if the array has no elements.
  cv::Mat::type
  int type() const
  Returns the type of a matrix element.
  cv::Size_
  Template class for specifying the size of an image or rectangle.
  Definition types.hpp:338
  cv::String
  std::string String
  Definition cvstd.hpp:151
  highgui.hpp
  main
  int main(int argc, char *argv[])
  Definition highgui_qt.cpp:3
  imgproc.hpp
  cv
  Definition core.hpp:107

Java

- The tutorial code's is shown lines below. You can also download it from here
  import java.awt.BorderLayout;
  import java.awt.Container;
  import java.awt.Image;
  import javax.swing.BoxLayout;
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JSlider;
  import javax.swing.event.ChangeEvent;
  import javax.swing.event.ChangeListener;
  import org.opencv.core.Core;
  import org.opencv.core.CvType;
  import org.opencv.core.Mat;
  import org.opencv.core.Scalar;
  import org.opencv.core.Size;
  import org.opencv.highgui.HighGui;
  import org.opencv.imgcodecs.Imgcodecs;
  import org.opencv.imgproc.Imgproc;
  public class CannyDetectorDemo {
  private static final int MAX_LOW_THRESHOLD = 100;
  private static final int RATIO = 3;
  private static final int KERNEL_SIZE = 3;
  private static final Size BLUR_SIZE = new Size(3,3);
  private int lowThresh = 0;
  private Mat src;
  private Mat srcBlur = new Mat();
  private Mat detectedEdges = new Mat();
  private Mat dst = new Mat();
  private JFrame frame;
  private JLabel imgLabel;
  public CannyDetectorDemo(String[] args) {
  String imagePath = args.length > 0 ? args[0] : "../data/fruits.jpg";
  src = Imgcodecs.imread(imagePath);
  if (src.empty()) {
  System.out.println("Empty image: " + imagePath);
  System.exit(0);
  }
  // Create and set up the window.
  frame = new JFrame("Edge Map (Canny detector demo)");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  // Set up the content pane.
  Image img = HighGui.toBufferedImage(src);
  addComponentsToPane(frame.getContentPane(), img);
  // Use the content pane's default BorderLayout. No need for
  // setLayout(new BorderLayout());
  // Display the window.
  frame.pack();
  frame.setVisible(true);
  }
  private void addComponentsToPane(Container pane, Image img) {
  if (!(pane.getLayout() instanceof BorderLayout)) {
  pane.add(new JLabel("Container doesn't use BorderLayout!"));
  return;
  }
  JPanel sliderPanel = new JPanel();
  sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
  sliderPanel.add(new JLabel("Min Threshold:"));
  JSlider slider = new JSlider(0, MAX_LOW_THRESHOLD, 0);
  slider.setMajorTickSpacing(10);
  slider.setMinorTickSpacing(5);
  slider.setPaintTicks(true);
  slider.setPaintLabels(true);
  slider.addChangeListener(new ChangeListener() {
  @Override
  public void stateChanged(ChangeEvent e) {
  JSlider source = (JSlider) e.getSource();
  lowThresh = source.getValue();
  update();
  }
  });
  sliderPanel.add(slider);
  pane.add(sliderPanel, BorderLayout.PAGE_START);
  imgLabel = new JLabel(new ImageIcon(img));
  pane.add(imgLabel, BorderLayout.CENTER);
  }
  private void update() {
  Imgproc.blur(src, srcBlur, BLUR_SIZE);
  Imgproc.Canny(srcBlur, detectedEdges, lowThresh, lowThresh * RATIO, KERNEL_SIZE, false);
  dst = new Mat(src.size(), CvType.CV_8UC3, Scalar.all(0));
  src.copyTo(dst, detectedEdges);
  Image img = HighGui.toBufferedImage(dst);
  imgLabel.setIcon(new ImageIcon(img));
  frame.repaint();
  }
  public static void main(String[] args) {
  // Load the native OpenCV library
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  // Schedule a job for the event dispatch thread:
  // creating and showing this application's GUI.
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
  @Override
  public void run() {
  new CannyDetectorDemo(args);
  }
  });
  }
  }
  cv::Scalar_::all
  static Scalar_< _Tp > all(_Tp v0)
  returns a scalar with all elements set to v0
  cv::Scalar
  Scalar_< double > Scalar
  Definition types.hpp:712

Python

- The tutorial code's is shown lines below. You can also download it from here
  from __future__ import print_function
  import cv2 as cv
  import argparse
  max_lowThreshold = 100
  window_name = 'Edge Map'
  title_trackbar = 'Min Threshold:'
  ratio = 3
  kernel_size = 3
  def CannyThreshold(val):
  low_threshold = val
  img_blur = cv.blur(src_gray, (3,3))
  detected_edges = cv.Canny(img_blur, low_threshold, low_threshold*ratio, kernel_size)
  mask = detected_edges != 0
  dst = src * (mask[:,:,None].astype(src.dtype))
  cv.imshow(window_name, dst)
  parser = argparse.ArgumentParser(description='Code for Canny Edge Detector tutorial.')
  parser.add_argument('--input', help='Path to input image.', default='fruits.jpg')
  args = parser.parse_args()
  src = cv.imread(cv.samples.findFile(args.input))
  if src is None:
  print('Could not open or find the image: ', args.input)
  exit(0)
  src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
  cv.namedWindow(window_name)
  cv.createTrackbar(title_trackbar, window_name , 0, max_lowThreshold, CannyThreshold)
  CannyThreshold(0)
  cv.waitKey()
  cv::samples::findFile
  cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
  Try to find requested data file.
  cv::imshow
  void imshow(const String &winname, InputArray mat)
  Displays an image in the specified window.
  cv::waitKey
  int waitKey(int delay=0)
  Waits for a pressed key.
  cv::namedWindow
  void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
  Creates a window.
  cv::createTrackbar
  int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
  Creates a trackbar and attaches it to the specified window.
  cv::imread
  CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
  Loads an image from a file.
  cv::cvtColor
  void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
  Converts an image from one color space to another.
  cv::Canny
  void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)
  Finds edges in an image using the Canny algorithm canny86 .
  cv::blur
  void blur(InputArray src, OutputArray dst, Size ksize, Point anchor=Point(-1,-1), int borderType=BORDER_DEFAULT)
  Blurs an image using the normalized box filter.

- What does this program do?
  - Asks the user to enter a numerical value to set the lower threshold for our Canny Edge Detector (by means of a Trackbar).
  - Applies the Canny Detector and generates a mask (bright lines representing the edges on a black background).
  - Applies the mask obtained on the original image and display it in a window.

# Explanation (C++ code)

1. Create some needed variables:

   Mat src, src_gray;
   Mat dst, detected_edges;
   int lowThreshold = 0;
   const int max_lowThreshold = 100;
   const int ratio = 3;
   const int kernel_size = 3;
   const char* window_name = "Edge Map";

   Note the following:

   1. We establish a ratio of lower:upper threshold of 3:1 (with the variable ratio).
   2. We set the kernel size of \(3\) (for the Sobel operations to be performed internally by the Canny function).
   3. We set a maximum value for the lower Threshold of \(100\).
2. Loads the source image:
   CommandLineParser parser( argc, argv, "{@input | fruits.jpg | input image}" );
   src = imread( samples::findFile( parser.get<String>( "@input" ) ), IMREAD_COLOR ); // Load an image
   if( src.empty() )
   {
   std::cout << "Could not open or find the image!\n" << std::endl;
   std::cout << "Usage: " << argv[0] << " <Input image>" << std::endl;
   return -1;
   }
3. Create a matrix of the same type and size of src (to be dst):
   dst.create( src.size(), src.type() );
4. Convert the image to grayscale (using the function cv::cvtColor ):
   cvtColor( src, src_gray, COLOR_BGR2GRAY );
5. Create a window to display the results:
   namedWindow( window_name, WINDOW_AUTOSIZE );
6. Create a Trackbar for the user to enter the lower threshold for our Canny detector:
   createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
   Observe the following:
   1. The variable to be controlled by the Trackbar is lowThreshold with a limit of max_lowThreshold (which we set to 100 previously)
   2. Each time the Trackbar registers an action, the callback function CannyThreshold will be invoked.
7. Let's check the CannyThreshold function, step by step:
   1. First, we blur the image with a filter of kernel size 3:
      blur( src_gray, detected_edges, Size(3,3) );
   2. Second, we apply the OpenCV function cv::Canny :
      Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
      where the arguments are:
      - detected_edges: Source image, grayscale
      - detected_edges: Output of the detector (can be the same as the input)
      - lowThreshold: The value entered by the user moving the Trackbar
      - highThreshold: Set in the program as three times the lower threshold (following Canny's recommendation)
      - kernel_size: We defined it to be 3 (the size of the Sobel kernel to be used internally)
8. We fill a dst image with zeros (meaning the image is completely black).
   dst = Scalar::all(0);
9. Finally, we will use the function cv::Mat::copyTo to map only the areas of the image that are identified as edges (on a black background). cv::Mat::copyTo copy the src image onto dst. However, it will only copy the pixels in the locations where they have non-zero values. Since the output of the Canny detector is the edge contours on a black background, the resulting dst will be black in all the area but the detected edges.
   src.copyTo( dst, detected_edges);
10. We display our result:
    imshow( window_name, dst );

# Result

- After compiling the code above, we can run it giving as argument the path to an image. For example, using as an input the following image:

- Moving the slider, trying different threshold, we obtain the following result:

- Notice how the image is superposed to the black background on the edge regions.