OpenCV 调整大小失败 [英] OpenCV resize failing
问题描述
我有一个使用 OpenCV for android 中的 JavaCameraView
的应用程序,它使用以下回调
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
在实际用户界面中什么也没有发生.我不知道为什么我会看到这个错误,因为我从不调用 Utils.bitmapToMat
或 Utils.matToBitmap
.当我取出 Imgproc.resize(inputFrame,fit,newSize);
并返回 inputFrame
时,一切都按预期进行.
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.
推荐答案
对于 onCameraFrame
,我相信您必须返回与 inputFrame
相同大小的 Mat.由于从您的相机获得的图像的分辨率是240*160(参考这个 -> E/CameraBridge: Bitmap type: 240*160
)你需要在你完成你的 Mat fit
调整到 240*160处理..
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..
一个简单的修复:
@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 使用它来抓取背面的图像:)
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 :)
这篇关于OpenCV 调整大小失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!