简体   繁体   English

将 exr/pfm 保存到位图 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:我正在尝试将一些位图文件转换为自定义图像( exrpfm等),然后再转换回位图:

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.一切顺利( .pfm 文件相同),我的意思是exr文件没问题, pfm文件也是如此。

But when this exr , or pfm file I trying to convert back to bitmap:但是当这个exrpfm文件我试图转换回位图时:

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.结果,test2.bmp 是黑色的。 Complete.完全的。 Why ?为什么 ? What I am doing wrong ?我做错了什么?

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.一些图像格式支持保存为浮点数,但大多数格式保存为无符号 8 位整数(或 uint8),这意味着正常图像值是从 0 到 255。如果您尝试将一个由 0 到 1 的浮点数组成的数组保存到不支持浮点数的格式,您的值很可能会转换为整数。 When you display your image with most image-viewing software, it'll appear entirely black since 0 is black and 1 is almost black.当您使用大多数图像查看软件显示图像时,它会显示为全黑,因为 0 是黑色而 1 几乎是黑色。

Most likely when you save your image to bitmap it is trying to convert the values to uint8 but not scaling properly.最有可能的是,当您将图像保存为位图时,它会尝试将值转换为 uint8 但无法正确缩放。 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) .您可以通过将 0 和 1 之间的归一化值乘以 255 来解决此问题。 img = int(img*255)或使用 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.也许在每行代码之间使用一些 imshow 函数(matplotlib.pyplot 可以轻松显示浮点灰度数组)来检查数组是否与您期望的一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM