我正在尝试检测一个矩形,当背景是扁平的独特颜色时,它可以完美地工作。但是,当它的图纸或线条交叉时,事情就会变得很奇怪。如何检测此矩形?
这是我所做的:我从相机获取图像,将其转换为灰色并使其模糊。结果如下:
如您所见,我的论文在沙发上的背景完全不同。
现在我通过了一个精巧的过滤器:
Imgproc.Canny(mGrayMat,mGrayMat,75,200);
完美。我希望这是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;
}
它什么也没返回...有什么想法吗?