opencv轮廓线交叉时似乎失败

问题描述 投票:1回答:1

我正在尝试检测一个矩形,当背景是扁平的独特颜色时,它可以完美地工作。但是,当它的图纸或线条交叉时,事情就会变得很奇怪。如何检测此矩形?

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

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

java opencv detection contour
1个回答
0
投票

修改代码以使其适合发布的图像的最简单方法是在使用FindContour(例如使用Otsu)之前将图像自动阈​​值化。

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

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

enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.