繁体   English   中英

OpenCV错误:断言失败(src.dims == 2 && info.height ==(uint32_t)

[英]OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)

我正在尝试使用适用于Android的OpenCV编写一个简单的应用程序,以加载图像,对其进行处理并在ImageView中显示该图像。 我在OpenCV论坛上进行了搜索,人们认为以下代码是实现此操作的典型方法。

public class mainactivity extends Activity {

private Mat destination, source;

static {
    OpenCVLoader.initDebug();
}

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        if (status == LoaderCallbackInterface.SUCCESS ) {

            threshold();
        } else {
            super.onManagerConnected(status);
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}

@Override
public void onResume() {;
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9,this, mLoaderCallback);
 }

public void threshold(){

    try {
        source = Highgui.imread(getAssets().open("10007.bmp").toString());
    } catch (IOException e) {
        e.printStackTrace();
    }

    destination = new Mat(source.rows(), source.cols(), source.type());

    Log.w("source type", String.valueOf(source.type()));
    Log.w("source rows", String.valueOf(source.rows()));
    Log.w("source columns", String.valueOf(source.cols()));
    Log.w("source width", String.valueOf(source.width()));
    Log.w("source height", String.valueOf(source.height()));
    Log.w("source size", String.valueOf(source.size()));

    Imgproc.threshold(source, destination, 127, 255, Imgproc.THRESH_TOZERO);

    Log.w("destination type", String.valueOf(destination.type()));
    Log.w("destination rows", String.valueOf(destination.rows()));
    Log.w("destination columns", String.valueOf(destination.cols()));
    Log.w("destination width", String.valueOf(destination.width()));
    Log.w("destination height", String.valueOf(destination.height()));

    Highgui.imwrite("ThreshZero.jpg", destination);

    Bitmap bmp = Bitmap.createBitmap(640, 480, Config.ARGB_8888);
    Utils.matToBitmap(source, bmp);
    ImageView image= (ImageView) findViewById(R.id.imageView1);
    image.setImageBitmap(bmp);

}
}

不幸的是,这组代码引发了以下错误(我包括了日志消息):

01-07 13:59:49.934: W/source type(31303): 0
01-07 13:59:49.934: W/source rows(31303): 0
01-07 13:59:49.934: W/source columns(31303): 0
01-07 13:59:49.934: W/source width(31303): 0
01-07 13:59:49.935: W/source height(31303): 0
01-07 13:59:49.935: W/source size(31303): 0x0
01-07 13:59:49.937: W/destination type(31303): 0
01-07 13:59:49.937: W/destination rows(31303): 0
01-07 13:59:49.937: W/destination columns(31303): 0
01-07 13:59:49.937: W/destination width(31303): 0
01-07 13:59:49.937: W/destination height(31303): 0
01-07 13:59:49.944: E/cv::error()(31303): 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 /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 97
01-07 13:59:49.944: E/org.opencv.android.Utils(31303): nMatToBitmap catched cv::Exception: /home/reports/ci/slave_desktop/50-SDK/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)
01-07 13:59:49.945: D/AndroidRuntime(31303): Shutting down VM
01-07 13:59:49.946: E/AndroidRuntime(31303): FATAL EXCEPTION: main
01-07 13:59:49.946: E/AndroidRuntime(31303): Process: com.example.test1, PID: 31303
01-07 13:59:49.946: E/AndroidRuntime(31303): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/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)
01-07 13:59:49.946: E/AndroidRuntime(31303): ]

好的,我在阅读其他类似文章后从这组错误中了解到的是,与Mat src相比,我的目标位图具有不同的尺寸。 因此,我包括了Log消息以观察行和列的尺寸,并且都反映了零。 我已经在OpenCV论坛上发布了这个问题,但尚未获得任何帮助。 任何人都可以帮助请。

imread()无法从zippd文件或apk jar(存储资产的位置)中读取,因此图像可能只是空的/无效的。

将映像复制到sdcard,然后从那里复制imread(),或尝试使用Utils.loadResource()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM