QImage(或图像通常)转换为RGB的3个1D阵列

时间:2011-06-09 03:24:53

标签: c++ qt qimage

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

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

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

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

我想验证我确实得到了正确的值。 (像专栏和行主要订单之类的东西让我怀疑)。因此,我试图从这三个一维维数阵列构建一个QImage图像。

2 个答案:

答案 0 :(得分: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。如果您发现这是错误的行主要/列主要变体,请将其反转。

由于您没有提供有关如何进行色彩空间转换的信息,而且我们不知道它是行主要还是列主要,也不能帮助您。

答案 1 :(得分: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