我正在使用MATLAB。我想用canny方法进行边缘检测。但我需要的是对角线的边缘或只有40至50度角的边缘。我怎样才能做到这一点?使用canny方法在一定程度上的边缘检测
回答
你需要自己编写canny边缘检测器的代码(你会得到很多的实现)在互联网上。然后,您将在第二步中计算梯度幅度和梯度方向。你需要过滤掉角度和相应的大小。
希望这可以帮助你。
我已经回答了一个类似的问题,关于如何使用Matlab的edge
函数来找到Canny的定向边(Orientational Canny Edge Detection),但我也想尝试一下由Avijit建议的自定义实现。
开始的图像,我会使用一个内置的演示图像。
A = im2double(rgb2gray(imread('peppers.png')));
高斯滤波器
A_filter = imgaussfilt(A);
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);
在该步骤中,我们通过取向传统仓边缘(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;
在剩余的边缘执行非最大抑制 - 这是为了适应不同的角度最困难的部分。要找到精确的边缘位置,可以比较两个相邻像素:对于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
边缘具迟滞的跟踪 - 确定弱边缘像素是否足够接近(我用一个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
这里是我的结果。
正如你所看到的,删除了其它边缘方位,因为检测到更少的强烈像素对滞后一步一个非常负面的影响。调整high_threshold会有所帮助。另一种选择是使用所有边缘方向执行步骤5和6,然后使用diagonal_map提取对角线边缘。
- 1. Canny边缘检测器C
- 2. 方向性Canny边缘检测
- 3. OpenCV的Canny边缘检测C++
- 4. java中的canny边缘检测器
- 5. Android的Canny边缘检测器
- 6. MATLAB中的Canny边缘检测器
- 7. Canny边缘检测器检测到所述图像的边界
- 8. 获取边缘检测后的边缘坐标(Canny)
- 9. 如何在Java中实现Canny边缘检测器和android studio
- 10. Android的Canny边缘检测器 - 递归函数的StackOverflow
- 11. 在Opencv中使用Canny边缘检测识别图像中的文本
- 12. Canny边缘检测 - 灰度图像始终显示为3通道,不可用?
- 13. 在Canny边缘算法中只需要一条边线
- 14. Canny边缘检测器 - 执行中的问题
- 15. Canny边缘VS骨架化
- 16. 如何测量边缘检测图像边缘的长度?
- 17. 如何灰度图像上使用边缘检测
- 18. SQLITE边缘检测
- 19. 边缘检测和透明度
- 20. 覆盖在图像上的canny边缘黑色
- 21. OpenCV从Canny边缘到轮廓
- 22. 使用matlab进行边缘检测normxcorr2
- 23. OpenCV中的边缘检测
- 24. C#中的边缘检测
- 25. 应用sobel边缘检测的预处理方法
- 26. 在iPhone上使用蒙版照片边缘检测
- 27. 如何提高边缘检测算法的精度?
- 28. 使用边缘检测的清除路径检测
- 29. c#GDI边缘空白检测算法
- 30. 边缘检测 - 检测不良
或者您可以旋转图片! :P –
我认为你正在寻找*“罗伯茨十字架”*卷积 - https://en.wikipedia.org/wiki/Roberts_cross –
我已经回答了类似的问题http://stackoverflow.com/questions/41020357 /取向-精明边缘检测/ 41291720#41291720 – Cecilia