2012-04-02 155 views
7

我想在不调用Matlab中的Canny函数的情况下执行canny边缘检测器。我为高斯滤波器(sigma = 1)和非最大抑制写了几个函数。原始图像和所得到的图像显示..不知道的错误...MATLAB中的Canny边缘检测器

原始图像是什么

enter image description here

输出我得到的是

enter image description here

我已附上密码:

%% Read in 
I = imread('fruit.jpg'); 
figure(1),imshow(I) 
I = double(I); 
%% Determine Mask Size 
sigma = 2; 
w = mask_size(sigma); 
%% Gaussian Smoothing Filter 
[ G,sum ] = gauss_mask(w,sigma); 
%% Convolve 
I1 = (1/sum) * image_convolution(I,w,G); 
figure(2),imshow(I1); 
%% Ix(derivative in x-direction) 
Ix= delx(I1); 
figure(3),imshow(Ix); 
%% Iy(derivative in y-direction) 
Iy= dely(I1); 
figure(4),imshow(Iy); 
%% Gradient Magnitude 
If = grad_mag(Ix,Iy); 
figure(5),imshow(If); 
%% Non-maxmimum suppression 
It = suppression(If,abs(Ix),abs(Iy)); 
figure(6),imshow(It); 



function [ G,sum ] = gauss_mask(w,sigma) 
min = 1; 
m = floor(w/2); 
sum = 0; 
for x = 1: w 
    for y = 1:w 
     g = x-m-1; 
     h = y-m-1; 
     k = -(g^2 +h^2)/(2*sigma^2); 
     G(x,y) = exp(k); 
     sum = sum + G(x,y); 
     if min > G(x,y) 
      min = G(x,y); 
     end 
    end 
end 
B=1/min; 
G= B * G; 
G = round(G); 
end 


function [ I2 ] = image_convolution(I,w,G) 
m= (w-1)/2; 
N= size(I,1); 
M=size(I,2); 
for i=1:N 
    for j=1:M 
     if (i > N-m-1 || j > M-m-1 || i<m+1 || j <m+1) 
      I2(i,j) = 0; 
      continue; 
     end 
     sum1 = 0; 
     for u=1:w 
      for v=1:w 
       sum1 = sum1+I(i+u-m-1,j+v-m-1)*G(u,v); 
      end 
     end 
     I2(i,j)=sum1; 
    end 
end 
end 


function [ Ix ] = delx(image) 
mask = [-1 0 1; -2 0 2; -1 0 1]; 
Ix =image_convolution(image,3,mask); 
end 

function [ Iy ] = dely(image) 
mask = [-1 -2 -1;0 0 0;1 2 1]; 
Iy =image_convolution(image,3,mask); 
end 

function [ Imag ] = grad_mag(Ix,Iy) 
m=size(Ix,1); 
n=size(Ix,2); 
for i=1:m 
    for j=1:n 
      Imag(i,j) =sqrt(Ix(i,j)^2 + Iy(i,j)^2); 
    end 
end 
end 

function [ It ] = suppression(If,Ix,Iy) 
m=size(Ix,1); 
n=size(Ix,2); 
for i = 1:m 
    for j=1:n 
      if (j == 1 || j == n || i == 1 || j == n) 
       It(i,j) = 0; 
      else if (Ix(i,j)*Iy(i,j)> 0) 
       f1 =If(i-1,j-1); 
       f2 =If(i,j); 
       f3 =If(i+1,j+1); 
       It(i,j) = thinning(f1,f2,f3); 
       else if(Ix(i,j)*Iy(i,j)< 0) 
        f1 =If(i+1,j-1); 
        f2 =If(i,j); 
        f3 =If(i-1,j+1); 
        It(i,j) = thinning(f1,f2,f3); 
        else if(abs(Ix(i,j))-abs(Iy(i,j))>5) 
          f1 =If(i-1,j); 
          f2 =If(i,j); 
          f3 =If(i+1,j); 
          It(i,j) = thinning(f1,f2,f3); 
          else if(abs(Iy(i,j))-abs(Ix(i,j)) > 5) 
           f1 =If(i,j-1); 
           f2 =If(i,j); 
           f3 =If(i,j+1); 
           It(i,j) = thinning(f1,f2,f3); 
           end 
         end 
        end 
       end 
      end 
    end 
end 

end 

function [ w ] = thinning(f1,f2,f3) 
if(f2>f1 && f2>f3) 
    w =1; 
else 
    w= 0; 
end 
end 

function sz = mask_size(sigma) 
sz = floor(6*sigma) + 1; 
end 

有很多噪音......我该如何解决这个错误?我需要一些帮助....

+0

您是否得到相同的结果,其σ值更高?另外,你的函数'mask_size'是如何工作的?使用(6 * sigma + 1)的掩码大小通常是一个好主意。 – 2012-04-02 22:57:38

+0

你的门槛在哪里?你需要低和高的门槛 – hmfarimani 2016-10-17 07:44:04

回答

4

错误实际上是在细化功能。

if(f2>f1 && f2>f3) 
     w =f2; 
    else  
     w= 0; 
3

你应该这样做既:

  1. Smooth图像消除噪音(conv with a gaussian matrix)之前做任何操作。
  2. 采取了较高的门槛在算法的Hysteresis部分:

以更大的T1,当你做算法的这一部分:

Define two thresholds T1 > T2 

for every pixel with value greater than T1 is presumed to be an edge pixel. 
+0

我已经用高斯矩阵平滑了图像...你怎么做休闲部分?在非最大抑制之前还是之后做? – lakesh 2012-04-02 20:56:04

+0

添加了一个链接滞后,希望它是足够的 – 0x90 2012-04-02 21:21:56

+0

非最大抑压总是先做 – vini 2012-04-13 17:50:19

0

你的问题是在thresholding添加一个强大的thresholder摆脱假边缘。

首先,您必须使用高斯函数来平滑图像。然后找到输入图像的梯度和大小。执行非最大值抑制。之后做滞后阈值。

0

看到你的输出边缘图像,我可以说,你应该检查..

  1. 迟滞功能是否工作正常与否
  2. 您可能需要很高的门槛高一点
  3. 可以平滑图像再多一点。