Canny Edge Detector - 实施上的麻烦

时间:2012-03-04 19:28:28

标签: matlab image-processing edge-detection

我正在尝试实现canny边缘检测器。我已经制作了一些代码来生成我认为应该在非最大值抑制阶段正确的代码,但是当我运行它时,我得到的图像只是显示轮廓,但不是我预期的结果。

我花了好几个小时试图解决它,但找不到我出错的地方。有人能指出我正确的方向吗?

% Set direction to either 0, 45, -45 or 90 depending on angle.
[x,y]=size(f1);
for i=1:x-1,
    for j=1:y-1,
        if ((gradAngle(i,j)>67.5 && gradAngle(i,j)<=90) || (gradAngle(i,j)>=-90 && gradAngle(i,j)<=-67.5)) 
            gradDirection(i,j)=0;
        elseif ((gradAngle(i,j)>22.5 && gradAngle(i,j)<=67.5))
            gradDirection(i,j)=45;
        elseif ((gradAngle(i,j)>-22.5 && gradAngle(i,j)<=22.5))
            gradDirection(i,j)=90;
        elseif ((gradAngle(i,j)>-67.5 && gradAngle(i,j)<=-22.5))
            gradDirection(i,j)=-45;
        end
    end
end

% Non-maxima suppression. 
% Compare to neighbours and set as 0 if smaller than either of them
for i=2:x-2,
    for j=2:y-2,
        if(gradDirection(i,j)==90)
           if (gradDirection(i,j)<(gradDirection(i,j-1) | gradDirection(i,j+1)))
               gradDirection(i,j)=0;
           end 
        end
        if(gradDirection(i,j)==45)
           if (gradDirection(i,j)<(gradDirection(i+1,j-1) | gradDirection(i-1,j+1)))
               gradDirection(i,j)=0;
           end 
        end
        if(gradDirection(i,j)==-45)
           if (gradDirection(i,j)<(gradDirection(i-1,j-1) | gradDirection(i+1,j+1)))
               gradDirection(i,j)=0;
           end 
        end
    end
end

1 个答案:

答案 0 :(得分:1)

我认为问题在于你在那里的按位OR:

 if (gradDirection(i,j)<(gradDirection(i,j-1) | gradDirection(i,j+1)))

我认为应该是这样的:

 if (gradDirection(i,j)<gradDirection(i,j-1) || gradDirection(i,j)<gradDirection(i,j+1))