0
我正在Android中构建图像处理项目。我通过相机捕获位图图片,并通过JNI将其提供给opencv C++函数。将位图转换为JNI中的Opencv :: Mat
首先,我使用保存的位图图片(PNG格式)测试我的opencv C++函数,并且它成功。
// in Android, save bitmap
Bitmap bmp = YUV_420_888_toRGB(img,img.getWidth(),img.getHeight());
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
Log.e(TAG,"saved successfully.)");
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
// in opencv c++ function
Mat im = imread("/Users/Jun/Downloads/20170227/P9/1488167433596_frame.PNG");
// processing im
然后我将每个捕获的位图图片送到同一个opencv C++函数。但是,检测结果完全不同。我认为在通过JNI将Java中的位图转换为C++中的opencv mat时必定存在一些错误。请找到以下转换代码:
//Java side:
public static int[] detector(Bitmap bitmap) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int []pixels = new int[w*h];
bitmap.getPixels(pixels,0,w,0,0,w,h);
return detect(pixels,w,h);
}
private static native int[] detect(int pixels[],int w,int h);
// c++ side:
JNIEXPORT jintArray JNICALL Java_com_example_jun_helloworld_JNIUtils_detect(JNIEnv *env, jclass cls, jintArray buf, jint w, jint h) {
jint* cbuf = env->GetIntArrayElements(buf, false);
if (cbuf == NULL) {
return NULL;
}
Mat im(h, w, CV_8UC4, (unsigned char *) cbuf);
// processing im
这两个“im”应该不同。有人能告诉我转换中有什么问题吗?谢谢。
请注意,jint是32位长,而字符只是16. – mko
所以你的意思是“CV_8UC4”不正确? –
不可以,这一个:(无符号字符*)cbuf – mko