For starters, I'm trying to draw contours and convexhull on matrix in OpenCV Android. I followed these codes to my program shown like this:
/// Start contourImg
Log.i(TAG, "called contourImg");
//init
List<MatOfInt> hull = new ArrayList<MatOfInt>();
List<Point[]> hullPoints = new ArrayList<Point[]>();
List<MatOfPoint> hullMOP = new ArrayList<MatOfPoint>();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat overlay = input.clone();
Mat hierarchy = new Mat(mask.rows(), mask.cols(), CvType.CV_8UC1, new Scalar(0));
Point titik1 = new Point(0,0);
Imgproc.findContours(mask, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, titik1);
//Find the convex hull
for (int i = 0; i < contours.size(); i++) {
hull.add(new MatOfInt());
}
for (int i = 0; i < contours.size(); i++) {
Imgproc.convexHull(contours.get(i), hull.get(i), false);
}
// Convert MatOfInt to MatOfPoint for drawing convex hull
// Loop over all contours
for (int i = 0; i < contours.size(); i++) {
Point[] points = new Point[hull.get(i).rows()];
// Loop over all points that need to be hulled in current contour
for (int j = 0; j < hull.get(i).rows(); j++) {
int index = (int) hull.get(i).get(j, 0)[0];
points[j] = new Point(contours.get(i).get(index, 0)[0], contours.get(i).get(index, 0)[1]);
}
hullPoints.add(points);
}
// Convert Point arrays into MatOfPoint
for (int i = 0; i < hullPoints.size(); i++) {
MatOfPoint mop = new MatOfPoint();
mop.fromArray(hullPoints.get(i));
hullMOP.add(mop);
}
// Draw contours + hull results
for (int i = 0; i < contours.size(); i++) {
Imgproc.drawContours(overlay, contours, i, green);
Imgproc.drawContours(overlay, hullMOP, i, red);
}
Basically, the output will draw the obtained contours and convexhulls in green and red respectively. Unfortunately, the drawing becomes too absurd that small contours and convexhulls are being drawn as well.
Edit: this is image example. Notice the smaller red and green regions those are also appeared, while I want them not to appear.
The question is: how should I manipulate the hullMOP
arraylist so that I get the index pointing to MatOfPoint
with only consists of largest contours and convexhull? I tried to apply the same thinking as Imgproc.boundingRect()
method by try finding the largest area (as follows):
for (int i = 0; i < contours.size(); i++) {
boundRect[i] = Imgproc.boundingRect(polyMOP.get(i));
}
Rect bigRect = new Rect();
for (int i = 0; i < boundRect.length; i++) {
if (bigRect.area() < boundRect[i].area()) {
bigRect = boundRect[i];
}
}
Core.rectangle(image3, bigRect.tl(), bigRect.br(), green, 2);
, but I can't get any hit. Any advice will help. Thanks before.