【问题标题】:QImage (or images generally) conversion to 3 1D arrays for RGBQImage(或通常的图像)转换为 RGB 的 3 个 1D 数组
【发布时间】:2011-06-09 03:24:52
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: c++ qt qimage


    【解决方案1】:

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

    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。如果您发现这是错误的行主要/列主要变体,只需将其反转即可。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-27
        • 1970-01-01
        • 2010-12-06
        • 1970-01-01
        • 2018-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多