Android findContours returning too many contours

asked 2016-09-30 15:48:06 -0500

marcosbontempo gravatar image

updated 2016-09-30 15:53:01 -0500

Hello,

I'm trying to find the contours of the camera frame in a Android app. Here is the code:

Imgproc.Canny(src, src, 50, 200);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
// find contours:
Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE);
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
    Imgproc.drawContours(src, contours, contourIdx, new Scalar(0, 0, 255), -1);
}

Here is an example of frame:

image description

It's working but my problem is that findContour is returning too many contours. Sometimes it returns more than 3000 contours and it takes too much time to draw them.

In the frame there is a person. I would like to get only one contour with the person instead of many of them in each line of the face.

Is there a way to approximate the little contours in a only one with the whole object? I'm looking for a way to reduce the number of contours, getting only the biggest of them.

Any tip will be very helpful,

Thanks

edit retag flag offensive close merge delete

Comments

1

You can try to Imgproc.RETR_EXTERNAL instead of Imgproc.RETR_TREE.

Also, you can remove small contours using contour area before drawing.

Eduardo gravatar imageEduardo ( 2016-09-30 16:09:49 -0500 )edit

" I would like to get only one contour with the person instead of many of them in each line of the face. " -- maybe findContours() is not the right tool then.

you could try face-detection first (to restrict further search) and landmark detection later, to get a coarse outline of the face

berak gravatar imageberak ( 2016-10-01 01:53:41 -0500 )edit