Canny Edge Detector

 

 

Goal

In this tutorial you will learn how to:

  • Use the OpenCV function Canny to implement the Canny Edge Detector.

Theory

  1. The Canny Edge detector was developed by John F. Canny in 1986. Also known to many as theoptimal detector, 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 ofsize = 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 andy 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 theupper 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.

 

 

 

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// Global variables

Mat src, src_gray;
Mat dst, detected_edges;

int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";

/**
 * @function CannyThreshold
 * @brief Trackbar callback - Canny thresholds input with a ratio 1:3
 */
void CannyThreshold(int, void*)
{
  /// Reduce noise with a kernel 3x3
  blur( src_gray, detected_edges, Size(3,3) );

  /// Canny detector
  Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );

  /// Using Canny's output as a mask, we display our result
  dst = Scalar::all(0);

  src.copyTo( dst, detected_edges);
  imshow( window_name, dst );
 }


/** @function main */
int main( int argc, char** argv )
{
  /// Load an image
  src = imread( argv[1] );

  if( !src.data )
  { return -1; }

  /// Create a matrix of the same type and size as src (for dst)
  dst.create( src.size(), src.type() );

  /// Convert the image to grayscale
  cvtColor( src, src_gray, CV_BGR2GRAY );

  /// Create a window
  namedWindow( window_name, CV_WINDOW_AUTOSIZE );

  /// Create a Trackbar for user to enter threshold
  createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );

  /// Show the image
  CannyThreshold(0, 0);

  /// Wait until user exit program by pressing a key
  waitKey(0);

  return 0;
  }


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值