1

我试图遵循的函数需要三个 double[19200] 类型的一维数组。以下数组是 RGB 数组,例如:

double r[19200]; // r
double g[19200]; // g
double b[19200]; // b

到目前为止,我可以从 QImage 中提取像素信息并填充上述数组。

问题在于测试。我不知道如何做相反的事情:给定三个一维数组,我如何从这些数据创建一个新的 QImage?

我想验证我确实得到了正确的值。(诸如列与行主要顺序之类的事情让我怀疑)。结果,我试图从这三个一维数组中构造一个图像 QImage 。

4

2 回答 2

2

如果您设法以一种方式做到这一点,我真的不明白为什么您会遇到问题。过程基本相同:

for (int x=0; x<w; x++)
  for (int y=0; y<h; y++)
     image.setPixel(x,y, convertToRGB(r[x*w+y], ...);

convertToRGB假设图像具有维度,您要转换的内容和 RGB 值到浮点值的逆变换在哪里w*h。如果您发现这是错误的行主要/列主要变体,只需将其反转。

由于您没有提供有关如何进行色彩空间转换的信息,而且我们也不知道它是行优先还是列优先,因此无法为您提供更多帮助。

于 2011-06-09T05:07:08.477 回答
1

好吧,看起来 QImage 支持几种从像素数组加载的方法。

QImage(const uchar *data, int width, int height, Format format)
bool QImage::loadFromData(const uchar *buf, int len, const char *format=0)

使用第一个示例,如果您有提到的数组,那么您可能希望使用格式 QImage::Format_RGB888(来自 qimage.h)。

您需要自己知道宽度和高度。

最后,您需要将数组重新打包成一个 uchar* 数组

uchar* rgb_array = new uchar[19200+19200+19200];

for( int i = 0, j = 0; j < 19200; ++j )
{
    // here we convert from the double range 0..1 to the integer range 0..255
    rgb_array[i++] = r[j] * 255;
    rgb_array[i++] = g[j] * 255;
    rgb_array[i++] = b[j] * 255;
}

{
    QImage my_image( rgb_array, width, height, QImage::Format_RGB888 );

    // do stuff with my_image...
}

delete[] rgb_array; // note you need to hold onto this array while the image still exists
于 2011-06-09T05:14:01.777 回答