OpenCV-Canny边缘检测无法正常工作 [英] OpenCV - canny edge detection not working properly

查看:173
本文介绍了OpenCV-Canny边缘检测无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的OpenCV的新手.我目前正在研究文件检测演示应用程序.到目前为止,我所做的如下:

I am new to OpenCV with Android. I am currently working on document detection demo app. What I did so far is as below:

原始图像-> 灰度图像-> GaussianBlur -> Canny边缘检测-> 找到轮廓-> 绘制轮廓

Original image -> Gray scale image -> GaussianBlur -> Canny edge detection -> finding contours -> draw Contours

如下面的图片所示,我能够完美地检测出纸张.

I am able to detect paper sheet perfectly as you can see in below image .

但是它无法检测到某些文档.下面是其中之一

But it does not detect some documents. Below is one of them

我对此进行了很多研究,发现问题出在Canny边缘检测上,下面是Canny图像:

I researched a lot about that and found that the problem lies in canny edge detection and below is the canny image :

如您所见,边缘检测未完全链接,并且在某些点上未连接边缘.这就是这样做的原因.

As you can see , the edge detection is not perfectly linked and it does not connect edges at some point. That is the reason for that .

我在以下位置经历过类似的问题:

I have gone through the similar issue at : How to select the best set of parameters in Canny Edge detection algorithm implemented in OpenCV? I have followed the solution but it didn't worked for me.

我的canny检测代码如下:

My canny detection code is as follow :

double otsu_thresold = Imgproc.threshold(mGray,edged,0,255, Imgproc.THRESH_OTSU);
Imgproc.Canny(mRgba, edged, otsu_thresold*0.5,otsu_thresold);

我不知道我哪里错了!我该怎么做才能完美地检测文档?

I don't know where I am wrong! What should I do to perfectly detect the document?

推荐答案

首先,必须更改执行 Canny边缘检测的方法.您正在cv2.Canny() 手动中设置较高和较低的阈值.您可以自动执行此操作.我使用了此链接作为参考.

First the method of performing Canny edge detection has to change. You are setting a lower and higher threshold in cv2.Canny()manually. You can do this automatically. I used THIS LINK as reference.

使用以下代码段:

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)

那我在这里做什么?

So what am I doing here?

我正在获取灰度图像的中位数值.选择sigma值0.33来设置lowerupper阈值.统计人员通常将 0.33 值用于数据科学.所以在这里也考虑.

I am taking the median value of the gray scale image. The sigma value of 0.33 is chosen to set the lower and upper threshold. 0.33 value is generally used by statisticians for data science. So it is considered here as well.

这就是我得到的:

为了增强边缘信息,我使用cv2.MORPH_CROSS内核执行了形态学扩张:

To enhance this edge information I performed morphological dilation using the cv2.MORPH_CROSS kernel:

现在只需执行通常的cv2.findContours()操作并绘制最大轮廓即可.

Now just perform the usual cv2.findContours() operation and draw the biggest contour.

:)

这篇关于OpenCV-Canny边缘检测无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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