我正在使用OpenCV4Android版本2.4.11我正在检测相机检索到的帧中的任何矩形形状。对于每个检测到的矩形形状,我在它周围绘制一个矩形。我想做的是,转换里面的图像 将矩形转换为Mat对象,然后将Mat对象转换为Bitmap,并在ImageView中显示Bitmap对象,如下面的代码所示。
问题是,当我运行以下代码时,有时App会崩溃并显示下面发布的logcat输出。实际上我无法理解logcat输出的含义是什么,但我试图通过指定一些条件来修复它 在下面发布的代码中显示的if语句中,但它没有解决问题。
请让我知道解决这个问题,为什么我会得到它?
码:
this.mBtnSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bounding_rect != null && bounding_rect.width > 0 && bounding_rect.height > 0 && bounding_rect.width > bounding_rect.height && mMatInputFrame != null) {
Mat roi = new Mat(mMatInputFrame, bounding_rect); //<<<<---------- Line 160
Log.w(TAG, "bounding_rectw: " + bounding_rect.width + ", bounding_recth: " + bounding_rect.height);
Log.w(TAG, "ROIw: " + roi.width() + ", ROIh: " + roi.height());
final Bitmap bitmap = Bitmap.createBitmap(roi.width(), roi.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(roi, bitmap);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mIVCapture.setImageBitmap(bitmap);
}
});
}
}
});
logcat的:
OpenCV Error: Assertion failed (0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows) in cv::Mat::Mat(const cv::Mat&, const cv::Range&, const cv::Range&), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix.cpp, line 284
E/org.opencv.core.Mat: Mat::n_1Mat__JIIII() caught cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix.cpp:284: error: (-215) 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows in function cv::Mat::Mat(const cv::Mat&, const cv::Range&, const cv::Range&)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.approxcontour_04, PID: 11436
CvException [org.opencv.core.CvException: cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/matrix.cpp:284: error: (-215) 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows in function cv::Mat::Mat(const cv::Mat&, const cv::Range&, const cv::Range&)
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:676)
at com.example.approxcontour_04.FragOpenCVCam$2.onClick(FragOpenCVCam.java:150)
at android.view.View.performClick(View.java:5721)
at android.widget.TextView.performClick(TextView.java:10930)
at android.view.View$PerformClick.run(View.java:22620)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
10-25 10:51:21.271 11436-11672/com.example.approxcontour_04 D/CameraBridge: mStretch value: 0.0
答案 0 :(得分:0)
我通过在if语句中指定以下条件来解决它
if (bounding_rect != null && mMatInputFrame.width() > bounding_rect.width
&& mMatInputFrame.height() > bounding_rect.height)