Extract the source code of edge Matlab by using the Canny operator

Source: Internet
Author: User
Function E = canny_edge (I, SIGMA) % functione = edge, sigma, output as edge image E [M, N] = size (I); RR = m-1; CC = 2: n-1; E = repmat (logical (uint8 (0), m, n); % generates edge image e of the same size, initialized to 1, that is, initialized edge gaussiandieoff =-0.001; % set the Gaussian function disappearance threshold percentofpixelsnotedges =-7; % used to calculate the edge threshold thresholdratio =-4; % set the ratio of two thresholds % first design the Gaussian filter and its differential PW =; % set the filter width SSQ = Sigma * sigma; % calculate the variance width = max (find (exp (-(PW. * PW)/(2 * Sigma * sigma)> gaussiandieoff); % calculate the filter operator width T = (-width: width); Len = 2 * width + 1; t3 = [T -. 5; t +. 5]; % average Gau = sum (exp (-(T3. * T3)/(2 * SSQ) for the values at the left and right sides of each pixel ))). '/(6 * pI * SSQ); % One-dimensional Gaussian filter dgau = (-t. * exp (-(T. * T)/(2 * SSQ)/SSQ ). '; % differential Ra of Gaussian filter = size (I, 1); CA = size (I, 2); ay = 255 * double (I ); ax = 255 * double (I '); H = Conv (Gau, dgau ); % combine image filtering using Gaussian Functions and first-order differentiation of Gaussian operators into an operator AX = conv2 (ax, H, 'same '). '; % generates X direction filter ay = conv2 (Ay, H, 'same'); % generates y direction filter mag = SQRT (ax. * Ax) + (Ay. * Ay); % calculate the range of the filtering result magmax = max (MAG (:); If magmax> 0mag = MAG/magmax; % normalization of the filtering amplitude end % the filtering threshold [counts, X] = imhist (MAG, 64) is calculated based on the probability density of the filtering amplitude ); % The histogram for calculating the amplitude of the filter result is highthresh = min (find (cumsum (counts)> percentofpixelsnotedges * m * N)/64; % determine the high threshold lowthresh = thresholdratio * highthresh by setting the ratio of non-edge points; % set the low threshold to high threshold multiplied by the proportion factor thresh = [lowthresh, highthresh]; % points with non-maximum suppression % greater than the high threshold are attributed to those with strong edge images % smaller than the low threshold are attributed to weak edge images idxstrong = []; for dir = idxlocalmax = cannyfindlocalmaxima (Dir, ax, ay, MAG); idxweak = idxlocalmax (MAG (idxlocalmax)> lowthresh); E (idxweak) = 1; idxstrong = [idxstrong; idxweak (MAG (idxweak)> highthresh)]; endrstrong = REM (idxStrong-1, m) + 1; % REM is used to evaluate the remainder cstrong = floor (idxStrong-1) /m) + 1; % Round-∞ integer e = bwselect (E, cstrong, rstrong, 8); % connect the edges of the two images using morphological operators

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.