- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在做模板匹配以找到人眼的虹膜。我一直在关注我在 OpenCV4Android 网站上找到的 romanhosek 教程 - http://romanhosek.cz/android-eye-detection-updated-for-opencv-2-4-6/
在过去 - 一年前,这段代码对我有用,但我在 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 中工作...但为什么呢?
最佳答案
对于遇到同样错误的其他人。
代替:
// Create a grayscale image
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2RGB);
尝试
Mat gray = inputFrame.gray();
在上面的行中,并在 .minmaxLoc() 函数中使用这个 Mat。我不知道它是如何工作的,但它对我有帮助。
关于java - OpenCV4Android 错误 : Assertion failed in Core. minMaxLoc(mROI) 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32799842/
我正在做模板匹配以找到人眼的虹膜。我一直在关注我在 OpenCV4Android 网站上找到的 romanhosek 教程 - http://romanhosek.cz/android-eye-det
我是一名优秀的程序员,十分优秀!