搭建Qt5.9.3的openCV开发环境笔记

重点参考大神博文:

OpenCV开发笔记(〇):使用mingw530_32编译openCV3.4.1源码,搭建Qt5.9.3的openCV开发环境

Qt+OpenCV环境搭建

安装qt opencv 环境Win7(cmake3.9.1和Opencv3.3.0)

编译OpenCV3.4.1时出现的一些问题

第一把用的OpenCV 3.3版本,编译到88%出错,通不过。换了OpenCV 3.4.8版本

搭建Qt5.9.3的openCV开发环境笔记_第1张图片

测试代码

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // read an image
    cv::Mat image = cv::imread("E://test_pic//bmp//411_300.bmp", 1);
    // create image window named "My Image"
    cv::namedWindow("My Image");
    // show the image on window
    cv::imshow("My Image", image);

}

MainWindow::~MainWindow()
{
    delete ui;
}
#-------------------------------------------------
#
# Project created by QtCreator 2020-02-11T16:35:22
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = testCV
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


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui


INCLUDEPATH += E:\Qt\opencv\build\include

LIBS += E:\Qt\opencv\build\bin\libopencv_core348.dll
LIBS += E:\Qt\opencv\build\bin\libopencv_highgui348.dll
LIBS += E:\Qt\opencv\build\bin\libopencv_imgcodecs348.dll
LIBS += E:\Qt\opencv\build\bin\libopencv_imgproc348.dll
LIBS += E:\Qt\opencv\build\bin\libopencv_features2d348.dll
LIBS += E:\Qt\opencv\build\bin\libopencv_calib3d348.dll

测试代码:

利用openCV库,录制视频,并保存

#include 
#include 
#include 
using namespace cv;


#undef main
int main()
{
//    Mat image;
//    image=imread("D:\\test_pic\\01.jpg");//括号里更改为自己图像的路径
//    namedWindow("显示");
//    imshow("显示",image);

    cv::Mat frame;
    cv::VideoCapture capture;
    bool stop = false;
    char *dirSave = (char*)"MergeVideo.avi";
    Size mSize = Size(640, 480);
    
    cv::VideoWriter writer(dirSave, CV_FOURCC('M', 'J', 'P', 'G'), 20, mSize);

    if (capture.isOpened())
        capture.release();     //decide if capture is already opened; if so,close it

    capture.open(0);
//        rate= capture.get(CV_CAP_PROP_FPS);
//        int width = capture.get(CV_CAP_PROP_FRAME_WIDTH);  // 640
//        int height = capture.get(CV_CAP_PROP_FRAME_HEIGHT); // 480
    
    while (capture.isOpened()&&!stop)
    {
        capture >> frame;
        if (!frame.empty())
        {
            imshow("frame",frame);
            writer << frame;

            if (waitKey(30) >= 0)
                stop = true;
        }
    }

//    waitKey(0);
    return 0;
}
#-------------------------------------------------
#
# Project created by QtCreator 2019-11-18T17:12:16
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = testCV
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


SOURCES += main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui


INCLUDEPATH+=D:\Qt\OpenCV_3.3_Source\build\include

             D:\Qt\OpenCV_3.3_Source\build\include\opencv

             D:\Qt\OpenCV_3.3_Source\build\include\opencv2

LIBS += D:\Qt\OpenCV_3.3_Build\lib\libopencv_*.a



参考文档:

Qt5下实现摄像头预览及捕获图像方法二(openCV3与Qt5交互使用)

qt显示通过opencv显示摄像头

QT 5.3 应用程序打包成可执行程序exe

你可能感兴趣的:(图像处理,C/C++,QT)