Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Detecting only one face using OpenCV in android

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.

click to hide/show revision 2
retagged

updated 2015-04-12 02:25:39 -0600

berak gravatar image

Detecting only one face using OpenCV in android

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.

Detecting only one face using OpenCV in android

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();

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.