gpt4 book ai didi

c++ - CImg get_crop() 返回错误的像素/获取 CImg 的子图像

转载 作者:行者123 更新时间:2023-11-28 05:43:53 36 4
gpt4 key购买 nike

我正在尝试获取 CImg 的子图像。据我了解,方法 get_crop() 可以满足我的需要,但我获得的子图像不包含我期望它们具有的像素值。

unsigned char* StringTextureGenerator::GetTexture(int &width, int &height)
{
cil::CImg<unsigned char>* tmp = new cil::CImg<unsigned char>(512, 512, 1, 4);

*tmp = _alphabet.get_crop(0, 0, 0, 0, 511, 511, 0, 3);

width = tmp->width();
height = tmp->height();
return tmp->data();
}

_alphabet CImg 是 512x512x1x4 并且包含一个 rgba 图像。为了进行测试,我试图获取整个图像 x = 0 - 511,y = 0 - 511。结果是这样的:

enter image description here

这是预期的结果。当我从文件中加载它时,我得到了整个图像。

但是当我尝试获取子图像时,奇怪的事情发生了。

unsigned char* StringTextureGenerator::GetTexture(int &width, int &height)
{
cil::CImg<unsigned char>* tmp = new cil::CImg<unsigned char>(256, 256, 1, 4);

*tmp = _alphabet.get_crop(0, 0, 0, 0, 255, 255, 0, 3);

width = tmp->width();
height = tmp->height();
return tmp->data();
}

我在这里得到像素 x = 0 - 255,y = 0 - 255,我希望它是图片的左上四分之一。结果是这样的:

The result is this:

如您所见,我得到了每个 128x128 block 的左上四分之一。这不是我想要的。

最佳答案

不确定您的代码发生了什么,但这在我的机器上运行良好。我正在使用 CImg v169。我用这个作为测试图像:

enter image description here

然后运行这段代码:

#include <iostream>
#include "CImg.h"

using namespace cimg_library;
int main()
{
CImg<unsigned char> alphabet = CImg<unsigned char>("alphabet.png");
CImg<unsigned char> tmp = CImg<unsigned char>(512, 512, 1, 4);

//tmp = alphabet.get_crop(0, 0, 0, 0, 511, 511, 0, 3);
tmp = alphabet.get_crop(0, 0, 0, 0, 255, 255, 0, 3);
tmp.save_png("result.png");
}

得到这个,对我来说是正确的:

enter image description here

我正在编译选项:

-Dcimg_use_png -lpng -lz

关于c++ - CImg get_crop() 返回错误的像素/获取 CImg 的子图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36617640/

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