QGraphicsView显示pgm注意事项
//读取像素值(将像素值存放在数组)
QImage image;
image.load(“C:/xxxxx/xxxx/1.pgm”);
image = image.convertToFormat(QImage::Format_Grayscale8);
int width = image.width();
int height = image.height();
qDebug()<<“width=”<<width<<“,height=”<<height;
uchar data[width*height];
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
// 获取像素值,必须从rgb颜色获取,转为uint
QColor color = image.pixelColor(x, y);
uint grayValue = color.rgb();
data[y * width + x] = grayValue;
}
}
//显示地图像素值
QImage imageShow(width, height, QImage::Format_Grayscale8);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
uint grayValue = data[y * width + x];
//显示,必须转换为qRgb
QRgb color2 = qRgb(grayValue, grayValue, grayValue);
imageShow.setPixel(x, y, color2);
}
}
m_scene->setSceneRect(0,0,width+100,height+100);
QGraphicsPixmapItem *slam = m_scene->addPixmap(QPixmap::fromImage(imageShow));
slam->setZValue(0);