Ask Your Question
0

How to use cvtColor convert UIImage to gray?

asked 2018-07-19 09:50:26 -0600

sinojelly gravatar image

updated 2018-07-19 09:56:55 -0600

I convert UIImage to Mat like this:(On iOS)

UIImageToMat(image1, baseImage, true);

And use imwrite("/home/before.png", baseImage); the output image is ok.

image description

I use the code like this to convert it to gray:

cvtColor(baseImage, baseImageB, COLOR_BGRA2GRAY);

And use imwrite("/home/after.png", baseImageB); the output image is black.

image description

and save it to txt file, it seems that there a many zeros, and at last some lines of 1.94871e+09 or some other value.

And I tried COLOR_BGR2GRAY, RGB2GRAY, RGBA2GRAY, all failed.

How to get the right gray image?

I want to convert image to gray, because it shows the following error. Is there any other ways to fix it? ontours.cpp:199: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'

Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2018-07-19 10:09:01 -0600

berak gravatar image

it's one of those silly PNG's where ALL of the information is in the alpha channel ONLY (R,G,B are all black !).

so, when you try to convert it to grayscale, the alpha will be thrown away first, and you're left with a black image.

if you need a grayscale image for findContours(), rather:

Mat gray;
extractChannel( baseImage, gray, 3 ); // get the alpha channel

it will look like this:

image description

edit flag offensive delete link more

Comments

sjhalayka gravatar imagesjhalayka ( 2018-07-19 10:44:11 -0600 )edit

^^ ... 10.3mb O~O

berak gravatar imageberak ( 2018-07-19 11:01:24 -0600 )edit

It works! Thanks very much!

sinojelly gravatar imagesinojelly ( 2018-07-19 11:12:13 -0600 )edit

@sinojelly, it was not at all obvious. i only knew, because some other folks had the same problem here before !

berak gravatar imageberak ( 2018-07-19 12:21:02 -0600 )edit

The poster-sized image is 45MB (it's 8000x8000 pixels, or 26.66 x 26.66 inches at 300 DPI). I got large screenshots working in my OpenGL code. :D

Glad to hear that you fixed sinojelly's problem.

So far there's 6 posters. I'll be printing them at the beginning of the month.

sjhalayka gravatar imagesjhalayka ( 2018-07-19 13:31:46 -0600 )edit

@berak — six poster-sized images are located at https://drive.google.com/uc?id=1G0xnk...

The code is located at https://github.com/sjhalayka/bezier_f...

sjhalayka gravatar imagesjhalayka ( 2018-07-19 21:03:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-19 09:50:26 -0600

Seen: 693 times

Last updated: Jul 19 '18