我正在尝试检测一个矩形,当背景是平坦的独特颜色时,它可以完美地工作。但当它有交叉的图画或线条时……事情就变得奇怪了。我怎样才能检测到这个矩形?
这是我所做的: 我从相机获取图像,将其转换为灰色并对其进行模糊处理。结果如下:
如您所见,我的论文位于沙发上一个非常不同的背景上。
现在我给它传递一个精明的过滤器:
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;
}
它什么也没返回...有什么想法吗?
最佳答案
修改代码以适用于您发布的图像的最简单方法是在使用 FindContour 之前自动设置图像阈值(例如使用 Otsu)。
Imgproc.threshold(inputGray, outputBinary, threshold, 255, Imgproc.THRESH_OTSU);
您将得到与此类似的结果:
但为了提高鲁棒性,您需要使用一些分段检测方法并根据某些条件搜索矩形。
关于java - 当线交叉时,opencv 轮廓检测似乎失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59461106/