这取决于允许的成本密集程度。一种简单的方法是:
(1) 使用 Sobel-Filters 对图像进行卷积(给出 Dx、Dy)。
对于每个精明的边缘像素:
(2) 归一化 (Dx, Dy), s.t.在每个像素中,您都有边缘的方向。
(3) 计算您想要移除的方向的内积(在您的情况下为 (0,1))。
(4) 如果内积的绝对值小于某个阈值,则移除该像素。
例子:
img = ...;
canny_img = ...;
removeDir = [0;1];
% convolute with sobel masks
sobelX = [1, 0, -1; 2, 0, -2; 1, 0, -1];
sobelY = sobelX';
DxImg = conv2(img,sobelX,'same');
DyImg = conv2(img,sobelY,'same');
% for each canny-edge-pixel:
for lin = 1:size(img,1) % <-> y
for col = 1:size(img,2) % <-> x
if canny_img(lin,col)
% normalize direction
normDir = [DxImg(lin,col); DyImg(lin,col)];
normDir = normDir / norm(normDir,2);
% inner product
innerP = normDir' * removeDir;
% remove edge?
if abs(innerP) < cos(45/180*pi) % 45° threshold
canny_img(lin,col) = 0;
end
end
end
end
您可以根据自己的要求对其进行大量优化。