2021-09-28vs+opencv+QT简单的图像处理工程_opencv 用qt还是vs_二两山栀子的博客-CSDN博客
【vs+opencv+Qt搭建简单的图像处理界面】https://www.bilibili.com/video/BV16T411j7XQ?vd_source=0aeb782d0b9c2e6b0e0cdea3e2121eba
调试过程一直出现这种问题,后来改DEBUG为release就可以了
mainwindow.hpp
#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 mainwindowClass;
}
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类型 转 QImage类型
void display_MatInQT(QLabel* label, cv::Mat mat); // MAT对象 QT显示
private:
Ui::mainwindowClass* ui;
Mat image;
Mat mat_Gaussian;
Mat gray;
};
#endif // MAINWINDOW_H#pragma once
mainwindow.cpp
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::mainwindowClass)
{
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()
{
//调用窗口打开文件
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);
// 通过 lable 方式显示图片
display_MatInQT(ui->label, image);
}
else
{
QMessageBox::information(this, tr("提示"), tr("未成功载入图片!"), QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_2_clicked()
{
ui->label_1->clear();
if (image.data)
{
// 高斯模糊
GaussianBlur(image, mat_Gaussian, Size(29, 29), 0, 0);
display_MatInQT(ui->label_1, mat_Gaussian);
}
else
{
QMessageBox::information(this, tr("提示"), tr("未成功载入图片!"), QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_3_clicked()
{
ui->label_1->clear();
if (image.data)
{
// 灰度化
cvtColor(image, gray, COLOR_BGR2GRAY);
display_MatInQT(ui->label_1, gray);
}
else
{
QMessageBox::information(this, tr("提示"), tr("未成功载入图片!"), QMessageBox::Ok);
}
}
void MainWindow::on_pushButton_4_clicked()
{
ui->label_1->clear();
if (image.data)
{
// 边缘检测
Canny(image, gray, 150, 100, 3);
display_MatInQT(ui->label_1, gray);
}
else
{
QMessageBox::information(this, tr("提示"), tr("未成功载入图片!"), 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));
}