Qt5和opencv4简单结合(八)

效果图:

Qt5和opencv4简单结合(八)_第1张图片Qt5和opencv4简单结合(八)_第2张图片

思路:引入opencv头文件,按下open打开图片,给label加个边框。

.pro(opencv4是cmake好的)

#opencv 
LIBS += -LD:/opencv4.2/opencv/newbuild/newbuild/install/x64/vc14/lib/ -lopencv_world420d
INCLUDEPATH += D:/opencv4.2/opencv/newbuild/newbuild/install/include/
D:/opencv4.2/opencv/newbuild/newbuild/install/include/opencv2/
LIBS +=D:/Qt/5.14.1/msvc2015_64/lib/qtmaind.lib

.h

private slots:
    void on_pushButton_clicked();//close_button
    void on_pushButton_2_clicked();//open_button

.cpp

#include "widget.h" 
#include "ui_widget.h"

#include
using namespace cv;
using namespace std;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("open image");//窗口标题
    //设置label边框样式
    ui->label->setFrameShape(QFrame::Box);
    ui->label->setFrameShadow(QFrame::Sunken);
    ui->label->setLineWidth(5);
    ui->label->setLineWidth(10);

}

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


void Widget::on_pushButton_clicked()
{
    close();
}

void Widget::on_pushButton_2_clicked()
{
    Mat src=imread("D:/image/1.jpg");
    Mat dst;
    cvtColor(src,dst,COLOR_BGR2RGB);
    ui->label->setPixmap(QPixmap::fromImage(QImage(dst.data,
                                                   dst.cols,
                                                   dst.rows,
                                                   dst.step,
                                                   QImage::Format_BGR888)));
}

你可能感兴趣的:(Qt,opencv)