2

我正在使用MATLAB。我想用canny方法进行边缘检测。但我需要的是对角线的边缘或只有40至50度角的边缘。我怎样才能做到这一点?使用canny方法在一定程度上的边缘检测

+0

或者您可以旋转图片! :P –

+0

我认为你正在寻找*“罗伯茨十字架”*卷积 - https://en.wikipedia.org/wiki/Roberts_cross –

+0

我已经回答了类似的问题http://stackoverflow.com/questions/41020357 /取向-精明边缘检测/ 41291720#41291720 – Cecilia

回答

2

你需要自己编写canny边缘检测器的代码(你会得到很多的实现)在互联网上。然后,您将在第二步中计算梯度幅度和梯度方向。你需要过滤掉角度和相应的大小。

希望这可以帮助你。

0

我已经回答了一个类似的问题,关于如何使用Matlab的edge函数来找到Canny的定向边(Orientational Canny Edge Detection),但我也想尝试一下由Avijit建议的自定义实现。

Canny Edge Detection steps

  1. 开始的图像,我会使用一个内置的演示图像。

    A = im2double(rgb2gray(imread('peppers.png'))); 
    
  2. 高斯滤波器

    A_filter = imgaussfilt(A); 
    
  3. Sobel Edge Detection - 我们不能使用内置的实现(edge(A_filter, 'Sobel')),因为我们想要的边缘角度,而不仅仅是边缘的位置,所以我们执行我们的运营商。

    a。卷积找到定向梯度

    %These filters measure the difference in values between vertically or horizontally adjacent pixels. 
    %Effectively, this finds vertical and horizontal gradients. 
    vertical_filter = [-1 0 1; -2 0 2; -1 0 1]; 
    horizontal_filter = [-1 -2 -1; 0 0 0; 1 2 1]; 
    A_vertical = conv2(A_filter, vertical_filter, 'same'); 
    A_horizontal = conv2(A_filter, horizontal_filter, 'same'); 
    

    b。计算角度

    A_angle = arctan(A_vertical./A_horizontal); 
    
  4. 在该步骤中,我们通过取向传统仓边缘(0°,45°,90°,135°),但由于只希望40幅50度之间的对角边缘,我们将保留这些边缘并丢弃其余部分。

    % I lowered the thresholds to include more pixels 
    % But for your original post, you would use 40 and 50 
    lower_angle_threshold = 22.5; 
    upper_angle_threshold = 67.5; 
    diagonal_map = zeros(size(A), 'logical'); 
    diagonal_map (A_angle>(lower_angle_threshold*pi/180) & A_angle<(upper_angle_threshold*pi/180)) = 1; 
    
  5. 在剩余的边缘执行非最大抑制 - 这是为了适应不同的角度最困难的部分。要找到精确的边缘位置,可以比较两个相邻像素:对于0°边缘,比较东西向,对于45°西南像素,对于东北像素,对于南北向为90°,对于东经135°,西向像素到东南像素。由于您所需的角度接近45°,因此我只是使用了西南方向,但是如果您想要10°到20°,则需要对这些比较进行更多的考虑。

    non_max = A_sobel; 
    [n_rows, n_col] = size(A); 
    %For every pixel 
    for row = 2:n_rows-1 
        for col = 2:n_col-1 
         %If we are at a diagonal edge 
         if(diagonal_map(row, col)) 
          %Compare north east and south west pixels 
          if(A_sobel(row, col)<A_sobel(row-1, col-1) || ... 
            A_sobel(row, col)<A_sobel(row+1, col+1)) 
           non_max(row, col) = 0; 
          end 
         else 
          non_max(row, col) = 0; 
         end 
        end 
    end 
    
  6. 边缘具迟滞的跟踪 - 确定弱边缘像素是否足够接近(我用一个3x3窗口),以强大的边缘像素。如果是,请将它们包含在边缘。如果不是,他们是噪音;删除它们。

    high_threshold = 0.5; %These thresholds are tunable parameters 
    low_threshold = 0.01; 
    
    weak_edge_pixels = non_max > low_threshold & non_max < high_threshold; 
    strong_edge_pixels = non_max > high_threshold; 
    
    final = strong_edge_pixels; 
    for row = 2:n_rows-1 
        for col = 2:n_col-1 
         window = strong_edge_pixels(row-1:row+1, col-1:col+1); 
         if(weak_edge_pixels(row, col) && any(window(:))) 
          final(row, col) = 1; 
         end 
        end 
    end 
    

这里是我的结果。

Result image

正如你所看到的,删除了其它边缘方位,因为检测到更少的强烈像素对滞后一步一个非常负面的影响。调整high_threshold会有所帮助。另一种选择是使用所有边缘方向执行步骤5和6,然后使用diagonal_map提取对角线边缘。