14

我正在构建一个 QT GUI 应用程序并使用 QImage 打开图像。我的问题是我不知道如何使用 QImage 的 bit() 和 scanline() 方法来获取每个像素级别的访问权限。

我已经看到这篇文章Qt QImage 像素操作问题 ,但这仅适用于每行的第一个像素。这是正确的还是我完全错了?

提前致谢

4

3 回答 3

14

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

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

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

QRgb *rowData = (QRgb*)img.scanLine(row);
QRgb pixelData = rowData[col];
int red = qRed(pixelData);
于 2010-01-19T16:41:18.123 回答
13

从 Kaleb 的帖子中可能不会立即显而易见,但以下适用于在 Format_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));
于 2011-05-23T20:46:58.807 回答
0

答案对我不起作用。看起来,我的系统上的数据不是 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);
    }
}
于 2016-12-03T09:45:49.457 回答