我正在构建一个QT GUI应用程序并使用QImage打开图像。 我的问题是我无法弄清楚如何使用QImage的bit()和scanline() 以像素级别访问的方法。
我看过这篇文章Qt QImage pixel manipulation problems 但这仅适用于每行的第一个像素。这是对的还是我错了?
提前致谢
答案 0 :(得分:14)
scanlines
对应于图像的高度,列对应于图像的宽度。
根据文档,原型看起来像uchar* QImage::scanline(int i)
或类似的const
版本。
但是,正如评论者指出的那样,因为数据依赖于机器架构和图像,所以 NOT 应该直接使用uchar *
。相反,请使用以下内容:
QRgb *rowData = (QRgb*)img.scanLine(row);
QRgb pixelData = rowData[col];
int red = qRed(pixelData);
答案 1 :(得分: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));
答案 2 :(得分:0)
答案对我不起作用。看起来,我的系统上的数据不是32位对齐的。 要获得正确的数据,我必须在我的系统上执行此操作:
0.0