OpenCV调整大小失败

时间:2016-06-22 14:09:30

标签: android opencv

我有一个使用OpenCV for A的JavaCameraView的应用程序,它使用以下回调

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

但是,当调整大小时,会出现错误

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

实际用户界面中没有任何事情发生。我不确定为什么我会看到此错误,因为我从不致电Utils.bitmapToMatUtils.matToBitmap。当我取出Imgproc.resize(inputFrame,fit,newSize);并返回inputFrame时,一切都按预期工作。

1 个答案:

答案 0 :(得分:3)

对于onCameraFrame,我认为您必须返回与inputFrame相同尺寸的垫子。由于从相机获得的图像的分辨率是 240 * 160(请参阅此 - > E/CameraBridge: Bitmap type: 240*160)您需要在完成处理后将垫子fit的大小调整为240 * 160 ..

一个简单的解决方法:

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

要更改从相机接收的图像尺寸,您必须拨打mOpenCvCameraView.setMaxFrameSize(width, height);,但分辨率将从相机硬件支持的分辨率列表中选择。要了解更多为什么您无法为Android Camera API上需要阅读的分辨率设置任意值,因为OpenCV使用它来获取后面的图像:)