Ask Your Question
0

Detecting only one face using OpenCV in android

asked 2015-04-11 15:03:51 -0600

Vishal Chhatwani gravatar image

updated 2015-04-13 09:00:12 -0600

Hello everyone. So what I want to do is to detect a face to perform some image processing operations. I got a sample code of face detection from OpenCV library. But that code is for detecting multiple faces. But I just want to detect single face. Here is a code:

MatOfRect faces = new MatOfRect();

        if (mJavaDetector != null)
            mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2,
                    2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                    new Size(mAbsoluteFaceSize, mAbsoluteFaceSize),
                    new Size());

    Rect[] facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++) {
        Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(),
                FACE_RECT_COLOR, 2);
        xCenter = (facesArray[i].x + facesArray[i].width + facesArray[i].x) / 2;
        yCenter = (facesArray[i].y + facesArray[i].y + facesArray[i].height) / 2;
        Point center = new Point(xCenter, yCenter);

You can see, there is a for loop to detect multiple faces. When I try to remove that for loop and change that array type variable, I get an error. Can someone please tell me how to use those tl() and br() methods inside for loop if I want to detect just one face.

Thanks in advance.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-04-30 16:04:52 -0600

MatOfRect faces = new MatOfRect();

        if (mJavaDetector != null)
            mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2,
                    4, // TODO: objdetect.CV_HAAR_FIND_BIGGEST_OBJECT -> with this parameter only biggest face will be detected
                    new Size(mAbsoluteFaceSize, mAbsoluteFaceSize),
                    new Size());

    Rect[] facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++) {
        Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(),
                FACE_RECT_COLOR, 2);
        xCenter = (facesArray[i].x + facesArray[i].width + facesArray[i].x) / 2;
        yCenter = (facesArray[i].y + facesArray[i].y + facesArray[i].height) / 2;
        Point center = new Point(xCenter, yCenter);
edit flag offensive delete link more
0

answered 2015-05-01 09:01:25 -0600

clytemnestra gravatar image

updated 2015-05-01 09:03:24 -0600

Just count 1 face and draw the rectangle only for that face. Make sure that you actually detected at least once face with

facesArray.length>0

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-04-11 15:03:51 -0600

Seen: 1,752 times

Last updated: May 01 '15