1

I'm currenty trying to save a cv::Mat of type CV_64FC3 to my harddisk of float values between about 0 and 5. If you want to know why, read below. I have been looking into saving it in hdr format, but if I just call imwrite("myname.hdr", myCV_64FC3);, which they mention in this tutorial http://www.programmersought.com/article/33831043697/, it outputs a format that if you imread it again, is a UINT8 type and does not contain the float values anymore. How do you save this correctly?

Why do I want this?

I want to use this to save the standard deviations of my background, which have float values of about 0 to 5, can be a bit higher. Now I can cast them to CV_8UC3 and be done with it, but it will round my values down. While I want to use it to threshold at x times the standard deviation, I want to keep this precise, since 3 times a standard deviation of 0.7, is something else than zero.

Does anyone know a way to save this?

Amber Elferink
  • 132
  • 1
  • 10
  • imread by default always loads images as 3 channel 8 but there is a flag to load in the native format. – Micka Feb 23 '20 at 15:53
  • IMREAD_ANYDEPTH Python: cv.IMREAD_ANYDEPTH – Micka Feb 23 '20 at 15:56
  • In case it is not clear, Micka is saying you have already saved it correctly. The issue is that you need the extra flag when reloading again later. – Mark Setchell Feb 23 '20 at 16:00
  • @Micka, even if I call imread with `cv::Mat hdrStdDev = cv::imread(outputStDev, cv::IMREAD_UNCHANGED);` Or `cv::Mat hdrStdDev = cv::imread(outputStDev, cv::IMREAD_ANYDEPTH);` It still says the same. – Amber Elferink Feb 23 '20 at 16:01
  • 1
    you can use `matread` and `matwrite` as explained [here](https://stackoverflow.com/a/32357875/5008845) to save/load matrices with any type and depth. – Miki Feb 23 '20 at 18:18
  • sorry, dont know whether saving as .hdr works at all and preserves float64 precision. – Micka Feb 23 '20 at 18:48
  • @Miki, thanks, I will try that one – Amber Elferink Feb 23 '20 at 18:51
  • @Micka, as far as I know from raytracer skydomes, it should work at least for float32 precision. And I would be okay with that. But apparently opencv does weird stuff – Amber Elferink Feb 23 '20 at 18:52
  • can you save and upload such a .hdr file? – Micka Feb 23 '20 at 18:56
  • @Micka You mean the not working one I'm currently generating or one that is usually used for skydomes and such? – Amber Elferink Feb 23 '20 at 18:58
  • best would be one that is know to be a working hdr image file with more than 8 bit color depth – Micka Feb 23 '20 at 19:01
  • 1
    @Micka, there is a hdr file in this package that works, it opens as I would expect: https://www.hdri-hub.com/hdrishop/freesamples/freehdri/item/113-hdr-111-parking-space-free – Amber Elferink Feb 23 '20 at 19:09
  • @Miki, post an answer, I will give you an upvote there. I think a solution with saving an hdr image would be nicer, but this works. If I don't get any reaction on how to save an hdr image, I will accept your answer as well :). So don't give up on the hdr yet, it would be nice to view the image you save in external programs :) – Amber Elferink Feb 23 '20 at 19:20

0 Answers0