QT (based on C++) 载入图像并访问像素

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

ui界面

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

包含头文件:

#include <QGraphicsScene>

#include  <QGraphicsView>

#include <QFileDialog>

.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++)链接

graph digitizer (内含qt图像的像素级操作)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容