gpt4 book ai didi

c++ - 获取 stb_image 中某个像素的 RGB

转载 作者:行者123 更新时间:2023-11-30 01:38:18 28 4
gpt4 key购买 nike

我创建并加载了这张图片:

int x, y, comps;
unsigned char* data = stbi_load(".//textures//heightMapTexture.png", &x, &y, &comps, 1);

现在,我如何获得此图像的某个像素的 RGB?

最佳答案

您正在使用每 channel 8 位接口(interface)。此外,您只请求一个 channel (给 stbi_load 的最后一个参数)。仅请求一个 channel ,您将无法获得 RGB 数据。

如果您使用 rgb 图像,您可能会在 comps 中得到 3 或 4 个,并且您希望在最后一个参数中至少有 3 个。

stbi_load 返回的 data 缓冲区将包含 8 位 * x * y * channelRequested 或 x * y * channelCount 字节。您可以这样访问 (i, j) 像素信息:

unsigned bytePerPixel = channelCount;
unsigned char* pixelOffset = data + (i + x * j) * bytePerPixel;
unsigned char r = pixelOffset[0];
unsigned char g = pixelOffset[1];
unsigned char b = pixelOffset[2];
unsigned char a = channelCount >= 4 ? pixelOffset[3] : 0xff;

这样您就可以获得 RGB(A) 每像素数据。

关于c++ - 获取 stb_image 中某个像素的 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48235421/

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