画像に凸包を描画しようとしました。このために、すべての輪郭を見つけて、最大面積の輪郭を選択します。drawContours
受け入れますMatOfPoint
がconvexHull
、私に与えますMatOfInt
。この質問を読んで、いくつかのコードを実行します。それらのいくつかは凸包を描きますが、実際の凸点を見つけることはできません。
以下のコードを使用していますが、最後の行に実行時エラーがありますm.fromArray(hullpoints.get(i));
。hullpoints.get(i)
ポイントは 1 つしかなく、コードはMatOfPoint
オブジェクトを作成できません。MatOfPoint
からに変換するにはどうすればよい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);
}