2021-09-28vs+opencv+QT简单的图像处理工程

初学C++完成一个小小的测试
前面已经配置好了opencv链接: VS2019+opencv4.5.3.

界面(QT)

先尝试采用FLTK库进行编写,只能显示个图片,关于这个的教程太少了,于是准备转战QT。
下载QT的时候老是报错说无法安全下载,于是我找到镜像文件在迅雷下载的,进入 Qt 5.9.0 的下载目录(https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.9/5.9.0/)链接: link.

复制链接到迅雷,安装可以就选中MSVC 2017 64-bit,其他就暂时不用了。
然后在vs界面扩展中下载QT插件,这样直接下载的版本会高一些,新建项目时会没有原始的gui项目,此为qt插件版本的问题,在官网(https://download.qt.io/official_releases/vsaddin/2.3.4/)上下载旧版本即可,此处下载的是qt2.3.4版本,注意关掉自动更新。

2021-09-28vs+opencv+QT简单的图像处理工程
接着创建新的项目
2021-09-28vs+opencv+QT简单的图像处理工程
然后直接运行main函数查看是否报错。

; 一个简单的opencv图像处理工程

2021-09-28vs+opencv+QT简单的图像处理工程

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <qlabel>
#include <qmainwindow>
#include <qfiledialog>
#include <opencv2 core core.hpp>
#include <opencv2 highgui highgui.hpp>
#include <opencv2 opencv.hpp>
#include <opencv2 imgproc imgproc.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();

    void on_pushButton_4_clicked();
private:
    QImage MatToQImage(const cv::Mat& mat);             // MAT&#x7C7B;&#x578B; &#x8F6C; QImage&#x7C7B;&#x578B;
    void display_MatInQT(QLabel* label, cv::Mat mat);   // MAT&#x5BF9;&#x8C61; QT&#x663E;&#x793A;

private:
    Ui::MainWindow* ui;
    Mat image;
    Mat mat_Gaussian;
    Mat gray;
};

#endif // MAINWINDOW_H#pragma once
</opencv2></opencv2></opencv2></opencv2></qfiledialog></qmainwindow></qlabel>

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qmessagebox>
MainWindow::MainWindow(QWidget* parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->pushButton_2->setEnabled(false);
    ui->pushButton_3->setEnabled(false);
    ui->pushButton_4->setEnabled(false);
}

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

void MainWindow::on_pushButton_clicked()
{
    //&#x8C03;&#x7528;&#x7A97;&#x53E3;&#x6253;&#x5F00;&#x6587;&#x4EF6;
    ui->label->clear();
    ui->label_1->clear();
    QString filename = QFileDialog::getOpenFileName(this,
        tr("open image"),
        ".",
        tr("Image file(*.png *.jpg *.bmp)"));
    image = imread(filename.toLocal8Bit().data());
    if (image.data) {
        ui->pushButton_2->setEnabled(true);
        ui->pushButton_3->setEnabled(true);
        ui->pushButton_4->setEnabled(true);
        // &#x901A;&#x8FC7; lable &#x65B9;&#x5F0F;&#x663E;&#x793A;&#x56FE;&#x7247;
        display_MatInQT(ui->label, image);
    }
    else
    {
        QMessageBox::information(this, tr("&#x63D0;&#x793A;"), tr("&#x672A;&#x6210;&#x529F;&#x8F7D;&#x5165;&#x56FE;&#x7247;&#xFF01;"), QMessageBox::Ok);
    }
}
void MainWindow::on_pushButton_2_clicked()
{
    ui->label_1->clear();

        if (image.data)
        {
            // &#x9AD8;&#x65AF;&#x6A21;&#x7CCA;
            GaussianBlur(image, mat_Gaussian, Size(29, 29), 0, 0);
            display_MatInQT(ui->label_1, mat_Gaussian);
        }
        else
        {
            QMessageBox::information(this, tr("&#x63D0;&#x793A;"), tr("&#x672A;&#x6210;&#x529F;&#x8F7D;&#x5165;&#x56FE;&#x7247;&#xFF01;"), QMessageBox::Ok);
        }

}
void MainWindow::on_pushButton_3_clicked()
{
    ui->label_1->clear();

    if (image.data)
    {
        // &#x7070;&#x5EA6;&#x5316;
        cvtColor(image, gray, COLOR_BGR2GRAY);
        display_MatInQT(ui->label_1, gray);
    }
    else
    {
        QMessageBox::information(this, tr("&#x63D0;&#x793A;"), tr("&#x672A;&#x6210;&#x529F;&#x8F7D;&#x5165;&#x56FE;&#x7247;&#xFF01;"), QMessageBox::Ok);
    }

}
void MainWindow::on_pushButton_4_clicked()
{
    ui->label_1->clear();

    if (image.data)
    {
        // &#x8FB9;&#x7F18;&#x68C0;&#x6D4B;

        Canny(image, gray, 150, 100, 3);
        display_MatInQT(ui->label_1, gray);
    }
    else
    {
        QMessageBox::information(this, tr("&#x63D0;&#x793A;"), tr("&#x672A;&#x6210;&#x529F;&#x8F7D;&#x5165;&#x56FE;&#x7247;&#xFF01;"), QMessageBox::Ok);
    }

}
QImage MainWindow::MatToQImage(const cv::Mat& mat)
{

    // 8-bits unsigned, NO. OF CHANNELS = 1
    if (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 Mat
        uchar* 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 = 3
    else if (mat.type() == CV_8UC3)
    {
        // Copy input Mat
        const uchar* pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage 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 Mat
        const uchar* pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage 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();
    }
}
//
void MainWindow::display_MatInQT(QLabel* label, Mat mat)
{

    label->setPixmap(QPixmap::fromImage(MatToQImage(mat)).scaled(label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

}

</qmessagebox>

mainwindow.ui需要自己设计,也可以直接更改成我这样试试

2021-09-28vs+opencv+QT简单的图像处理工程
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>934</width>
    <height>628</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>710</x>
      <y>20</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x6253;&#x5F00;&#x56FE;&#x50CF;</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>710</x>
      <y>110</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x9AD8;&#x65AF;&#x6A21;&#x7CCA;</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>20</y>
      <width>531</width>
      <height>261</height>
     </rect>
    </property>
    <property name="text">
     <string>
    </string></property>
   </widget>
   <widget class="QLabel" name="label_1">
    <property name="geometry">
     <rect>
      <x>13</x>
      <y>291</y>
      <width>521</width>
      <height>261</height>
     </rect>
    </property>
    <property name="text">
     <string>
    </string></property>
   </widget>
   <widget class="QPushButton" name="pushButton_3">
    <property name="geometry">
     <rect>
      <x>710</x>
      <y>200</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x7070;&#x5EA6;&#x5316;</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_4">
    <property name="geometry">
     <rect>
      <x>710</x>
      <y>280</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>&#x8FB9;&#x7F18;&#x68C0;&#x6D4B;</string>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox">
    <property name="geometry">
     <rect>
      <x>710</x>
      <y>360</y>
      <width>69</width>
      <height>22</height>
     </rect>
    </property>
    <item>
     <property name="text">
      <string>&#x4F55;&#x6C5F;</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>&#x4F55;</string>
     </property>
    </item>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>934</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar">
 </widget>
 <layoutdefault spacing="6" margin="11">
 <resources>
 <connections>
</connections></resources></layoutdefault></widget></ui>

最后结果就是这样

2021-09-28vs+opencv+QT简单的图像处理工程
可以自己添加修改功能,还在学,有问题可以在评论区交流。

Original: https://blog.csdn.net/regulationmyself/article/details/120530841
Author: 二两山栀子
Title: 2021-09-28vs+opencv+QT简单的图像处理工程

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/703167/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球