qt快速加载图片_QT (based on C++) 载入图像并访问像素

本文介绍了如何在QT应用中快速加载图片并访问像素。首先,通过QGraphicsScene和QGraphicsView来展示图像,然后利用QPixmap加载图片到场景中。此外,还提到使用QImage转换QPixmap,以便直接访问图像的像素,获取RGB值或灰度值。
摘要由CSDN通过智能技术生成

1. 通过QGraphicsScene()和QGraphicsView ()载入图像

ui界面

在*.ui界面设计文件中插入 Display Widget -> Graphics View 控件。

包含头文件:

#include

#include 

#include

.h文件中定义变量:

QGraphicsScene *graph=new QGraphicsScene();

QPixmap map;

.cpp文件中函数:

主窗口函数中添加  ui->graphicsView->setScene(graph)如下

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent)

, ui(new Ui::MainWindow)

{

ui->setupUi(this);

ui->graphicsView->setScene(graph);  // connecting the graph to the View

}

载入图像函数如下

void MainWindow::on_load_clicked()

{

QString filename=QFileDialog::getOpenFileName(this,tr("Load Note"),tr("*.png")); // Allows the usr to choose an input file

map = QPixmap(filename); // Saves the file in a a QPixmap object

graph->addPixmap(map); // Adds the pixmap to the scene

}

2. 通过QImage()操作graph像素

note: qt中只有QImage数据类型可以直接访问像素(貌似)。

.cpp文件中函数:

QImage mimg = map.toImage();

mimg.pixel(int x, int y) 返回像素点(x,y)的RGB值

qGray(mimg.pixel(int x, int y)) 返回像素点(x,y)的灰度值

源程序graph digitizer (qt c++)链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值