我尝试在图像上绘制凸包。为此,我找到所有轮廓并选择最大区域轮廓。drawContours
接受MatOfPoint
但convexHull
给我MatOfInt
。我读了这个问题并运行了一些代码。他们中的一些人绘制了凸包,但他们找不到真正的凸点。
我使用下面的代码,但它在最后一行出现运行时错误m.fromArray(hullpoints.get(i));
。hullpoints.get(i)
只有一点,代码无法创建MatOfPoint
对象。如何转换为MatOfPoint
from MatOfInt
?
// 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);
}