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