[英]QImage (or images generally) conversion to 3 1D arrays for RGB
我试图遵守的 function 需要三个 double[19200] 类型的一维 arrays。 以下 arrays 是 RGB arrays 使得:
double r[19200]; // r
double g[19200]; // g
double b[19200]; // b
到目前为止,我可以从 QImage 中提取像素信息并填充上面的 arrays。
问题在于测试。 我不知道如何做相反的事情:给定三个一维 arrays 我如何从这些数据中创建一个新的 QImage ?
我想验证我确实得到了正确的值。 (诸如列与行主要顺序之类的事情让我怀疑)。 结果,我试图从这三个一维维度 arrays 构建一个图像 QImage。
如果您设法以一种方式做到这一点,我真的不明白为什么您会遇到问题。 过程基本相同:
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
。 如果您发现这是错误的行主要/列主要变体,只需将其反转。
由于您没有提供有关如何进行色彩空间转换的信息,而且我们也不知道它是行优先还是列优先,因此无法为您提供更多帮助。
好吧,看起来 QImage 支持从像素 arrays 加载的几种方法。
QImage(const uchar *data, int width, int height, Format format)
bool QImage::loadFromData(const uchar *buf, int len, const char *format=0)
使用第一个示例,如果您有您提到的 arrays,那么您可能希望使用格式 QImage::Format_RGB888(来自 qimage.h)。
您需要自己知道宽度和高度。
最后,您需要将 arrays 重新打包到单个 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.