vs2017配置opencv4.2及QTcreator配置opencv4.2在界面显示图像

前面有配置好了pcl,接下来再配置下常用的opencv

opencv4.2安装版本见如下百度网盘链接

链接:https://pan.baidu.com/s/1rndS4-C777kU3TcUggtG0A 
提取码:e5pz 
下载完毕后,按照默认安装即可(按照自己的路径来)

界面上添加button按钮及picture control控件,然后include目录添加如下路径:

D:\mycode\0-PCL_VTK\OpenCV4.2.0\opencv\build\include

D:\mycode\0-PCL_VTK\OpenCV4.2.0\opencv\build\include\opencv2

连接器常规项附加库目录输入如下路径:

D:\mycode\0-PCL_VTK\OpenCV4.2.0\opencv\build\x64\vc15\lib

链接器输入附加依赖项输入opencv_world420d.lib

属性页调试环境里写入path=D:\mycode\0-PCL_VTK\OpenCV4.2.0\opencv\build\x64\vc15\bin

加入头文件

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui_c.h"

按钮的回调函数里代码

// TODO: 在此添加控件通知处理程序代码
    namedWindow("ImageShow");//创建OpenCV窗口
    HWND hWnd = (HWND)cvGetWindowHandle("ImageShow");//嵌套opencv窗口
    HWND hParent = ::GetParent(hWnd);
    ::SetParent(hWnd, GetDlgItem(IDC_STATIC)->m_hWnd);
    ::ShowWindow(hParent, SW_HIDE);
    Mat mat = imread("D:\\cat003.jpg");//opencv读取图片
    imshow("ImageShow", mat);//opencv显示图片
    waitKey(1);

 

运行显示界面为:

 

下面讲如何在QTcreator界面上显示图像

新建一个qt工程

test_opencv.prp中信息编辑如下:

#-------------------------------------------------
#
# Project created by QtCreator 2020-11-02T19:53:47
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test_opencv
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

INCLUDEPATH += D:\mycode\0-PCL_VTK\OpenCV4.2.0\opencv\build\include


CONFIG(debug,debug|release){
LIBS += -LD:/mycode/0-PCL_VTK/OpenCV4.2.0/opencv/build/x64/vc15/lib \
        opencv_world420d.lib
} else {
LIBS += -LD:/mycode/0-PCL_VTK/OpenCV4.2.0/opencv/build/x64/vc15/lib \
        opencv_world420.lib
}

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

界面上拖放一个label控件

 

mainwindow.cpp中代码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "opencv2/opencv.hpp"
#include <opencv2/imgproc/types_c.h>
using namespace cv;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Mat image = imread("D:\\cat003.jpg");
    imshow("out", image);
    waitKey(0);


    Mat temp;
    cvtColor(image, temp, CV_BGR2RGB);//BGR convert to RGB
    QImage Qtemp = QImage((const unsigned char*)(temp.data), temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
    ui->label->setPixmap(QPixmap::fromImage(Qtemp));
    ui->label->resize(Qtemp.size());
    ui->label->show();


}

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

 

运行结果:

Guess you like

Origin blog.csdn.net/jiugeshao/article/details/109459501