java Android 上的 OpenCV 转换为灰度不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15912392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:16:29  来源:igfitidea点击:

OpenCV on Android converting to grayscale not working

javaandroidopencv

提问by Pax0r

I am trying to convert some OpenCV Mat to grayscale for Contours detection algorithms. For some reason the image after convertion is all black. Mine code (b is Android Bitmap):

我正在尝试将一些 OpenCV Mat 转换为轮廓检测算法的灰度。由于某种原因,转换后的图像全黑。我的代码(b是Android Bitmap):

Mat tmp = new Mat (b.getWidth(), b.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(b, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_BGR2GRAY);
//there could be some processing
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2BGRA, 4);
Utils.matToBitmap(tmp, b);

And now I am drawing this bitmap and it is all black. When I applied contour detection to this bitmap (in place of comment) there were no match, so I assume problem is with convertion. After removing convertion (simply call bitmapToMat and matToBitmap) then bitmap is not all black so convertion to Mat are also working. Bitmap is in ARGB_8888 and there are no errors, just the output bitmap is all black.

现在我正在绘制这个位图,它是全黑的。当我对此位图(代替注释)应用轮廓检测​​时,没有匹配,所以我认为问题出在转换上。删除转换后(简单地调用 bitmapToMat 和 matToBitmap)然后位图不是全黑的,所以转换为 Mat 也有效。位图在ARGB_8888中,没有错误,只是输出位图全黑。

Edit: Just to be sure I tried to save a bitmap with ocv imwrite - it's still all black so the problem is for 100% at cvtColor...

编辑:为了确保我尝试用 ocv imwrite 保存位图 - 它仍然是全黑的,所以问题是 cvtColor 100%......

回答by Karthik Balakrishnan

If the bitmap b is from the android device, then try using COLOR_RGB2GRAYinstead of COLOR_BGR2GRAYbecause BGRis the default pixel format for OpenCV images, not all images.

如果位图 b 来自 android 设备,则尝试使用COLOR_RGB2GRAY而不是COLOR_BGR2GRAY因为BGR是 OpenCV 图像的默认像素格式,而不是所有图像。

Try this code:

试试这个代码:

Mat tmp = new Mat (b.getWidth(), b.getHeight(), CvType.CV_8UC1);
Utils.bitmapToMat(b, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
//there could be some processing
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_GRAY2RGB, 4);
Utils.matToBitmap(tmp, b);

回答by Gaby Bou Tayeh

I tried this method and it works perfectly, first you must convert it into grey scale, then to Canny, and finally to Bitmap.

我试过这个方法,效果很好,首先你必须把它转换成灰度,然后是Canny,最后是位图。

this function returns a black and white image.

此函数返回黑白图像。

public static Bitmap edgesim(Mat img1) {

Bitmap image1;

//mat gray img1 holder
Mat imageGray1 = new Mat();

//mat canny image
Mat imageCny1 = new Mat();

//mat canny image
Mat imageCny2 = new Mat();

/////////////////////////////////////////////////////////////////

//Convert img1 into gray image
Imgproc.cvtColor(img1, imageGray1, Imgproc.COLOR_BGR2GRAY);

//Canny Edge Detection
Imgproc.Canny(imageGray1, imageCny1, 10, 100, 3, true);

///////////////////////////////////////////////////////////////////

//////////////////Transform Canny to Bitmap/////////////////////////////////////////
image1= Bitmap.createBitmap(imageCny1.cols(), imageCny1.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imageCny1, image1);

return image1;

}

}