gpt4 book ai didi

c++,无符号字符之间的线性插值

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:33 25 4
gpt4 key购买 nike

没想到我遇到了奇怪的问题:

这里是普通线性拉格朗日插值的逻辑实现示例:

unsigned char mix(unsigned char x0, unsigned char x1, float position){
// LOGICALLY must be something like (real implementation should be
// with casts)...
return x0 + (x1 - x0) * position;
}

参数 x0x1 始终在 0 - 255 范围内。
参数位置始终在 0.0f - 1.0f 范围内。

真的,我尝试了大量的实现(使用不同的转换等),但它在我的情况下不起作用!它返回不正确的结果(看起来像变量溢出或类似的东西。在互联网上寻找解决方案整整一周后,我决定问问。可能有人遇到过类似的问题。

我正在使用 MSVC 2017 编译器(除语言级别外,大多数参数都是默认参数)。
操作系统 - Windows 10x64,Little Endian。

我做错了什么,问题的可能来源是什么?

更新:看来这个问题比我预期的要深(感谢您的回复)。

这是演示我的问题的微型 github 项目的链接: https://github.com/elRadiance/altitudeMapVisualiser

输出的 bmp 文件应该包含平滑的高度图。相反,它包含垃圾。如果我只使用 x0 或 x1 作为插值函数的结果(没有插值),它就可以工作。没有它 - 不会(产生垃圾)。

期望的结果(如此处,但在插值颜色中,平滑)
Desired result (as here, but in interpolated colors, smooth)

实际结果(更新后的最佳结果)
Actual result (updated, best result achieved)

运行它的主类:

#include "Visualiser.h"


int main() {

unsigned int width = 512;
unsigned int height = 512;


float* altitudes = new float[width * height];

float c;

for (int w = 0; w < width; w++) {
c = (2.0f * w / width) - 1.0f;
for (int h = 0; h < height; h++) {
altitudes[w*height + h] = c;
}
}

Visualiser::visualiseAltitudeMap("gggggggggg.bmp", width, height, altitudes);

delete(altitudes);
}

提前致谢!

最佳答案

已解决:谢天谢地@wololo。我项目中的错误不在计算中。

我应该使用“二进制”选项打开文件:

file.open("test.bin", std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);

如果没有它,数据中的某些点可能会遇到值为 10 的字节Windows环境下可以像换行一样处理,改为13

关于c++,无符号字符之间的线性插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48321888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com