QGraphicsView显示pgm注意事项

本文介绍了如何在Qt中使用QGraphicsView加载PGM图像文件,包括读取像素值、转换格式、显示像素值以及添加到QGraphicsScene中的过程。
摘要由CSDN通过智能技术生成

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值