我可以消除这些jaggies'从Canny Edge检测?

发布于 2025-01-25 07:32:06 字数 994 浏览 1 评论 0原文

在我使用Canny Edge探测器(来自OpenCV)时,我遇到了一个烦人,在那里我会遇到一些锯齿状的边缘,在那里我更喜欢直的边缘。以下是基于对情况的简单蒸馏的示例。我的实际应用包括以外的其他角度,以外的其他角度,超出0/90/180/270度,但是每当线几乎平行于X/Y轴时,效果都非常相似。

我的输入图像通常在直线正交方向上有轻微的梯度,我认为这是问题的基本来源。我在OpenCV中进行了一些挖掘,并在OpenCV中进行了调试,并且大多说服了自己的问题是Canny算法的工作方式所固有的,而不是OpenCV的特殊实现,但我仍然没有完全放弃Canny。

我尝试了Arrayfire的精巧实现,并获得了类似但沃尔斯的结果,并且我尝试将模糊应用于边缘输出以使效果不那么明显,但结果仍然不是理想的。

from PIL import Image, ImageDraw
import numpy as np
import cv2 as cv

cross_img = Image.new('L', (500, 500))
d = ImageDraw.Draw(cross_img)
for i in range(500):
    c = 60 + i / 10
    d.line(((240, i), (260, i)), int(c))
    d.line(((i, 240), (i, 260)), int(c))
npimg = np.asarray(cross_img)
cannpimg = np.zeros_like(npimg)
cv.Canny(npimg, 200, 400, cannpimg)
canimg = Image.fromarray(cannpimg)

cross_img.save('/tmp/cross.png')
canimg.save('/tmp/can.png')

输入图像

In my application of a Canny edge detector (from OpenCV), I've run into an annoyance where I get slight jagged edges where I would prefer straight ones. Below is an example based on a simple distillation of the situation. My actual application includes lines at other angles than 0/90/180/270 degrees, but the effect is quite similar whenever lines are nearly parallel to the X/Y axes.

My input images often have a slight gradient in the direction orthogonal to the straight line, and I think this the basic source of the problem. I did some digging through code and debugging in OpenCV and have mostly convinced myself that the problem is inherent to the way that the Canny algorithm works, rather than OpenCV's particular implementation of it, but I still haven't completely given up on Canny.

I have tried Arrayfire's Canny implementation and got similar-but-worse results, and I've tried applying a blur to the edge output to make the effect less pronounced, but the results are still not ideal.

from PIL import Image, ImageDraw
import numpy as np
import cv2 as cv

cross_img = Image.new('L', (500, 500))
d = ImageDraw.Draw(cross_img)
for i in range(500):
    c = 60 + i / 10
    d.line(((240, i), (260, i)), int(c))
    d.line(((i, 240), (i, 260)), int(c))
npimg = np.asarray(cross_img)
cannpimg = np.zeros_like(npimg)
cv.Canny(npimg, 200, 400, cannpimg)
canimg = Image.fromarray(cannpimg)

cross_img.save('/tmp/cross.png')
canimg.save('/tmp/can.png')

Input image
Canny output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几度春秋 2025-02-01 07:32:06

您是对的,jaggies是由于Canny Edge检测处理的方式是由梯度定义的边缘的方式,该梯度朝着与上述边缘不同的方向。在运行Canny之前,您可以在图像上运行灰度转换,将所有梯度值挤压到白色和黑色值仍然是黑色的。

You're right, the jaggies are due to the way Canny Edge detection handles this case of an edge defined by a gradient which is in a different direction than said edge. You can run a gray level transform on the image before running canny, which squashes all gradient values to white and black values remain black.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文