Ask Your Question

imwrite a Mat_float keeping the image.

asked 2015-11-07 01:49:16 -0500

215 gravatar image

updated 2015-11-08 02:50:15 -0500

I at moment trying to save a Mat which is given the type Mat_<float>.. When i try to save it, the image which it saves becomes black, but imshow shows that the image isn't black at all. Is there a way to save the image even though it has the type Mat_float?

edit retag flag offensive close merge delete


How do you save your image ? I think only yml format can save float image

LBerger gravatar imageLBerger ( 2015-11-07 02:18:50 -0500 )edit

as jpg, but i could try yml. It could not write to specified extension.

215 gravatar image215 ( 2015-11-07 02:21:50 -0500 )edit

You have to do something like this :

Mat m=acos(-1)*Mat::ones(256,256,CV_32FC1);
cv::FileStorage fs("image.yml", cv::FileStorage::WRITE);

It's a text format so it's not very efficiency in size but it's readable with a text editor. You can add tgz extension and it would be compressed.

You can use this technique too

LBerger gravatar imageLBerger ( 2015-11-07 02:28:40 -0500 )edit

what is Mat m=acos(-1)*Mat::ones(256,256,CV_32FC1); ?

Could you explain what you are doing?

215 gravatar image215 ( 2015-11-07 02:36:02 -0500 )edit

It's only an example : an image 256 rows by 256 column of type float(32F) with one channel (C1). All pixel values are equal to 3.14159274

LBerger gravatar imageLBerger ( 2015-11-07 02:39:29 -0500 )edit

'fs<<"Image"<<m'< p="">

fs is the file "image" ? i don't get and m is the Mat_<float> ?

215 gravatar image215 ( 2015-11-07 02:43:23 -0500 )edit

If you prefer :

Mat_<float> output=acos(-1)*Mat::ones(256,256,CV_32FC1);// your data in float
cv::FileStorage fs("image.yml", cv::FileStorage::WRITE);// image.yml file name
fs<<"ImageId"<<output; // text id of your data

to read you can do :

Mat mread;
cv::FileStorage fs("image.yml", cv::FileStorage::READ);
cout <<<float>(10,10)<<endl;

you can read this example

LBerger gravatar imageLBerger ( 2015-11-07 03:01:02 -0500 )edit

So i can't save as an viewable image and then process it again?

215 gravatar image215 ( 2015-11-07 03:06:27 -0500 )edit

That's another question you can just changed name file

LBerger gravatar imageLBerger ( 2015-11-07 03:20:58 -0500 )edit

both save it as black images.

215 gravatar image215 ( 2015-11-07 18:04:16 -0500 )edit

2 answers

Sort by Ā» oldest newest most voted

answered 2015-11-07 07:13:28 -0500

theodore gravatar image

.jeg, .png, etc. formats would not work in your case since they are ment to keep uchar values. Instead you might be able to use .bmp

Try to use the following and see the result imwrite("~/imgout.bmp", cv::Mat(512, 512, CV_32FC1, gray)); if that does not work then saving the Mat by using the OpenEXR format might do the trick. So, you will need to save your Mat with an .exr extension but be careful because you need Opencv with thatĀ support to be compiled in. Thus something like that: imwrite("~/imgout.exr", cv::Mat(512, 512, CV_32FC1, gray));

edit flag offensive delete link more


what is gray supposed to be?

215 gravatar image215 ( 2015-11-07 17:29:56 -0500 )edit

answered 2015-11-08 02:49:26 -0500

215 gravatar image

updated 2015-11-08 09:22:21 -0500

resolved with some help from here

    image.convertTo(image, CV_8UC3, 255.0); 
    imwrite(path, image);

edit flag offensive delete link more



Which is? Can you also post your solution instead of just a link.

theodore gravatar imagetheodore ( 2015-11-08 04:18:00 -0500 )edit

I really hope your Mat of floats only contains numbers in the range [0.0, 1.0], because otherwise you're ruining all your data with the implicit saturate_cast...

LorenaGdL gravatar imageLorenaGdL ( 2015-11-08 09:31:46 -0500 )edit

they are normalized within that range.

215 gravatar image215 ( 2015-11-08 09:42:35 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-11-07 01:49:16 -0500

Seen: 4,520 times

Last updated: Nov 08 '15