java - 当线交叉时,opencv 轮廓检测似乎失败

标签 java opencv detection contour

我正在尝试检测一个矩形,当背景是平坦的独特颜色时,它可以完美地工作。但当它有交叉的图画或线条时……事情就变得奇怪了。我怎样才能检测到这个矩形?

这是我所做的: 我从相机获取图像,将其转换为灰色并对其进行模糊处理。结果如下:

enter image description here

如您所见,我的论文位于沙发上一个非常不同的背景上。

现在我给它传递一个精明的过滤器:

Imgproc.Canny(mGrayMat, mGrayMat, 75, 200);

enter image description here

完美。我希望这是 OpenCV 查找轮廓并获取矩形的最简单的图像。但事实并非如此……线交叉似乎是问题所在,因为如果我将它从那里移到沙发上的另一个地方,它就可以正常工作。

我错过了什么吗?为什么这个矩形没有被检测到?

这是我的轮廓检测代码:

Imgproc.FindContours(inputMat, mContourList, mHierarchy, Imgproc.RetrExternal, Imgproc.ChainApproxSimple);
findQuadrilateral(findGreatestContourByArea(mContourList.ToList(), 10, 0));

static List<MatOfPoint> findGreatestContourByArea(List<MatOfPoint> points, int minRows, int minCols)
        {
            if (points == null || points.Count == 0)
            {
                return null;
            }

            MatOfPoint pointsResult = null;
            MatOfPoint matOfPoint = null;
            double area = 0;

            for (int i=0; i<points.Count; i++)
            {
                matOfPoint = points[i];
                if (matOfPoint == null)
                    continue;

                if (matOfPoint.Rows() < minRows)
                {
                    continue;
                }

                if (matOfPoint.Cols() < minCols)
                {
                    continue;
                }

                double areaTmp = Imgproc.ContourArea(matOfPoint);
                if (areaTmp > area)
                {
                    pointsResult = matOfPoint;
                    area = areaTmp;
                }
            }
            if (pointsResult == null)
                return null;

            List<MatOfPoint> result = new List<MatOfPoint>();
            result.Add(pointsResult);
            return result;
        }
        
        private static Quadrilateral findQuadrilateral(IList<MatOfPoint> mContourList)
        {
            MatOfPoint2f c2f = new MatOfPoint2f();
            for (int i = 0; i < mContourList.Count && i < 2; i++)
            {
                c2f.FromList(mContourList[i].ToList()); 
                // double peri = Imgproc.ArcLength(c2f, true);
                MatOfPoint2f approx = new MatOfPoint2f();

                // l.Count * 0.05
                // 0.02 * Imgproc.ArcLength(c2f, true)
                Imgproc.ApproxPolyDP(c2f, approx, 0.02 * Imgproc.ArcLength(c2f, true), true);

                // select biggest 4 angles polygon
                if (approx.Rows() == 4)
                {

                    return new Quadrilateral(approx);
                }
            }
            return null;
        }
        

它什么也没返回...有什么想法吗?

作为引用,我在这里留下了另一张具有完全相同问题的图像。 灰色的 enter image description here 还有精明的 enter image description here

最佳答案

修改代码以适用于您发布的图像的最简单方法是在使用 FindContour 之前自动设置图像阈值(例如使用 Otsu)。

 Imgproc.threshold(inputGray, outputBinary, threshold, 255, Imgproc.THRESH_OTSU);

您将得到与此类似的结果:

enter image description here

但为了提高鲁棒性,您需要使用一些分段检测方法并根据某些条件搜索矩形。

关于java - 当线交叉时,opencv 轮廓检测似乎失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59461106/

相关文章:

c++ - openCV 错误 : undefined reference to `cvLoadImage' Ubuntu

java - OpenCV 检测最大矩形产生令人费解的结果

ios - OpenCV检测一张纸

c++ - Tesseract 虚假空间识别

python - 从列表项中找到最大值后如何获取二维列表的索引

python - 行人检测与 HOG 描述符和 SVM 理解 Python 代码

java - 不寻常的通用语法 : Arrays. <String>asList(...)

java - 使用 Collections.sort 进行比较

java - isPrime 算法在输入大小 n 方面的渐近运行时间

java - 如何使用解析从字符串中分离 Int 和字符(java)