First time here? Check out the FAQ!

Ask Your Question
0

Android java convertTo

asked Nov 8 '12

Jep gravatar image

Hi!

I'm trying to use watershed in android. To do so I need a mask of 32f. I have an image with the zones that I want for the watershed, but it is in RGB. I have tried convertTo but it doesn't work. I have done the following test to try the conversion:

Mat maskColor = new Mat( mBitmapPintar.getHeight(), mBitmapPintar.getWidth(), CvType.CV_8UC3);
Mat maskgris = new Mat( maskColor.rows(), maskColor.cols(), CvType.CV_8U );
Mat mask = new Mat( maskColor.rows(), maskColor.cols(), CvType.CV_32S);

maskColor contains the original mask in RGB. I have maskgris to bridge the conversion (it didn't work straight ahead). Now I convert it in gray, then to 32S, back to gray to visualize it and it doesn't work:

Imgproc.cvtColor( maskColor, maskgris, Imgproc.COLOR_BGR2GRAY);
Highgui.imwrite(directoriFitxer + "/maskengris.jpg", maskgris);
mask.convertTo( maskgris, CvType.CV_32F);
maskgris.convertTo(mask,  CvType.CV_8U);
Highgui.imwrite(directoriFitxer + "/maskengrisDespConv.jpg", maskgris);

When I check the two images, the last one maskengrisDespConv.jpg is all in black. What am I doing wrong?

Preview: (hide)

2 answers

Sort by » oldest newest most voted
5

answered Nov 9 '12

Pay attention on mat data types and convertTo params:

source.convertTo(destination, type)

Fixed code:

Mat maskColor = new Mat( mBitmapPintar.getHeight(), mBitmapPintar.getWidth(), CvType.CV_8UC3);
Mat maskgris = new Mat( maskColor.rows(), maskColor.cols(), CvType.CV_8U );
Mat mask = new Mat( maskColor.rows(), maskColor.cols(), CvType.CV_32F);

Imgproc.cvtColor( maskColor, maskgris, Imgproc.COLOR_BGR2GRAY);
Highgui.imwrite(directoriFitxer + "/maskengris.jpg", maskgris);
maskgris.convertTo(mask, CvType.CV_32F);
mask.convertTo(maskgris, CvType.CV_8U);
Highgui.imwrite(directoriFitxer + "/maskengrisDespConv.jpg", maskgris);
Preview: (hide)
1

answered Dec 14 '12

V.G. gravatar image

If you believe, that this issue has been resolved, could you please accept the most appropriate answer (even if it's your own one) and close this question. It would greatly improve navigation and overall experience with OpenCV Q&A.

Thanks.

Preview: (hide)

Question Tools

Stats

Asked: Nov 8 '12

Seen: 5,424 times

Last updated: Dec 14 '12