sjyn sjyn - 5 months ago 90
Android Question

OpenCV resize failing

I have an application using A

JavaCameraView
from OpenCV for android that uses the following callback

CameraBridgeViewBase.CvCameraViewListener cvListener = new CameraBridgeViewBase.CvCameraViewListener() {
@Override
public void onCameraViewStarted(int width, int height) {

}

@Override
public void onCameraViewStopped() {

}

@Override
public Mat onCameraFrame(Mat inputFrame) {
Size newSize = new Size(400, 200);
Mat fit = new Mat(newSize, CvType.CV_8UC4);
Imgproc.resize(inputFrame,fit,newSize);
return fit;
}
};


However, when the resizing happens, an error occurs saying

E/CameraBridge: Utils.matToBitmap() throws an exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) 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/cv::error(): OpenCV 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 /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp, line 97
E/org.opencv.android.Utils: nMatToBitmap catched cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) 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 [ 244*432*CV_8UC4, isCont=true, isSubmat=false, nativeObj=0x1ade1b0, dataAddr=0x5dbc6010 ]
E/CameraBridge: Bitmap type: 240*160


Nothing happens in the actual UI. I'm not sure why I'm seeing this error, since I'm never calling
Utils.bitmapToMat
or
Utils.matToBitmap
. When I take out the
Imgproc.resize(inputFrame,fit,newSize);
and return the
inputFrame
, everything works as expected.

Answer

For onCameraFrame, I believe you have to return the Mat of the same size as your inputFrame. Since the resolution of the image obtained from your camera is 240*160 (refer this-> E/CameraBridge: Bitmap type: 240*160) you will need to resize your Mat fit back to 240*160 after you done your processing..

A simple fix:

@Override
public Mat onCameraFrame(Mat inputFrame) {
    Size newSize = new Size(400, 200);
    Mat fit = new Mat(newSize, CvType.CV_8UC4);
    Imgproc.resize(inputFrame,fit,newSize);

    /**Your processing code */

    Imgproc.resize(fit,fit,inputFrame.size()); <- Add this line

    return fit;
}

To change the size of the image you receive from camera, you have to call mOpenCvCameraView.setMaxFrameSize(width, height); but the resolution will be chosen from the list of resolutions supported by the camera hardware. To know more why you can't set an arbitrary value for the resolution you'll need to read on Android Camera API, since that's what OpenCV uses to grab the images at the back :)

Comments