【发布时间】:2019-04-05 19:35:00
【问题描述】:
我有一个 256x256 的 floats 数组,它代表一个高度图。
我想将其导出为每像素 16 位的 RAW 图像。将float 转换为uint16_t 的正确方法是什么。 (我知道精度损失)
我的快速而肮脏的测试代码:
void ExportHeightmap(const Vector<float>& rHeights)
{
std::vector<uint16_t> output(256 * 256);
float min = std::numeric_limits<float>::max();
float max = std::numeric_limits<float>::min();
for (size_t i = 0; i < output.size(); ++i)
{
float f = rHeights[i];
if (min > f) min = f;
if (max < f) max = f;
output[i] = static_cast<uint16_t>(rHeights[i]);
}
std::cout << " Min: " << min << std::endl; // -49.77
std::cout << " Max: " << max << std::endl; // 357.84
std::fstream file("Heightmap.raw", std::ios::out | std::ios::binary);
file.write((char*)output.data(), output.size() * sizeof(uint16_t));
}
编辑:我的目标是将应用程序中制作的高度图导出到图像。
【问题讨论】:
-
为什么是
std::vector<char> output((256 * 256) * sizeof(uint16_t), 0);?为什么不简单地std::vector<uint16_t>(256 * 256, 0)? -
确实,你应该有一个 std::vector
。其他问题如果它是图像,它已经在 0 和 1 之间标准化了吗? -
将
float转换为uint16_t确实意味着丢失所有小数部分——它只保留了整数部分。这是故意的吗? -
您希望
float高度图的值在哪个范围内?