代码之家  ›  专栏  ›  技术社区  ›  theosem

qt qImage像素操作

  •  13
  • theosem  · 技术社区  · 15 年前

    我正在构建一个qt gui应用程序,并使用qimage打开图像。 我的问题是我不知道如何使用qimage的bit()和scanline()。 方法以每像素级别访问。

    我看过这个帖子 Qt QImage pixel manipulation problems 但这只适用于每行的第一个像素。这是正确的还是我都弄错了?

    提前谢谢

    3 回复  |  直到 8 年前
        1
  •  14
  •   Kaleb Pederson    11 年前

    这个 scanlines 对应于图像的高度,列对应于图像的宽度。

    根据文件,原型看起来像 uchar* QImage::scanline(int i) ,或类似 const 版本。

    但是,正如一位评论者指出的那样,由于数据依赖于机器的体系结构和图像,所以您应该 不是 使用 uchar * 直接。相反,请使用如下内容:

    QRgb *rowData = (QRgb*)img.scanLine(row);
    QRgb pixelData = rowData[col];
    int red = qRed(pixelData);
    
        2
  •  13
  •   LeviX    14 年前

    从Kaleb的帖子上看,这可能不是很明显,但下面的内容适用于在格式为“rgb32”的图像上设置像素。

    // Get the line we want
    QRgb *line = (QRgb *)image->scanLine(row_index);
    
    // Go to the pixel we want
    line += col_index;
    
    // Actually set the pixel
    *line = qRgb(qRed(color), qGreen(color), qBlue(color));
    
        3
  •  0
  •   MvHorssen    8 年前

    答案对我不起作用。看起来,我的系统中的数据不是32位对齐的。 要获得正确的数据,我必须在我的系统上执行以下操作:

    for(uint32_t Y = 0; Y < mHeight; ++Y)
    {
        uint8_t* pPixel = Image.scanLine(Y);
    
        for(uint32_t X = 0; X < mWidth; ++X)
        {
          const int Blue = *pPixel++;
          const int Green = *pPixel++;
          const int Red = *pPixel++;
    
          uint8_t GrayscalePixel = (0.21f * Red) + (0.72f * Green) + (0.07 * Blue);
        }
    }