如何在OpenCV中实现的Canny边缘检测算法中选择最佳的参数集? [英] How can I select the best set of parameters in the Canny edge detection algorithm implemented in OpenCV?

查看:138
本文介绍了如何在OpenCV中实现的Canny边缘检测算法中选择最佳的参数集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android平台上使用OpenCV.在这个社区和技术人员的巨大帮助下,我能够成功地从图像中检测出一张纸.

I am working with OpenCV on the Android platform. With the tremendous help from this community and techies, I am able to successfully detect a sheet out of the image.

这些是我使用的步骤.

  1. Imgproc.cvtColor()
  2. Imgproc.Canny()
  3. Imgproc.GausianBlur()
  4. Imgproc.findContours()
  5. Imgproc.approxPolyDP()
  6. findLargestRectangle()
  7. 找到矩形的顶点
  8. 使用质心方法找到左上角的矩形的顶点
  9. 找到矩形的高度和宽度以保持纵横比并进行warpPerspective变换.

应用所有这些步骤之后,我可以轻松地从图像中获取文档或最大的矩形.但这很大程度上取决于背景和文档纸强度的差异.由于 Canny边缘检测器是根据强度梯度原理工作的,因此始终假定强度存在差异从实施方面来看.这就是为什么Canny考虑了各种阈值参数的原因.

After applying all these steps I can easily get the document or the largest rectangle from an image. But it highly depends on the difference in the intensities of the background and the document sheet. As the Canny edge detector works on the principle of intensity gradient, a difference in intensity is always assumed from the implementation side. That is why Canny took into the account the various threshold parameters.

  1. 下限阈值
  2. 更高的门槛

因此,如果像素的强度梯度大于较高阈值,则它将作为边缘像素添加到输出图像中.如果像素的强度梯度值低于下限阈值,则将完全拒绝该像素.而且,如果像素的强度介于阈值上限和下限之间,则仅当它连接到值大于阈值上限的任何其他像素时,才将其添加为边缘像素.

So if the intensity gradient of a pixel is greater than the higher threshold, it will be added as an edge pixel in the output image. A pixel will be rejected completely if its intensity gradient value is lower than the lower threshold. And if a pixel has an intensity between the lower and higher threshold, it will only be added as an edge pixel if it is connected to any other pixel having the value larger than the higher threshold.

我的主要目的是使用Canny边缘检测进行文档扫描.那么,如何动态计算这些阈值,以使其可以在黑暗和明亮背景的两种情况下正常工作?

My main purpose is to use Canny edge detection for the document scanning. So how can I compute these thresholds dynamically so that it can work with the both cases of dark and light background?

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

I tried a lot by manually adjusting the parameters, but I couldn't find any relationship associated with the scenarios.

推荐答案

您可以使用大津的方法.

(Python)代码如下:

The (Python) code would look like this:

high_thresh, thresh_im = cv2.threshold(im, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
lowThresh = 0.5*high_thresh

这篇关于如何在OpenCV中实现的Canny边缘检测算法中选择最佳的参数集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆