【发布时间】:2015-06-01 16:36:45
【问题描述】:
我尝试在图像上绘制凸包。为此,我找到所有轮廓并选择最大区域轮廓。 drawContours 接受MatOfPoint 但convexHull 给了我MatOfInt。我读了这个question 并运行了一些代码。他们中的一些人绘制了凸包,但他们找不到真正的凸点。
我使用下面的代码,但在最后一行 m.fromArray(hullpoints.get(i)); 上有一个运行时错误。 hullpoints.get(i) 只有一个点,代码无法创建MatOfPoint 对象。如何从 MatOfInt 转换为 MatOfPoint ?
// Find the convex hull
List<MatOfInt> hull = new ArrayList<MatOfInt>();
for(int j=0; j < contours.size(); j++){
hull.add(new MatOfInt());
}
for(int j=0; j < contours.size(); j++){
Imgproc.convexHull(contours.get(j), hull.get(j));
}
// Convert MatOfInt to MatOfPoint for drawing convex hull
// Loop over all contours
List<Point[]> hullpoints = new ArrayList<Point[]>();
for(int j=0; j < hull.size(); j++){
Point[] points = new Point[hull.get(j).rows()];
// Loop over all points that need to be hulled in current contour
for(int k=0; k < hull.get(j).rows(); k++){
int index2 = (int)hull.get(j).get(k, 0)[0];
points[k] = new Point(contours.get(j).get(index2, 0)[0], contours.get(j).get(index2, 0)[1]);
}
hullpoints.add(points);
}
// Convert Point arrays into MatOfPoint
List<MatOfPoint> hullmop = new ArrayList<MatOfPoint>();
for(int j=0; j < hullpoints.size(); j++){
MatOfPoint m = new MatOfPoint();
m.fromArray(hullpoints.get(i));
hullmop.add(m);
}
【问题讨论】:
-
我的索引错误。我不会删除这个问题来指导在 android 应用程序上绘制凸包。替换
hullpoints.get(i)hullpoints.get(j) -
contours和hull的Class类型是什么?Imgproc是什么?
标签: android opencv convex-hull opencv-contour