资源简介

本人毕业设计所选的canny算子matlab源代码,绝对可用,第一次上传,选最好的

资源截图

代码片段和文件信息

function result=canny(IszT1T2)
% perform canny edge detector. I is the input image sz indicates the size
% of gaussian T1 is the lower threshold and T2 is the upper threshold.

% smooth the image with a gaussian filter
h=fspecial(‘gaussian‘szsz/6);
I=imfilter(Ih‘replicate‘);
I=double(I);

% choose priwitt to compute the gradient magnitude image g(xy)
GX=[-1 -1 -1; 0 0 0; 1 1 1]  ;% Gx
GY=[-1 0 1;-1 0 1; -1 0 1];   % Gy
I1=imfilter(IGX‘replicate‘);
I2=imfilter(IGY‘replicate‘);
g=sqrt(I1.^2+I2.^2);


[xy]=size(I); % get the size of I.
% compute the edge direction image theta(xy)
theta=zeros(xy);
for i=1:x
    for j=1:y
        if(I1(ij)==0 && I2(ij)==0)     % if both Gx and Gy are equal to 0 set the direction to be 0
            theta(ij)=0;
        else
            theta(ij)=atan(I2(ij)./I1(ij))*180/3.1415926; % get the direction in degrees.
        end
        % set the direction‘s range to be [0360]
       if(I1(ij)>=0&&I2(ij)<0)
           theta(ij)=theta(ij)+360;
       elseif(I1(ij)<0&&I2(ij)>=0)
           theta(ij)=theta(ij)+180;
       elseif(I1(ij)<0&&I2(ij)<0)
           theta(ij)=theta(ij)+180;
       end
       
    end
end

% quantize the angle to the nearest angle:{04590135180225270315360}
angle=(0:8)*45;
for i=1:x
    for j=1:y
        for k=1:9
            if(abs(theta(ij)-angle(k))<=45/2)
                theta(ij)=angle(k);
            end
        end
    end
end

% call localmax function to perform the nonmaximal supression
G=localmax(gtheta);
G=uint8(G);

G(find(G>T2*max(G(:))))=255; % change the pixels above upper threshold to be edges.
G

评论

共有 条评论