I'm developing a project about face recognition on android. I meet a problem after detect face region [closed]

asked 2014-04-25 07:52:27 -0500

rainNight gravatar image

updated 2014-04-26 00:34:19 -0500

berak gravatar image

I meet a problem in the onCameraFrame() function. After detect face in the function, I want to get the face region, and then create a new Mat to save the face area. but there are a problem.My code is:

mRgba = inputFrame.rgba();
    Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGB2GRAY);
    int height = mGray.rows();
    if (Math.round(height * mRelativeFaceSize) > 0) {
        mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
    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, 3);
    Mat faceImg = new Mat(mGray,facesArray[0]);

    return mRgba;

I want to get the face area, so I use the gray image to init the new Mat data. My error isimage description

while the program is right if I move the penultimate sentence

Mat faceImg = new Mat(mGray,facesArray[0]);

I want to know why is it. thanks

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-28 14:06:06.737654


you will be getting an access violation, if it did not find any faces : facesArray[0]

(would you be so nice, to copy/paste your logcat output , and change the unreadable image above to plain text ?)

berak gravatar imageberak ( 2014-04-26 00:33:54 -0500 )edit

yes, the facesArray[0] may be null, can I succeed atfer adding the code : if(facesArray[0]!=null) Mat faceImg = new Mat(mGray,facesArray[0]);

rainNight gravatar imagerainNight ( 2014-04-26 01:54:39 -0500 )edit

no, still the same problem. you just can't use [0] on an empty array.

if (facesArray.length > 0 ) // do something with facesArray[0]

maybe even put Mat faceImg = new Mat(mGray,facesArray[0]);inside the loop, and break out after the 1st )

berak gravatar imageberak ( 2014-04-26 02:09:09 -0500 )edit

thank you very much, I have sloved this problem. you help me a lot, thanks again

rainNight gravatar imagerainNight ( 2014-04-26 03:06:28 -0500 )edit