码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV Tutorials —— Canny Edge Detector

时间:2014-11-19 00:02:06      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   for   strong   

最好的边缘检测子

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.

 

  1. 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 bubuko.com,布布扣 and bubuko.com,布布扣 directions:

      bubuko.com,布布扣

    2. Find the gradient strength and direction with:

      bubuko.com,布布扣

      The direction is rounded to one of four possible angles (namely 0, 45, 90 or 135)

    3.  

    4. 3,Non-maximum suppression is applied. 非最大抑制

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

    6. 滞后性阈值

      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.

 

Code

#include "stdafx.h"

#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( "img2.jpg" );

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

 

程序中在进行canny边缘检测之前进行了,滤波 ~~

OpenCV Tutorials —— Canny Edge Detector

标签:style   http   io   ar   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/sprint1989/p/4106723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!