Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QImage(或一般图像)转换为RGB的3 1D阵列_C++_Qt_Qimage - Fatal编程技术网

C++ QImage(或一般图像)转换为RGB的3 1D阵列

C++ QImage(或一般图像)转换为RGB的3 1D阵列,c++,qt,qimage,C++,Qt,Qimage,我试图遵循的函数需要三个double类型的一维数组[19200]。以下阵列为RGB阵列,因此: double r[19200]; // r double g[19200]; // g double b[19200]; // b 到目前为止,我可以从QImage中提取像素信息并填充上面的数组 问题在于测试。我不知道如何做相反的事情:给定三个一维数组,如何从这些数据创建一个新的QImage? 我想确认我确实得到了正确的值。(像列与行的主要顺序让我感到怀疑)。因此,我试图从这三个一维数组中构造一个图

我试图遵循的函数需要三个double类型的一维数组[19200]。以下阵列为RGB阵列,因此:

double r[19200]; // r
double g[19200]; // g
double b[19200]; // b
到目前为止,我可以从QImage中提取像素信息并填充上面的数组

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


我想确认我确实得到了正确的值。(像列与行的主要顺序让我感到怀疑)。因此,我试图从这三个一维数组中构造一个图像

我真的不明白,如果你用一种方法解决了问题,为什么会有问题。过程基本相同:

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

for(intx=0;x看起来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
uchar*rgb_数组=新uchar[19200+19200+19200];
对于(int i=0,j=0;j<19200;++j)
{
//这里我们将从双精度范围0..1转换为整数范围0..255
rgb_阵列[i++]=r[j]*255;
rgb_阵列[i++]=g[j]*255;
rgb_阵列[i++]=b[j]*255;
}
{
QImage my_图像(rgb_数组,宽度,高度,QImage::Format_RGB888);
//用我的形象做事。。。
}
delete[]rgb_array;//注意,图像仍然存在时,您需要保留此数组