刚装上Qt和OpenCV,配置好后,测试的第一个程序。
项目展示
项目链接
小项目(0积分),有兴趣的可以下载下来玩一玩
https://download.csdn.net/download/weixin_45525272/75098303
项目代码
界面
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QFileDialog> #include <QDebug> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp> using namespace cv; namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_3_clicked(); private: Ui::MainWindow *ui; QImage img; Mat src; bool colorful = true; QString img_name; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <iostream> #include <QTextCodec> #include <string> #include <QString> using namespace std; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { img_name = QFileDialog::getOpenFileName( this, tr("Open Image"), ".",tr("Image Files(*.png *.jpg *.jpeg *.bmp)")); QTextCodec *code = QTextCodec::codecForName("gb18030"); string name = code->fromUnicode(img_name).data(); if(name.length()<1) return; qDebug() << "打开图片:" << img_name; try{ src=imread(img_name.toUtf8().data()); namedWindow( "src", WINDOW_NORMAL); if(!src.empty()){ imshow("src",src); } }catch(Exception e){ cout << e.err << endl; } } void MainWindow::on_pushButton_2_clicked() { exit(0); } //实现灰度图和彩色图之间的相互转换 void MainWindow::on_pushButton_3_clicked() { try{ if(colorful) { cvtColor(src, src, COLOR_RGB2GRAY); imshow("src",src); ui->pushButton_3->setText("转为原图"); } else { src=imread(img_name.toUtf8().data());// imshow("src",src); ui->pushButton_3->setText("转为灰度图"); } colorful = !colorful; }catch(Exception e){ cout << e.err << endl; } }