【QT数字图像处理】(一)读取图像

这学期修“医学图像处理”这门课,最后的大作业是搭一个图像处理系统,要求用C++写界面,不能调库。我本科阶段学的是机械工程,没有接触过C++,只好边学边做。听说MFC已死,就选择了QT平台进行开发。

关于QT环境配置、新建工程的基本操作不进行说明,这次想实现的是读取图像的操作,效果如下:

代码实现 (.h):

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    //QLabel* imgLabel;

private:
    Ui::MainWindow *ui;
    QDockWidget* dock_Image;
    QLabel* imgLabel;
    QString currentPath;
    void Menu_File();
    void InitImage();
private slots:
    void File_new();
    void File_open();
    void File_save();
    void File_saveas();
};

#endif // MAINWINDOW_H

代码实现 (.c):

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("数字图像处理系统-by马");
    resize(800, 600);
    Menu_File();
    InitImage();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Menu_File(){
    QMenuBar* bar = menuBar();
    setMenuBar(bar);
    QMenu* fileMenu = bar->addMenu("文件");

    QAction* Act_file_new = new QAction("新建");
    connect(Act_file_new, SIGNAL(triggered()), this, SLOT(File_new()));
    QAction* Act_file_open = new QAction("打开");
    connect(Act_file_open, SIGNAL(triggered()), this, SLOT(File_open()));
    QAction* Act_file_save = new QAction("保存");
    connect(Act_file_save, SIGNAL(triggered()), this, SLOT(File_save()));
    QAction* Act_file_saveas = new QAction("另存为");
    connect(Act_file_saveas, SIGNAL(triggered()), this, SLOT(File_saveas()));

    fileMenu->addAction(Act_file_new);
    fileMenu->addAction(Act_file_open);
    fileMenu->addAction(Act_file_save);
    fileMenu->addAction(Act_file_saveas);
}
void MainWindow::InitImage(){
    dock_Image = new QDockWidget(tr("图像"),this);
    setCentralWidget(dock_Image);

    imgLabel = new QLabel(dock_Image);//imgLabel为什么只能用在构造函数 重复定义
    imgLabel->setScaledContents(true);

    QImage image = QImage(500, 500, QImage::Format_RGB32);
    image.fill(qRgb(255, 255, 255));
    imgLabel->setPixmap(QPixmap::fromImage(image));
    imgLabel->resize(image.width(), image.height());

    QScrollArea* scrollArea = new QScrollArea(this);
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setAlignment(Qt::AlignCenter);
    scrollArea->setWidget(imgLabel);
    dock_Image->setWidget((scrollArea));
}

void MainWindow::File_new(){
    QImage image = QImage(500, 500, QImage::Format_RGB32);
    image.fill(qRgb(255, 255, 255));
    imgLabel->setPixmap(QPixmap::fromImage(image));
    imgLabel->resize(image.width(), image.height());
    currentPath = "";
}
void MainWindow::File_open(){
    QString path = QFileDialog::getOpenFileName(this, tr("选择图像"), ".", tr("Images(*.jpg *.png *.bmp)"));
    if(!path.isEmpty()){
        QImage* img = new QImage();
        if(!(img->load(path))){
            QMessageBox::information(this, tr("错误"), tr("打开图像失败!"));
            delete img;
            return;
        }
        imgLabel->setPixmap(QPixmap::fromImage(*img));//有bug 已解决
        imgLabel->resize(img->width(), img->height());//有bug 已解决
        currentPath = path;
    }
}

void MainWindow::File_save(){
    if(currentPath.isEmpty()){
        QString path = QFileDialog::getSaveFileName(this, tr("保存图像"), ".", tr("Images(*.jpg *.png *.bmp)"));
        {
            if(!path.isEmpty())
                currentPath = path;
        }
    }
    QImage img = imgLabel->pixmap()->toImage();
    img.save(currentPath);
}
void MainWindow::File_saveas(){
    QString path = QFileDialog::getSaveFileName(this, tr("图像另存为"), ".", tr("Images(*.jpg *.png *.bmp)"));
    if(!path.isEmpty()){
        QImage img = imgLabel->pixmap()->toImage();
        img.save(path);
        currentPath = path;
    }
}

你可能感兴趣的:(QT数字图像处理,qt)