2011-06-09 69 views
1

我试图符合的函数需要三个类型为double的1维数组[19200]。下面阵列是RGB阵列,使得:QImage(或图像一般)转换为3个1D阵列的RGB

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

到目前为止,我可以从提取QImage的像素信息,并填充上述阵列。

问题出在测试。我不知道如何做相反的处理:给出三个1维数组我该如何从这些数据创建一个新的QImage?

我想验证我确实获得了正确的值。 (像列与行的主要命令给我怀疑)。因此,我试图从这三个一维维数组中构建一个图像QImage。

回答

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。如果您发现这是错误的行 - 主要/列主要变体,则反之。

由于您没有提供有关如何进行色彩空间转换的信息,而且我们也不知道它是否是行大或列大小,所以不能帮助您更多。

0

看起来好像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