2015-09-26 37 views
2

我正在做templateMatching以查找人眼的虹膜。我一直在OpenCV4Android網站上找到了romanhosek的教程 - http://romanhosek.cz/android-eye-detection-updated-for-opencv-2-4-6/OpenCV4Android錯誤:Core.minMaxLoc(mROI)方法中斷言失敗

過去 - 一年前,這段代碼適合我,但是我在Eclipse上使用2.4 lib版本。但作爲一個Android開發人員,我不喜歡這個IDE,並且儘可能地嘗試切換到Android Studio。現在,當3.0可用時,我終於可以將它連接到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; 
    } 

和錯誤發生在Core.minMaxLoc(MROI)get_template方法。

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

我也可以爲你提供的logcat證明插入模板具有正確的尺寸:

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 

而最終錯誤日誌:

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) 

-----編輯: 好吧,我已經安裝了Eclipse環境,因爲它過去在Eclipse IDE上爲我工作。我已經從OpenCV 3.0 lib打開facedetect示例,並在其中添加了此代碼。我有同樣的錯誤。在2.6中工作的代碼不再適用於3.0 ...但爲什麼?

回答

0

對於其他人,誰得到相同的錯誤。

代替:

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

嘗試

Mat gray = inputFrame.gray(); 
在上面的行

,並使用此墊在.minmaxLoc()函數。我不知道它是如何工作的,但它對我有幫助。