Why my App crashes when i applied Imgproc.resize
As the illustration shows, now i have the 'original' frame, not fill fully the screen. And i expect to got a fullscreen frame like below part, so i choose using imgproc.resize to achieve this, but when i run my app on the mobilephone, the app quit unexpectedly, i don't know why, anyone can help me ? Thanks !
Here is some related code:
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Mat frame = inputFrame.rgba();
Mat dst = new Mat();
Imgproc.resize(frame,dst,new Size(),1.5,1.5,Imgproc.INTER_LINEAR);
frame.release();
return dst;
}
And this is debug error report:
E/cv::error(): OpenCV(3.4.7) Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /build/3_4_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp, line 101
E/org.opencv.android.Utils: nMatToBitmap caught cv::Exception: OpenCV(3.4.7) /build/3_4_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:101: error: (-215:Assertion failed) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function 'void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)'
E/CameraBridge: Mat type: Mat [ 1620*2880*CV_8UC4, isCont=true, isSubmat=false, nativeObj=0xffffffffc9934a88, dataAddr=0xffffffffc3100000 ]
E/CameraBridge: Bitmap type: 1920*1080
E/CameraBridge: Utils.matToBitmap() throws an exception: OpenCV(3.4.7) /build/3_4_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:101: error: (-215:Assertion failed) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function 'void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)'
please do not release the frame Mat (you did not construct it), and
dst
should be a class member and only initialized once, else you have a memleak there.Thanks, berak ! I have move the ”Mat dst = new Mat();“ to outside, and delete "frame.release();". But in this way,i got another error: E/uardonxiaomi8s: No implementation found for long org.opencv.core.Mat.n_Mat() (tried Java_org_opencv_core_Mat_n_1Mat and Java_org_opencv_core_Mat_n_1Mat__) D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.iguardonxiaomi8se, PID: 5366 java.lang.UnsatisfiedLinkError: No implementation found for long org.opencv.core.Mat.n_Mat() (tried Java_org_opencv_core_Mat_n_1Mat and Java_org_opencv_core_Mat_n_1Mat__) ........ What's the wrong?
this means, you try to use opencv code, before the native libs are loaded. (put it into onCameraViewStarted() or such, NOT into the constructor)
I don't quite understand, you mean i can't use Imgproc.resize in
public Mat onCameraFrame( )
? But if i use Imgproc.cvtColor or Imgproc.threshold etc., it is OK. And if i putImgproc.resize(frame,dst,new Size(),1.5,1.5,Imgproc.INTER_LINEAR);
in onCameraViewStarted(), android studio cannot resolve symbol 'frame'. Please forgive me for such a elementary question ...yea, it's complicated and painful.
you may do whatever you want in onCameraFrame() (and your resize() code has to stay there !), but the 1st possible place, where you can use opencv code is onCameraViewStarted(); (that's where you have to put the
dst = new Mat();
. and again,dst
should be a class member.please have another look at opencv's android examples !
Sir, when I was about to give up resize, the miracle appeared. I ran the App without resize on the phone again. The preview size is exactly 1920*1080 that i expected !Previously my preview size is like the first image of above illustration, I don't know why this time preview size suddenly changed.Is it my previous resize command that forced the opencv to choose the largest preview size?