如何获得轮廓分明的边缘,而与颜色无关 [英] How to get well-defined edges regardless of the color

查看:251
本文介绍了如何获得轮廓分明的边缘,而与颜色无关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试开发一个使用Android Camera来检测万事达卡,签证,客户卡等"卡的应用程序,为此,我使用了OpenCV4Android版本3.0.0.为了完成此任务,我做了以下事情:

I am trying to develop an App that detects cards "master cards, visa, cutomer cards, etc" using Android Camera, for that purpose i used OpenCV4Android version 3.0.0. To achieve this task, i did the following:

1-使用

Imgproc.cvtColor(this.mMatInputFrame, this.mMatGray, Imgproc.COLOR_BGR2GRAY);

2-使用

Imgproc.blur(this.mMatGray, this.mMatEdges, new Size(7, 7));

3-如下应用Canny边缘检测器

3- apply Canny edge detector as follows

Imgproc.Canny(this.mMatEdges, this.mMatEdges, 2, 900, 7, true);

4-要在真实图像上显示Canny的结果,我做了以下

4- to show Canny's result on the real image, i did the following

this.mDest = new Mat(new Size(this.mMatInputFrame.width(), this.mMatInputFrame.height()), CvType.CV_8U, Scalar.all(0));
this.mMatInputFrame.copyTo(this.mDest, this.mMatEdges);

5-使用

dilated = new Mat();
Mat dilateElement = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE, new Size(3, 3));
Imgproc.dilate(mMatEdges, dilated, dilateElement);

6-找到检测到的卡的轮廓,如下所示:

6- finding the contour of the card detected as follows:

ArrayList<MatOfPoint> contours = new ArrayList<>();
 hierachy = new Mat();
Imgproc.findContours(dilated, contours, hierachy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);

for (int i = 0; i < contours.size(); i++) {
    if (Imgproc.contourArea(contours.get(i), true) > 90000) {
        Rect rect = Imgproc.boundingRect(contours.get(i));

        if (rect.height > 60) {
            Imgproc.rectangle(mMatInputFrame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0));
            }
    }
}

当我运行该应用程序时,

When I run the App,

案例1

如果要检测的卡具有相同的颜色整张卡都涂有相同的颜色",则Canny会产生边界清晰的边缘,可以很容易地检测出该边缘,如图像"same-color-0"和"same-color"所示-color-1". 此外,当我将同色的卡片放在桌子上并在周围移动相机时,即使我在移动相机,也可以正确检测到边缘.或换句话说,红色框包围 该卡始终固定在边缘,永远不会消失

if the card to be detected is of a homogenous color "the entire card is painted with the same color", Canny produces well defined edges which can easily detected as shown in the image "same-color-0" and "same-color-1". moreover, when i place the card that of a homogenous color on a table and move the camera around it, the edges are getting detected properly despite i am moving the camera. or in other words, the red frame that surrounds the edges of the card is always fixed around the edges and never disappears

案例2

如果卡不是混合颜色"的同色,则边缘检测很差,如图像"mixed-color-0"和"mixed-color-1"所示,此外,红色卡周围的边框经常消失. 从该情况扩展出的另一种情况是,当卡为两种颜色时,一种是浅色而一种是深色,在这种情况下,边缘检测器仅检测到卡中的深色部分,因为其边缘定义良好,如图中所示. "mixed-color-2"

if the card is not of a homogenous color "of a mixed colors", then the edge detection is bad as shown in image "mixed-color-0" and "mixed-color-1", and moreover, the red frame that surrounds the edges of the card disappears so often. Another case extended from this case is, when the card is of two colors, one is light and one is dark, in this case, the edge detector detects only the dark part in the card because its edges are well defined as shown in image "mixed-color-2"

请让我知道如何获得清晰定义和卡片大小的边缘,而不管颜色如何? 还有其他更准确的边缘检测方法吗?

Please let me know how to get well defined and card-sized edges of the cards regardless of the color? is there any other more accurate way for edge detection?

相同颜色0 :

相同颜色1

混合色0

mixed-color-1

混合色2

原始图片:

推荐答案

您可以使用这是我的另一个答案中,我通过运行C ++代码获得了这些结果.对我来说,这似乎是一个很好的结果.

I got these results running the C++ code in this my other answer. This seems like a good and robust result to me.

要在Java中使用此功能,您应该知道contrib模块ximgproc中的 Structured Edge Detection . 您可能需要重新编译OpenCV才能使用它:使用contrib模块和Java包装器

To use this in Java, you should know that Structured Edge Detection is in contrib module ximgproc. You probably need to recompile OpenCV to use it: Build OpenCV with contrib modules and Java wrapper

这篇关于如何获得轮廓分明的边缘,而与颜色无关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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