我正在构建一个QT GUI应用程序,并使用QImage打开图像。
我的问题是,我无法弄清如何使用QImage的bit()和scanline()方法来访问每个像素级别。
我看到了这篇文章Qt QImage像素操作问题 但这仅适用于每行的第一个像素。这是正确的还是我搞错了?
提前致谢
我看到了这篇文章Qt QImage像素操作问题 但这仅适用于每行的第一个像素。这是正确的还是我搞错了?
提前致谢
Scanlines
对应于图像的高度,而列则对应于图像的宽度。
根据文档,原型看起来像是 uchar* QImage::scanline(int i)
,或者一个类似的 const
版本。
但正如一位评论者指出的,由于数据依赖于机器架构和图像,因此不应直接使用 uchar *
。相反,可以使用以下内容:
QRgb *rowData = (QRgb*)img.scanLine(row);
QRgb pixelData = rowData[col];
int red = qRed(pixelData);
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));
这个答案对我没用。看起来,在我的系统上,数据没有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);
}
}