Ask Your Question

Revision history [back]

OpenCV4Android, Core.minMaxLoc() assertion failed

Hello,

I have created also ticket for my issue on Stackoverflow: http://stackoverflow.com/questions/32799842/opencv4android-error-assertion-failed-in-core-minmaxlocmroi-method

I am looking to answer what should I fix in my code to make it work. Code was working in the past and is taken also from tutorial that worked for me before.

One year ago I have been working with OpenCV4Android lib 2.4.9 on Eclipse. I have been using code written by romanhosek (http://romanhosek.cz/android-eye-detection-and-tracking-with-opencv/) for tracking eyes and iris.

My code has been working at that time. After one year I came back and tried to setup new project on Android Studio. I managed to connect OpenCV and all methods worked until I copied snipets of code from one year ago. So code that worked in the past is not working for me now. I can see that Roman has updated his code to OpenCV 2.6: http://romanhosek.cz/android-eye-detection-updated-for-opencv-2-4-6/. I have tried to copy paste this code but I still got error.

So I thought that maybe problem is Android Studio and returned to Eclipse IDE but I received the same error as in Android Studio.

@Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        inputFrame.rgba().copyTo(mRgba);

        // Create a grayscale image
        Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2RGB);

        MatOfRect faces = new MatOfRect();

        // Use the classifier to detect faces
        if (cascadeFace != null) {
            cascadeFace.detectMultiScale(mGray, faces, 1.1, 2, 2,
                    new Size(absoluteFaceSize, absoluteFaceSize), new Size());
        }

        // If there are any faces found, draw a rectangle around it
        facesArray = faces.toArray();
        for (int i = 0; i < facesArray.length; i++) {
               // If there are any faces found, draw a rectangle around it
               Imgproc.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(),
                    yellowScalar, 3);

               //hardcode area of left eye
               Rect r = facesArray[i];
               Rect eyearea_left = new Rect(r.x + r.width / 16
                    + (r.width - 2 * r.width / 16) / 2,
                    (int) (r.y + (r.height / 4.5)),
                    (r.width - 2 * r.width / 16) / 2, (int) (r.height / 3.0));
               // draw left eye area
               Imgproc.rectangle(mRgba, eyearea_left.tl(), eyearea_left.br(),
                    new Scalar(255, 0, 0, 255), 2);


               if (learn_frames < 5) {
               templateL = get_template(cascadeLeftEye, eyearea_left, 24);
               learn_frames++;
           } else {
               // Learning finished, use the new templates for template
               // matching
              match_eye(eyearea_left, templateL, 0);
           }
       }

       return mRgba;
   }

And error occurs in get_template method in Core.minMaxLoc(mROI).

private Mat get_template(CascadeClassifier clasificator, Rect area, int size) {
        Mat template = new Mat();
        Mat mROI = mGray.submat(area);
        Log.d("AppInfo", "areaI Size: " + area.height + "x" + area.width);
        Log.d("AppInfo", "1 mROI Size: " + mROI.height() + "x" + mROI.width());
        MatOfRect eyes = new MatOfRect();
        Point iris = new Point();
        Rect eye_template = new Rect();
        clasificator.detectMultiScale(mROI, eyes, 1.15, 2,
                Objdetect.CASCADE_FIND_BIGGEST_OBJECT
                        | Objdetect.CASCADE_SCALE_IMAGE, new Size(30, 30),
                new Size());

        Rect[] eyesArray = eyes.toArray();
        for (int i = 0; i < eyesArray.length;) {
            Rect e = eyesArray[i];
            e.x = area.x + e.x;
            e.y = area.y + e.y;
            Rect eye_only_rectangle = new Rect((int) e.tl().x,
                    (int) (e.tl().y + e.height * 0.4), (int) e.width,
                    (int) (e.height * 0.6));
            Log.d("AppInfo", "e: " + e.height + "x" + e.width);
            Log.d("AppInfo", "eye_only_rect: " + eye_only_rectangle.height + "x" + eye_only_rectangle.width);
            mROI = mGray.submat(eye_only_rectangle);
            Log.d("AppInfo", "2 mROI Size: " + mROI.height() + "x" + mROI.width());
            Mat vyrez = mRgba.submat(eye_only_rectangle);

            Core.MinMaxLocResult mmG = Core.minMaxLoc(mROI); // error line

            Imgproc.circle(vyrez, mmG.minLoc, 2, new Scalar(255, 255, 255, 255), 2);
            iris.x = mmG.minLoc.x + eye_only_rectangle.x;
            iris.y = mmG.minLoc.y + eye_only_rectangle.y;
            eye_template = new Rect((int) iris.x - size / 2, (int) iris.y
                    - size / 2, size, size);
            Imgproc.rectangle(mRgba, eye_template.tl(), eye_template.br(),
                    new Scalar(255, 0, 0, 255), 2);
            template = (mGray.submat(eye_template)).clone();
            return template;
        }
        return template;
    }

Also I can provide you with LogCat to prove that templates inserted are having right sizes:

09-26 18:53:39.260      610-867/com.agh.krzyk.kamil.facialexpressionrecogsys D/AppInfo﹕ areaI Size: 125x164
09-26 18:53:39.260      610-867/com.agh.krzyk.kamil.facialexpressionrecogsys D/AppInfo﹕ 1 mROI Size: 125x164
09-26 18:53:39.345      610-867/com.agh.krzyk.kamil.facialexpressionrecogsys D/AppInfo﹕ e: 24x36
09-26 18:53:39.345      610-867/com.agh.krzyk.kamil.facialexpressionrecogsys D/AppInfo﹕ eye_only_rect: 14x36
09-26 18:53:39.345      610-867/com.agh.krzyk.kamil.facialexpressionrecogsys D/AppInfo﹕ 2 mROI Size: 14x36

And in the end error log:

09-26 18:53:39.350      610-867/com.agh.krzyk.kamil.facialexpressionrecogsys E/cv::error()﹕ OpenCV Error: Assertion failed ((cn == 1 && (_mask.empty() ||
_mask.type() == CV_8U)) || (cn > 1 && _mask.empty() && !minIdx && !maxIdx)) in void cv::minMaxIdx(cv::InputArray, double*, double*, int*, int*, cv::InputArray), file /builds/master_pack-android/opencv/modules/core/src/stat.cpp, line 2192 09-26 18:53:39.355      610-867/com.agh.krzyk.kamil.facialexpressionrecogsys E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-26900
    Process: com.agh.krzyk.kamil.facialexpressionrecogsys, PID: 610
    CvException [org.opencv.core.CvException: /builds/master_pack-android/opencv/modules/core/src/stat.cpp:2192: error: (-215) (cn == 1 && (_mask.empty() || _mask.type() == CV_8U)) || (cn > 1 && _mask.empty() && !minIdx && !maxIdx) in function void cv::minMaxIdx(cv::InputArray, double*, double*, int*, int*, cv::InputArray)
    ]
            at org.opencv.core.Core.n_minMaxLocManual(Native Method)
            at org.opencv.core.Core.minMaxLoc(Core.java:2165)
            at org.opencv.core.Core.minMaxLoc(Core.java:2177)
            at com.agh.krzyk.kamil.facialexpressionrecogsys.ui.presenter.CameraActivityPresenter.get_template(CameraActivityPresenter.java:278)
            at com.agh.krzyk.kamil.facialexpressionrecogsys.ui.presenter.CameraActivityPresenter.onCameraFrame(CameraActivityPresenter.java:227)
            at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:393)
            at com.agh.krzyk.kamil.facialexpressionrecogsys.utils.view.PortraitCameraView.access$400(PortraitCameraView.java:25)
            at com.agh.krzyk.kamil.facialexpressionrecogsys.utils.view.PortraitCameraView$CameraWorker.run(PortraitCameraView.java:331)
            at java.lang.Thread.run(Thread.java:841)