Ask Your Question
0

imwrite a Mat_float keeping the image.

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

215 gravatar image

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

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

Comments

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

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

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

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

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);
fs<<"Image"<<m;

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 -0600 )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 -0600 )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 -0600 )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 -0600 )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);
fs["ImageId"]>>mread;
cout << mread.at<float>(10,10)<<endl;

you can read this example

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

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

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

That's another question you can just changed name file

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

both save it as black images.

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

2 answers

Sort by Ā» oldest newest most voted
0

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

215 gravatar image

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

resolved with some help from here

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

http://stackoverflow.com/questions/10...

edit flag offensive delete link more

Comments

1

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

theodore gravatar imagetheodore ( 2015-11-08 04:18:00 -0600 )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 -0600 )edit

they are normalized within that range.

215 gravatar image215 ( 2015-11-08 09:42:35 -0600 )edit
0

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

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

Comments

what is gray supposed to be?

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

Question Tools

1 follower

Stats

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

Seen: 6,419 times

Last updated: Nov 08 '15