为什么在OpenCV中检测到的脸部并不完全围绕着脸?

时间:2013-11-19 17:12:19

标签: android opencv face-detection

我正在尝试进行面部检测应用程序。

它从onPreviewFrame接收数据并在openCV中处理它。

问题在于,当我将其转换回Bitmap以在Android中显示时,检测到的脸部矩形并不完全围绕脸部。

略微向下移动。因此,矩形的顶部位于鼻子下方,底部位于颈部的底部。

我的代码如下。

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

0 个答案:

没有答案