How to use OpenCV face detection in portrait using byte[] data from onPreviewFrame()?

asked 2014-04-29 09:31:57 -0500

updated 2015-10-06 09:07:59 -0500

Hi guys,

I am trying to use OpenCV face detection using the the byte[] data obtained from the onPreviewFrame() method of the Camera.PreviewCallback

I manage to convert the data into grayscale image using the codes below.

        Mat matNew = new Mat(pHeight, pWidth, CvType.CV_8U);
        matNew.put(0, 0, data);
        Mat matrgb = new Mat();
        Imgproc.cvtColor(matNew, matrgb, Imgproc.COLOR_YUV420sp2RGB, 4);
        Mat matgray = new Mat();
        Imgproc.cvtColor(matrgb, matgray, Imgproc.COLOR_RGB2GRAY, 0);

and I have set android:screenOrientation to "portrait" in the AndroidManifest file.

I am using OpenCV JavaDetector

mJavaDetector.detectMultiScale(matgray, faceDetected, 1.1, 3, 0,
new org.opencv.core.Size(0,0), new org.opencv.core.Size(matgray.width(), matgray.height()));

and drawing a rectangle over the faces detected using this

     for (Rect rect : faceDetected.toArray()){
      Core.rectangle(matgray, new Point(rect.x, rect.y), 
new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
                    }

However, in the resulting grayscale mat, face detection only happens when I hold my Android phone in landscape position. It does not work in portrait position.

Is there any way to overcome this issue? I have used the Android FaceDetectionListener and that doesn't seem to have problem detecting faces in portrait mode. But, FaceDetectionListener's functions are limited compared to OpenCV.

Any help would be greatly appreciated. Thx.

edit retag flag offensive close merge delete