非最大抑制(细化算法)

时间:2015-08-02 20:49:13

标签: matlab image-processing edge-detection non-maximum-suppression

我在应用这种细化算法时遇到问题,我不知道提供的代码有什么问题。输出图像几乎与阈值图像相同。 thresholded Image After thinning

  

nonMaxSupp:

 %this function returns a boolean matrix to be multiplied with thresholded image(element wise)
 function [ marks ] = nonMaxSupp( mag,dir )
    [m,n]=size(mag);
    marks=uint8(ones(size(mag)));
    for i=2:m-1
        for j=2:n-1
            if mag(i,j) ~=0
               angle=dir(i,j)+180;
               if (angle>=340 || angle<=22.5) || (angle>=157.5 && angle<=202.5)  %horizontal
                if (mag(i,j+1)>mag(i,j)) || (mag(i,j-1)>mag(i,j))
                    marks(i,j)=0;
                end

            elseif (angle>22.5 && angle<=67.5) || (angle>202.5 && angle<=247.5) %45
                 if (mag(i+1,j+1)>mag(i,j)) || (mag(i-1,j-1)>mag(i,j))
                    marks(i,j)=0;
                 end
            elseif (angle>67.5 && angle<=112.5) || (angle>247.5 && angle<=292.5) %vertical
                 if (mag(i-1,j)>mag(i,j)) ||(mag(i+1,j)>mag(i,j))
                    marks(i,j)=0;
                end
            else %135
                 if (mag(i-1,j+1)>mag(i,j)) || (mag(i+1,j-1)>mag(i,j))
                    marks(i,j)=0;
                 end
            end
        end
    end 
end

end
  

梯度:

function [ mag,dir ] = gradient(I,h1,h2)
[m,n]=size(I);
mag=I;
dir=zeros(size(I));
for i=2:m-1
    for j=2:n-1
        x=convolution(h2,I(i-1:i+1 ,j-1:j+1));
        y=convolution(h1,I(i-1:i+1 ,j-1:j+1));
        mag(i,j)=sqrt(x^2 +y^2);
        dir(i,j)=(180*atan2(y,x)/pi);

    end
end
end

0 个答案:

没有答案