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

时间:2015-01-07 06:13:33

标签: java android debugging opencv

我正在尝试使用OpenCV为Android编写一个简单的应用程序来加载图像,处理它并在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论坛上发布了这个问题,但还没有得到任何帮助。任何人都可以帮助。

1 个答案:

答案 0 :(得分:2)

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

将图像复制到SD卡,然后从那里复制imread(),或者尝试Utils.loadResource()