[英]OpenCV in Android Studio with NDK grabCut returns always black mask
I created an Android Studio project with NDK support and OpenCV native libraries using the steps in this example: 我使用此示例中的步骤创建了一个具有NDK支持和OpenCV本机库的Android Studio项目:
https://github.com/leadrien/opencv_native_androidstudio https://github.com/leadrien/opencv_native_androidstudio
With this example working fine, I'm trying to use grabCut algorithm. 在这个示例正常工作的情况下,我尝试使用grabCut算法。 My app should show 3 images in the main Activity: the original image, the transformed image and the mask used.
我的应用程序应在主“活动”中显示3张图像:原始图像,转换后的图像和使用的蒙版。 But actually the mask that is showing is all black and, for that, the processed image is like the original one.
但是实际上显示的蒙版是全黑的,为此,处理后的图像就像原始图像一样。
My java code: 我的Java代码:
private void processImage(){
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pastis);
Bitmap bmp32 = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
Mat imgToProcess = new Mat();
Mat mask = new Mat();
//utils from opencv library
Utils.bitmapToMat(bmp32, imgToProcess);
//NDK function
salt(imgToProcess.getNativeObjAddr(), mask.getNativeObjAddr(), 2000);
Bitmap bmp = Bitmap.createBitmap(imgToProcess.cols(), imgToProcess.rows(), Bitmap.Config.ARGB_8888);
//utils from opencv library
Utils.matToBitmap(imgToProcess, bmp);
Bitmap bmpMask = Bitmap.createBitmap(mask.cols(), mask.rows(), Bitmap.Config.ARGB_8888);
//utils from opencv library
Utils.matToBitmap(mask, bmpMask);
ImageView imageContainer = (ImageView) findViewById(R.id.resultImage);
imageContainer.setImageBitmap(bmp);
ImageView maskContainer = (ImageView) findViewById(R.id.maskImage);
maskContainer.setImageBitmap(bmpMask);
}
And my NDK code is: 我的NDK代码是:
void JNICALL Java_com_ach_MainActivity_salt(JNIEnv *env, jobject instance, jlong matAddrGray, jlong mask, jint nbrElem) {
Mat &mGr = *(Mat *) matAddrGray;
Mat &maskR = *(Mat *) mask;
int r = mGr.rows;
int c = mGr.cols;
Mat bgdModel, fgdModel;
Rect rect = Rect(10, 10,c-10, r-10);
cvtColor(mGr , mGr , CV_RGBA2RGB);
maskR.create(mGr.size(), CV_8UC1);
grabCut(mGr, maskR, rect, bgdModel, fgdModel, 2, GC_INIT_WITH_RECT);
mGr.copyTo(mGr, maskR);
}
¿Why I'm getting always all-black mask? ¿为什么我总是得到全黑的口罩?
Moreover, execute grabCut it taking about 50 seconds in my mobile (Samsung Galaxy S5) with an image resolution of 256 x 192 and only 2 steps. 此外,在我的手机(Samsung Galaxy S5)中以大约50秒钟的时间执行grapCut,图像分辨率为256 x 192,仅执行2步。 ¿Is this normal?¿How can I improve this?
这是正常现象吗?该如何改善?
Thanks in advance :) 提前致谢 :)
I found the problem some time ago but I forgot to post the answer. 我前段时间发现了问题,但忘了发布答案。
It seems that there was a problem with some bugs in opencv + ndk and I solved my problem not using the same bitmap at source and destination in copyTo function: 看来在opencv + ndk中存在一些错误的问题,我解决了我的问题,即在copyTo函数的源和目标上不使用相同的位图:
mGr.copyTo(mGr, maskR); --> PROBLEMS
mGr.copyTo(mGrDest, maskR); --> WORKING
And, just in case, since this problem I avoid the same for other functions with source-destination bitmaps like cvtColor , but I think there is not problem with that function. 而且,以防万一,由于这个问题,对于具有源目标位图的其他函数,例如cvtColor ,我避免使用相同的函数,但是我认为该函数没有问题。
I hope my solution works with your codes! 希望我的解决方案能与您的代码一起使用! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.