gpt4 book ai didi

c++ - 内核大小 > 7 的 Canny 边缘检测器

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:38 24 4
gpt4 key购买 nike

为什么不能在内核大小大于 7 的 OpenCV 中执行 Canny 边缘检测?

例如,

// This works
cv::Canny(src_image, out_edges, th1, 2 * th1, 3);
cv::Canny(src_image, out_edges, th1, 2 * th1, 7);

// This raises an exception
cv::Canny(src_image, out_edges, th1, 2 * th1, 9);

我知道 Sobel 只采用 1、3、5 或 7 的内核大小。但我看到论文使用的内核大小为 9。这在 OpenCV 中不可能吗?

* 编辑 *

我一直在编辑 Canny 代码以支持更大的内核。问题出在这里(canny.cpp):

if (L2gradient)
{
low_thresh = std::min(32767.0, low_thresh);
high_thresh = std::min(32767.0, high_thresh);
if (low_thresh > 0) low_thresh *= low_thresh;
if (high_thresh > 0) high_thresh *= high_thresh;
}

if (L2gradient)
{
low_thresh = std::min(32767.0, low_thresh);
high_thresh = std::min(32767.0, high_thresh);

if (low_thresh > 0) low_thresh *= low_thresh;
if (high_thresh > 0) high_thresh *= high_thresh;
}

我猜这与他们使用了一些移位操作有关,

#define CANNY_SHIFT 15
const int TG22 = (int)(0.4142135623730950488016887242097*(1<<CANNY_SHIFT) + 0.5);

因此将精度限制为 16 位,因此限制为 32767...。我该如何解决这个问题?

最佳答案

看看 OpenCV 文档:

对于 Canny :

apertureSize: aperture size for the Sobel operator.

对于 Sobel :

ksize: size of the extended Sobel kernel; it must be 1, 3, 5, or 7.

因此 Canny 中的孔径大小受限于 Sobel 内核大小。

这在 source code 中得到验证:

 if ((aperture_size & 1) == 0 || (aperture_size != -1 && (aperture_size < 3 || aperture_size > 7)))
CV_Error(CV_StsBadFlag, "Aperture size should be odd");

因此,除非您自己重写一些代码,否则无法使用具有更大孔径的 Canny。您可以使用 filter2d 应用您的自定义 large sobel 过滤器 ,然后对 Canny 非最大值抑制进行编码。

然而,在实践中很少使用掩模大于 3x3 的 Sobel。

关于c++ - 内核大小 > 7 的 Canny 边缘检测器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com