日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT+OpenCV综合示例:载入、读取图片

發布時間:2024/7/23 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT+OpenCV综合示例:载入、读取图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

QT+OpenCV綜合示例:載入、讀取圖片

    • 1、代碼:
    • 2、運行結果:

下載1
GitHub:
下載2

1、代碼:

1)opencv_imwrite_Q.pro 添加:

INCLUDEPATH+= D:\opencv-3.1.0\opencv\build\includewin32:CONFIG(release, debug|release): LIBS += -LD:/opencv-3.1.0/opencv/build/x64/vc12/lib/ -lopencv_world310 else:win32:CONFIG(debug, debug|release): LIBS += -LD:/opencv-3.1.0/opencv/build/x64/vc12/lib/ -lopencv_world310d else:unix: LIBS += -LD:/opencv-3.1.0/opencv/build/x64/vc12/lib/ -lopencv_world310INCLUDEPATH += D:/opencv-3.1.0/opencv/build/x64/vc12 DEPENDPATH += D:/opencv-3.1.0/opencv/build/x64/vc12

2)主函數 main.cpp 添加:

#include "Widget_op.h" #include <QApplication> #include <QCoreApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget_op w;w.show();return a.exec(); }

3)Widget_op.h (主窗口頭文件)添加:

#ifdef WIN32 #pragma execution_character_set("utf-8") #endif #ifndef WIDGET_OP_H #define WIDGET_OP_H#include <QWidget> #include <QImage> #include <QLabel>#include "mylabel.h" #include <opencv2/opencv.hpp>namespace Ui { class Widget_op; }class Widget_op : public QWidget {Q_OBJECTpublic:explicit Widget_op(QWidget *parent = 0);~Widget_op();private slots:void on_pushButton_1_clicked();void on_pushButton_2_clicked();void on_pushButton_3_clicked();void on_pushButton_4_clicked();void on_pushButton_5_clicked();void on_myLabel_1_clicked();void on_myLabel_2_clicked();private:Ui::Widget_op *ui;private:QImage MatToQImage(const cv::Mat& mat); // MAT類型 轉 QImage類型void display_MatInQT(QLabel* label,cv::Mat mat); // MAT對象 QT顯示private:cv::Mat mat_origin;cv::Mat mat_logo;cv::Mat mat_add;cv::Mat mat_Gaussian; };#endif // WIDGET_OP_H

4)Widget_op.cpp (主窗口源文件)添加:
4.1)Mat轉QImage 函數:

QImage Widget_op::MatToQImage(const cv::Mat& mat) {// 8-bits unsigned, NO. OF CHANNELS = 1if(mat.type() == CV_8UC1){QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);// Set the color table (used to translate colour indexes to qRgb values)image.setColorCount(256);for(int i = 0; i < 256; i++){image.setColor(i, qRgb(i, i, i));}// Copy input Matuchar *pSrc = mat.data;for(int row = 0; row < mat.rows; row ++){uchar *pDest = image.scanLine(row);memcpy(pDest, pSrc, mat.cols);pSrc += mat.step;}return image;}// 8-bits unsigned, NO. OF CHANNELS = 3else if(mat.type() == CV_8UC3){// Copy input Matconst uchar *pSrc = (const uchar*)mat.data;// Create QImage with same dimensions as input MatQImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_RGB888);return image.rgbSwapped();}else if(mat.type() == CV_8UC4){//qDebug() << "CV_8UC4";// Copy input Matconst uchar *pSrc = (const uchar*)mat.data;// Create QImage with same dimensions as input MatQImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_ARGB32);return image.copy();}else{//qDebug() << "ERROR: Mat could not be converted to QImage.";return QImage();} }

4.2)MAT 顯示于 QT函數:

// void Widget_op::display_MatInQT(QLabel* label,Mat mat) {// QImage image=MatToQImage(mat_1);// QPixmap pix_temp= QPixmap::fromImage(image);// QPixmap pix =pix_temp.scaled(ui->label_1->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);// ui->label_1->setPixmap(pix);label->setPixmap(QPixmap::fromImage(MatToQImage(mat)).scaled(label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));// // 通過 Graphics View 方式打開圖片// QGraphicsScene *scene = new QGraphicsScene; // 創建圖形視圖場景對象// scene->addPixmap(QPixmap::fromImage(MatToQImage(mat_origin))); // 添加pixmap// ui->graphicsView->setScene(scene);// ui->graphicsView->show(); } #include "Widget_op.h" #include "ui_Widget_op.h"#include <QFileDialog> #include <QMessageBox>using namespace cv;Widget_op::Widget_op(QWidget *parent) :QWidget(parent),ui(new Ui::Widget_op) {ui->setupUi(this);// 關聯信號和槽QObject::connect(ui->label_1, &myLabel::clicked, this, &Widget_op::on_myLabel_1_clicked);QObject::connect(ui->label_2, &myLabel::clicked, this, &Widget_op::on_myLabel_2_clicked);// 窗口固定尺寸this->setFixedSize(900,675);//設置窗口的標題欄只有關閉、最小化的按鈕// 置頂窗口,不搶焦點this->setWindowFlags(Qt::WindowCloseButtonHint |Qt::WindowMinimizeButtonHint |Qt::WindowStaysOnTopHint);ui->label_1->setText(tr("點擊加載圖片..."));ui->label_1->setFont(QFont("微軟雅黑",20,QFont::Bold,true));ui->label_1->setStyleSheet("color:blue; background-color:lightGray;");//設置文本顏色+背景顏色(前景色)ui->label_2->setText(tr("點擊加載圖片..."));ui->label_2->setFont(QFont("微軟雅黑",20,QFont::Bold,true));ui->label_2->setStyleSheet("color:blue; background-color:lightGray;");//設置文本顏色+背景顏色(前景色)ui->label_3->setStyleSheet("background-color:lightGray;");ui->label_4->setStyleSheet("background-color:lightGray;");ui->pushButton_3->setEnabled(false);ui->pushButton_4->setEnabled(false);ui->pushButton_5->setEnabled(false);}Widget_op::~Widget_op() {delete ui; }// 打開顯示 mat_1 void Widget_op::on_pushButton_1_clicked() {QString fileName = QFileDialog::getOpenFileName(this, tr("文件對話框"),"F:/C++/2. OPENCV 3.1.0/opencv_imwrite_Q", tr("圖片文件(*.png *.jpg *.jpeg *.bmp *.tif *.tiff);;所有文件(*)"));if(!fileName.isEmpty()){mat_origin= imread(fileName.toLocal8Bit().data());if(mat_origin.data){// 通過 lable 方式顯示圖片display_MatInQT( ui->label_1,mat_origin);}else{QMessageBox::information(this, tr("提示"),tr("未成功載入圖片!"), QMessageBox::Ok);}} }// 打開顯示 mat_2 void Widget_op::on_pushButton_2_clicked() {QString fileName = QFileDialog::getOpenFileName(this, tr("文件對話框"),"F:/C++/2. OPENCV 3.1.0/opencv_imwrite_Q", tr("圖片文件(*.png *.jpg *.jpeg *.bmp *.tif *.tiff);;所有文件(*)"));if(!fileName.isEmpty()){mat_logo = imread(fileName.toLocal8Bit().data(),1); // 總是轉換圖像到彩色圖,在返回if(mat_logo.data){display_MatInQT( ui->label_2,mat_logo);}else{QMessageBox::information(this, tr("提示"),tr("未成功載入圖片!"), QMessageBox::Ok);}}if(mat_origin.data&&mat_logo.data){ui->pushButton_3->setEnabled(true);ui->pushButton_5->setEnabled(true);} }// 混合 mat_1和mat_2 并顯示 void Widget_op::on_pushButton_3_clicked() {if(!mat_add.data){if(mat_origin.data&&mat_logo.data){mat_add=mat_origin.clone();Mat imageROI = mat_add(Rect(800, 350, mat_logo.cols,mat_logo.rows));// 加權混合if(imageROI.type()==mat_logo.type()){addWeighted(imageROI, 0.5, mat_logo, 0.3, 0.0, imageROI);display_MatInQT(ui->label_3,mat_add);}else{QMessageBox::information(this, tr("提示"),tr("類型不同,無法混合!"), QMessageBox::Ok);}ui->pushButton_4->setEnabled(true);}else{QMessageBox::critical(this, tr("錯誤"),tr("未成功載入Mat1和Mat2!"), QMessageBox::Ok);}} }// 圖像處理:高斯濾波(模糊) void Widget_op::on_pushButton_5_clicked() {if(!mat_Gaussian.data){if(mat_add.data){// 高斯模糊GaussianBlur(mat_add,mat_Gaussian,Size(29,29),0,0);display_MatInQT(ui->label_4,mat_Gaussian);}else{QMessageBox::information(this, tr("提示"),tr("未成功載入加權混合圖片!"), QMessageBox::Ok);}} }// 保存文件 void Widget_op::on_pushButton_4_clicked() {if(mat_add.data){QString filename = QFileDialog::getSaveFileName(this,tr("保存對話框"),"F:/C++/2. OPENCV 3.1.0/opencv_imwrite_Q",tr("*.jpg;; *.bmp;; *.png;; *.tif;; *.GIF"));if(!filename.isEmpty()){imwrite(filename.toLocal8Bit().data(),mat_add);}}else{QMessageBox::information(this, tr("提示"),tr("未處理完圖片!"), QMessageBox::Ok);} }// 點擊標簽1 void Widget_op::on_myLabel_1_clicked() {// 觸發 按鈕1 點擊事件ui->pushButton_1->click(); }// 點擊標簽2 void Widget_op::on_myLabel_2_clicked() {// 觸發 按鈕2 點擊事件ui->pushButton_2->click(); }

5)mylabel.h (自定義QLabel類頭文件) 添加:

#ifndef MYLABEL_H #define MYLABEL_H #include <QLabel> #include <QMouseEvent>class myLabel : public QLabel {Q_OBJECT public:explicit myLabel(QWidget *parent = 0);protected:virtual void mousePressEvent(QMouseEvent *event);signals:void clicked(void); // 聲明鼠標左擊中信號};#endif // MYLABEL_H

6)mylabel.cpp (自定義QLabel類源文件)添加:

#include "mylabel.h" #include <QMessageBox>myLabel::myLabel(QWidget *parent):QLabel(parent) {}void myLabel::mousePressEvent(QMouseEvent *event) {if(event->button()==Qt::LeftButton){emit clicked(); // 發射信號} }

6)Widget_op.ui (界面文件)設計:

2、運行結果:

總結

以上是生活随笔為你收集整理的QT+OpenCV综合示例:载入、读取图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。