Why face detected is in OpenCV is not exactly surrounding face??

asked 2013-11-19 10:53:52 -0500

I am trying to do a face detection application.

It receives data from from onPreviewFrame and process it in openCV.

The problem is that when I convert it back to Bitmap to be displayed in Android, the rectangle for the face detected is not exactly surrounding the face.

It is moved slightly downwards. So, the top of the rectangle is at below the nose and the bottom is at the bottom of the neck.

My code are as below.

Mat matYuv = new Mat();

matYuv = new Mat(pHeight, pWidth, CvType.CV_8UC1);
matYuv.put(0, 0, data);

Mat matRgb = new Mat();
Imgproc.cvtColor(matYuv, matRgb, Imgproc.COLOR_YUV420sp2RGB, 4);
try{
Mat matGray = new Mat();

int height = matGray.rows();
int faceSize = Math.round(height * 1.0F);


MatOfRect rectFaces = new MatOfRect();
Imgproc.cvtColor(matRgb, matGray, Imgproc.COLOR_RGB2GRAY, 0);

//transpose and flipping matrix to enable detection in portrait mode                                            
Mat temp = matGray.clone();
Core.transpose(matGray, temp);
Core.flip(temp, temp, -1);

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

facesArray = faces.toArray();
try{
//drawing rectangle around faces
for (int i = 0; i < facesArray.length; i++){
Core.rectangle(matGray, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
}
//converting from OpenCV to bitmap to be displayed in surface view.
try{

resultBitmap = Bitmap.createBitmap(640, 480, Bitmap.Config.RGB_565);
Utils.matToBitmap(matGray, resultBitmap);

catch(Exception e){                         
Log.e(TAG, "Length of resultBitmap" +resultBitmap.getByteCount());
}
edit retag flag offensive close merge delete

Comments

Any idea??

Jagatheesan Jack gravatar imageJagatheesan Jack ( 2013-11-20 09:36:07 -0500 )edit