c++imageimage-processingvisual-c++cimg

Save exr/pfm to bitmap CImg


I am trying to convert some bitmap files into custom images (exr, pfm, whatever), and after that, back to bitmap:

CImg<float> image(_T("D:\\Temp\\test.bmp"));
image.normalize(0.0, 1.0);
image.save_exr(_T("D:\\Temp\\test.exr"));

and goes fine (same for .pfm file), I mean the exr file is ok, same for pfm file.

But when this exr, or pfm file I trying to convert back to bitmap:

CImg<float> image;
image.load_exr(_T("D:\\Temp\\test.exr"));    // image.load_pfm(_T("D:\\Tempx\\test.pfm"));
image.save_bmp(_T("D:\\Temp\\test2.bmp"));

the result, test2.bmp is black. Complete. Why ? What I am doing wrong ?


Solution

  • Some image formats support saving as float, but most formats save as unsigned 8 bit integer (or uint8), meaning normal image values are from 0 to 255. If you try to save an array that is made up of floats from 0 to 1 into a format that does not support floats, your values will most likely be converted to integers. When you display your image with most image-viewing software, it'll appear entirely black since 0 is black and 1 is almost black.

    Most likely when you save your image to bitmap it is trying to convert the values to uint8 but not scaling properly. You can fix this by multiplying normalized values between 0 and 1 by 255. img = int(img*255) or using numpy img = (img*255).astype(np.uint8).

    It is also possible that somehow your save function is able to preserve floating point values in the bitmap format. However your image viewing software might not know how to view/display a float image. Perhaps use some imshow function (matplotlib.pyplot can easily display floating point grayscale arrays) between each line of code to check if the arrays are consistent with what you expect them to be.