如何在openCV中实现的Canny Edge检测算法中选择最佳参数集?

Ank*_*tam 16 opencv image-processing edge-detection image-segmentation

我在Android平台上使用openCV.在这个社区和技术人员的大力帮助下,我能够成功地从图像中检测到一张纸.这些是我使用的步骤.

1.Imgproc.cvtColor()
 2.Imgproc.Canny()
 3.Imgproc.GausianBlur()
 4.Imgproc.findContours()
 5.Imgproc.approxPolyDP()
 6.findLargestRectangle()
 7.find the vertices of the rectangle
 8.find the vertices of the rectangle top-left anticlockwise order using center of mass approach
 9.find the height and width of the rectangle just to maintain the aspect ratio and do warpPerspective transformation.
Run Code Online (Sandbox Code Playgroud)

应用了所有这些步骤后,我可以轻松地从图像中获取文档或最大的矩形,但这在很大程度上取决于背景和文档表的强度差异.由于Canny边缘的工作原理是强度梯度,不同之处在于强度总是从实现方面假设.这就是为什么Canny将各种阈值参数考虑在内.

  1. 降低Thershold
  2. Heigher Thershold

因此,如果像素的强度梯度大于高阈值,则它将被添加为输出图像中的边缘像素.如果像素的强度梯度值低于下阈值,则像素将被完全拒绝.如果是像素在较低和较高阈值之间具有强度,如果它连接到具有大于较高阈值的值的任何其他像素,则它将仅被添加为边缘像素.

我的主要目的是使用Canny边缘检测进行文档扫描,因此我想知道如何动态计算这些阈值,以便它可以处理暗背景和浅背景两种情况.

我通过手动调整参数尝试了很多,但我找不到与场景关联的关系.

希望我明确我的观点,并提前致谢

Mai*_*mon 18

您可以使用Otsu方法计算阈值

(Python)代码如下所示:

high_thresh, thresh_im = cv2.threshold(im, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
lowThresh = 0.5*high_thresh
Run Code Online (Sandbox Code Playgroud)


Jer*_*uke 5

使用我从这个博客获得的以下片段:

v = np.median(gray_image)

#---- Apply automatic Canny edge detection using the computed median----
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(gray_image, lower, upper)
cv2.imshow('Edges',edged)
Run Code Online (Sandbox Code Playgroud)

那我在这里做什么?

我正在取灰度图像的中值。选择 0.33 的 sigma 值来设置上下阈值。0.33 值通常被统计学家用于数据科学。所以这里也考虑。

  • 在答案中提到了它 (3认同)