Sorry, this content is no longer available

Ask Your Question
0

imwrite a Mat_float keeping the image.

asked Nov 7 '15

215 gravatar image

updated Nov 8 '15

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?

Preview: (hide)

Comments

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

LBerger gravatar imageLBerger (Nov 7 '15)edit

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

215 gravatar image215 (Nov 7 '15)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 (Nov 7 '15)edit

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

Could you explain what you are doing?

215 gravatar image215 (Nov 7 '15)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 (Nov 7 '15)edit

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

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

215 gravatar image215 (Nov 7 '15)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 (Nov 7 '15)edit

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

215 gravatar image215 (Nov 7 '15)edit

That's another question you can just changed name file

LBerger gravatar imageLBerger (Nov 7 '15)edit

both save it as black images.

215 gravatar image215 (Nov 8 '15)edit

2 answers

Sort by » oldest newest most voted
0

answered Nov 8 '15

215 gravatar image

updated Nov 8 '15

resolved with some help from here

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

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

Preview: (hide)

Comments

1

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

theodore gravatar imagetheodore (Nov 8 '15)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 (Nov 8 '15)edit

they are normalized within that range.

215 gravatar image215 (Nov 8 '15)edit
0

answered Nov 7 '15

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));

Preview: (hide)

Comments

what is gray supposed to be?

215 gravatar image215 (Nov 7 '15)edit

Question Tools

1 follower

Stats

Asked: Nov 7 '15

Seen: 6,983 times

Last updated: Nov 08 '15