配置环境与系统开发搭建教程(铭籽)
基于上一篇中的界面程序,将图像的显示通过opencv实现,将opencv的环境加入至qt程序中,注意qt默认为release环境。
- 属性->VC 目录->包含目录中添加
- buildinclude
- buildincludeopencv
- buildincludeopencv2
- 属性->VC 目录->库目录中添加
- opencvbuildx64vc14lib
- 属性->链接器->输入->附加依赖项中添加
- opencv_world320d.lib
- opencv_world320.lib
代码修改
在QtGuiApplication1.h
中加入声明
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
#include <opencv.hpp>
using namespace cv;
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplication1Class ui;
//这里新加入的槽响应函数
private slots:
void func();
};
static QImage Mat2QImage(Mat& image);
在QtGuiApplication1.cpp
中加入实现
#include "QtGuiApplication1.h"
#include <opencv.hpp>
using namespace cv;
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//建立信号-槽链接
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(func()));
}
//响应函数的具体操作
void QtGuiApplication1::func()
{
ui.pushButton->setText(tr("OK"));
ui.label->setPixmap(QPixmap("./pic.jpg"));
QGraphicsScene *scene = new QGraphicsScene;
//QImage *image = new QImage();
//image->load("./pic.jpg");
//scene->addPixmap(QPixmap::fromImage(*image));
//ui.graphicsView->setScene(scene);
Mat img = imread("pic.jpg");
//MAT转换为Qt的格式
QImage cvimg = Mat2QImage(img);
scene->addPixmap(QPixmap::fromImage(cvimg));
ui.graphicsView->setScene(scene);
}
static QImage Mat2QImage(Mat& image)
{
QImage img;
if (image.channels() == 3) {
cvtColor(image, image, CV_BGR2RGB);
img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
image.cols*image.channels(), QImage::Format_RGB888);
}
else if (image.channels() == 1) {
img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
image.cols*image.channels(), QImage::Format_ARGB32);
}
else {
img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
image.cols*image.channels(), QImage::Format_RGB888);
}
return img;
}