Canny Edge detection algorithm

Source: Internet
Author: User
Tags cos sin x2 y2

As a comparison, let's look at the principle of Sobel:

The principle of Sobel:

The bell operator (Sobeloperator) is one of the operators in image processing, which is mainly used for edge detection. Technically, it is a discrete difference operator that is used to calculate the approximate value of the gradient of the image luminance function. Using this operator at any point in the image will result in a corresponding gradient vector or its normal vector.

The operator consists of two sets of 3x3 matrices, which are transverse and longitudinal, and the horizontal and vertical luminance difference approximation can be obtained by the plane convolution with the image respectively. If the original image is represented by a, the GX and Gy represent images that are detected by transverse and longitudinal edges, respectively

MATLAB implementation of Canny edge detection operator

In the above example, if the angle θ above is equal to zero, that means the image has a longitudinal edge and the left side is darker than the right.

In edge detection, a commonly used template is the Sobel operator. There are two Sobel operators, one is to detect the horizontal edge, and the other is to detect the vertical flat edge. The effect of Sobel operator on the position of pixels is weighted, and the effect is better.

Another form of Sobel operator is isotropic Sobel (Isotropicsobel) operator, there are two, one is to detect the horizontal edge, and the other is to detect vertical flat edge. Compared with the ordinary Sobel operator, the isotropic Sobel operator has a more accurate position weighting coefficient, which is consistent in detecting the edges of different directions. Because of the particularity of the building image, we can find that it is not necessary to calculate the gradient direction when dealing with the contour of this kind of image, so the program does not give the processing method of the isotropic Sobel operator.

Because the Sobel operator is the form of filter operator, which can be used to extract the edge, the fast convolution function is simple and effective, so it is widely used. In the ointment, the Sobel operator does not distinguish the subject and background of the image strictly, in other words, the Sobel operator is not processed based on the image grayscale, and since the Sobel operator does not strictly simulate human visual physiology, the extracted image contour is sometimes not satisfactory. In the observation of an image, we often first notice that the image and the background of the different parts, it is this part of the main highlight, based on the theory, we give the following threshold contour extraction algorithm, the algorithm has been mathematically proved that when the pixel points to meet the normal distribution of the solution is the best

================= Hellili Division line Ah, start canny bar =======

The principle of canny:

(1) Image edge detection must meet two conditions: one can effectively suppress noise, and the second must be as precise as possible to determine the position of the edge.

(2) The optimal approximation operator is obtained based on the measurement of SNR and the product of positioning. This is the canny edge detection operator.

(3) similar to Marr (LoG) edge detection method, also belongs to the first smoothing after the derivative number method

Canny Edge detection algorithm

Step1: Smoothing the image with Gaussian filter;

Step2: The amplitude and direction of the gradient are calculated by using the finite difference of first order bias;

Step3: Non-maximum suppression of gradient amplitude values

STEP4: Detecting and connecting edges with a dual-threshold algorithm

<span style= "color: #6600cc;" ><strong>i=rgb2gray (Imread (' 9.jpg ')); subplot (121); Imshow (I); title (' Original '); Bw=edge (I, ' canny '); subplot (122); Imshow (BW); title (' Canny Edge processing ');</strong></span>


<strong><span style= "FONT-SIZE:18PX;" ><span style= "color: #6600cc;" >clear all;close All;clc;img=rgb2gray (imread (' 9.jpg ')); subplot (241); Imshow (IMG); title (' Original ') [M N]=size (IMG); img =double (IMG); The first two steps of%%canny edge detection are relatively uncomplicated, so I directly call the system function with a percent-Gaussian filter w=fspecial (' Gaussian ', [5 5]); Img=imfilter (Img,w, ' Replicate '); subplot (242); Imshow (Uint8 (IMG)); title (' Gaussian filter ')%%sobel Edge detection w=fspecial (' Sobel '); Img_w=imfilter (Img,w, '      Replicate ');      % seek transverse edge w=w ';% transpose Img_h=imfilter (img,w, ' replicate ');        % Seeking vertical edge img=sqrt (img_w.^2+img_h.^2); % Note here is not a simple averaging, but a square sum in the root. I have been mistaken for a long time subplot (243), Imshow (IMG), title (' Sobel Edge detection ') percent of the following is a non-maximum suppression new_edge=zeros (m,n); for i=2:m-1 for j=2:        N-1 mx=img_w (I,J);                My=img_h (I,J);      If My~=0 O=atan (mx/my);        The normal radian of the edge is ElseIf my==0 && mx>0 O=PI/2;                    else O=-PI/2;      End%mx interpolation with My and IMG adds=get_coords (o); % edge pixel normal side of the two-point coordinate, interpolation needs m1=my*img (I+adds (2), J+adds (1)) + (mx-my) *img (I+adds (4), J+adds (3)); The number of pixels obtained after the interpolation, using this pixel and the current pixel to compare adds=get_coords (O+PI); The two-point coordinate obtained by the other side of the edge normals, the interpolation needs m2=my*img (I+adds (2), J+adds (1)) + (mx-my) *img   (I+adds (4), J+adds (3)); The other side of the interpolation is worth the pixels, the same and the current pixel comparison isbigger= (mx*img (I,J) >m1) * (mx*img (I,J) >=m2) + (mx*img (i,j) <m1) * (mx*img (I,J) &lt ; =m2);         % if the current point is larger than both sides of the point 1 if Isbigger New_edge (i,j) =img (I,J);     End Endendsubplot (244); Imshow (Uint8 (New_edge)); title (' Non-maximal inhibition ') The following is the lag threshold processing up=120;    % upper threshold value low=100;  % lower threshold set (0, ' Recursionlimit ', 10000); % set maximum recursion depth for i=1:m for j=1:n if New_edge (i,j) >up &&new_edge (i,j) ~=255% judgment on threshold New_edge (I,J) =            255;      New_edge=connect (New_edge,i,j,low); End Endendsubplot (245); Imshow (new_edge==255); title (' Lag threshold processing ') </span></span></strong><pre Name= "code" class= "plain" ><span style= "color: #009900;" >function nedge=connect (nedge,y,x,low)% connectivity analysis after seed positioning neighbour=[-1-1;-1 0;-1 1;0-1; 0  1;1-1;1 0;1 1];    % eight connected search [M n]=size (Nedge);        For K=1:8 Yy=y+neighbour (k,1);        Xx=x+neighbour (k,2); If Yy>=1 &&yy<=m &&xx>=1 && xx<=n if Nedge (yy,xx) >=low && nedg                E (yy,xx) ~=255% of the threshold value Nedge (yy,xx) = 255;            Nedge=connect (Nedge,yy,xx,low); End End End End</span>

<span style= "color: #3333ff;" >function re=get_coords (angle)       %angle is the angle of the edge normals and returns two points before and after the normals    sigma=0.000000001;    X1=ceil (cos (ANGLE+PI/8) *sqrt (2) -0.5-sigma);    Y1=ceil (-sin (ANGLE-PI/8) *sqrt (2) -0.5-sigma);    X2=ceil (cos (ANGLE-PI/8) *sqrt (2) -0.5-sigma);    Y2=ceil (-sin (ANGLE-PI/8) *sqrt (2) -0.5-sigma);    Re=[x1 y1 x2 y2];end</span>




Canny Edge detection algorithm

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.