我正在尝试使用适用于 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 源相比具有不同的维度。因此,我包含了日志消息来观察行和列的维度,它都反射(reflect)了零。我已经在 OpenCV 论坛上发布了这个问题,但尚未收到任何帮助。任何人都可以帮忙吗。
最佳答案
imread() 无法从 zippd 文件或您的 apk jar(这是您的 Assets 存储位置)中读取,因此您的图像可能只是空的/无效的。
要么将图像复制到 sdcard,然后从那里使用 imread(),要么尝试 Utils.loadResource()
关于java - OpenCV 错误 : Assertion failed (src. dims == 2 && info.height == (uint32_t),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27813446/