简体   繁体   English

Android上的OpenCV转换为灰度无法正常工作

[英]OpenCV on Android converting to grayscale not working

I am trying to convert some OpenCV Mat to grayscale for Contours detection algorithms. 我正在尝试将一些OpenCV Mat转换为灰度以用于Contours检测算法。 For some reason the image after convertion is all black. 由于某种原因,转换后的图像全黑。 Mine code (b is Android Bitmap): 我的代码(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. 删除转换后(只需调用bitmapToMat和matToBitmap),然后位图不是全黑的,所以转换为Mat也有效。 Bitmap is in ARGB_8888 and there are no errors, just the output bitmap is all black. 位图在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%...

If the bitmap b is from the android device, then try using COLOR_RGB2GRAY instead of COLOR_BGR2GRAY because BGR is 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);

I tried this method and it works perfectly, first you must convert it into grey scale, then to Canny, and finally to Bitmap. 我尝试了这种方法并且它完美地工作,首先你必须将它转换为灰度,然后转换为Canny,最后转换为Bitmap。

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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM