OpenCV image recognition from zero to proficient (+)-----canny operator Edge detection

Source: Internet
Author: User

Finally look at the canny operator, this is to be the best operator, because of the process, there is a quasi-test, the following will be listed, but also the last edge detection, so here as the end, to see the effect of the various edge detection.

Comparison of edge detection results

    • Roberts operator The detection method has a good effect on the image processing with steep low noise, but the result of extracting edge by using Roberts operator is thicker edge, so the edge location is not very accurate.
    • Sobel operator The detection method has better effect on the image processing of gray gradient and noise, and the Sobel operator is not accurate to the edge location, and the edge of the image is more than one pixel.
    • Prewitt operator The detection method has good effect on the image processing with gray gradient and noise. But the edge is wider, and there are more breaks.
    • Laplacian operator The method is sensitive to noise, so it is seldom used to detect edges, but to determine whether the edge pixels are treated as a clear or dark region of the image.
    • Canny Method It is not susceptible to noise disturbances and can detect real weak edges. The advantage is that strong and weak edges are detected separately using two different thresholds, and weak edges are included in the output image when weak edges and strong edges are connected

When analyzing the quality of edge detection, canny has 3 principles:

    • 1. Signal-to-noise ratio criterion
    • 2. Positioning accuracy Criteria
    • 3. Single-Edge response criteria

The basic idea of canny edge detection is: First, the image selection of a certain Gauss filter smoothing filtering, and then using the non-extremum suppression technology processing to obtain the final edge image.

Specific steps:

1. Smoothing the image with a Gaussian filter

Gaussian filtering of the image sounds very iffy, in fact, according to the pixel points to be filtered and the gray values of the neighboring points according to a certain parameter rules for weighted average. This effectively filters out the high-frequency noise superimposed in the ideal image.

2. Calculate the amplitude and direction of the gradient using the finite difference of first order bias

The image grayscale merit gradient can be approximated using first-order finite difference, so that the image can be x and the y two matrices of partial derivatives in the direction. The common gradient operator is Roberts.sobel,prewitt.,canny

3, the gradient amplitude of non-maximum value suppression

The larger the value of the element in the image gradient amplitude matrix, the greater the gradient value of the point in the image, but this does not mean that the point is the edge (which is only a process of image enhancement). in the Canny algorithm, the non-maximal value suppression is an important step for edge detection, which refers to finding the local maximum of pixel points, and the gray value corresponding to the non-maximal point is set to 0. This can eliminate a large part of the non-marginal point

4. Detecting and connecting edges with double threshold algorithm

Canny the method of reducing the number of false edges in the algorithm is to adopt the double threshold method. Select two thresholds (the selection method for thresholds is discussed in the extension), according to the high threshold is worth an edge image, such an image contains very few false edges, but because the threshold is high, the resulting image edge may not be closed, the problem is not resolved to use another low threshold value.

Popular: Is in the edge detection, or to use the filter to reduce noise, first through the horizontal and vertical direction of a partial derivative, the amplitude and direction of the gradient, so that each point may have 4 middle direction (0,45,90,135 degrees), in the local range, remain in the same direction, the largest gradient point, Non-maximum on zero, finally using 2 thresholds T1 and T2 (T1<T2), T2 used to find each segment, T1 is used to extend the two sides of these segments upward to find the break of the edge, and connect these edges.

<span style= "FONT-SIZE:18PX;" >c++: void Canny (Inputarray image,outputarray edges, double threshold1, double threshold2, int aperturesize=3,bool l2g Radient=false)  </span>

    • The first parameter, image of the Inputarray type, input images, i.e. source images, fill the object of the mat class, and need to be a single-channel 8-bit image.
    • second parameter, The Outputarray type of edges, the output edge graph, needs to have the same size and type as the source picture.
    • third parameter, A double of type threshold1, the first hysteresis threshold.
    • fourth parameter, A double of type threshold2, and a second hysteresis threshold value.
    • fifth parameter, An int of type aperturesize that represents the aperture size of the applied Sobel operator, which has a default value of 3.
    • sixth parameter, L2gradient of type bool, an identifier that calculates the gradient amplitude of an image, has a default value of FALSE.
<span style= "FONT-SIZE:18PX;" > #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <stdlib.h> #include  <stdio.h> #include <iostream> using namespace CV;  using namespace Std;  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;   Const char* window_name = "Edge Map"; static void Cannythreshold (int, void*) {Canny (Src_gray, Detected_edges, Lowthreshold, Lowthreshold*ratio, kernel      _size);      DST = scalar::all (0);      Src.copyto (DST, detected_edges);  Imshow (Window_name, DST);    } int main (int, char** argv) {src = imread ("d:\\lena.jpg", Cv_load_image_color);    if (!src.data) {return-1;}    Dst.create (Src.size (), Src.type ());    Cvtcolor (SRC, Src_gray, cv_bgr2gray);    Namedwindow (Window_name, cv_window_autosize); Createtrackbar ("Min Threshold:", Window_name, &lowthreshold, Max_lowthreshold, Cannythreshold);    Cannythreshold (0, 0);    Waitkey (0);  return 0; } </span>

Matlab

<span style= "FONT-SIZE:18PX;" >i=imread (' d:\lena.jpg '); I1=rgb2gray (i); Img1=edge (I1, ' canny ', [0.03,0.08],3]; subplot (121), imshow (i); subplot ( 122), Imshow (IMG1) </span>





OpenCV image recognition from zero to proficient (+)-----canny operator Edge detection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.