I try to draw convex hull on image.For this, I find all contours and select max area contour. drawContours
accepts MatOfPoint
but convexHull
gives me MatOfInt
. I read this question and run some codes. Some of them draw convex hull, but they cannot find really convex points.
I use below code, but it has a runtime error on last line m.fromArray(hullpoints.get(i));
. hullpoints.get(i)
has only one point and the code cannot create MatOfPoint
object. How can I convert to 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);
}