目录
1. MSVC版本
1.1 使用cmake编译opencv
1.2 再使用visual studio 2019生成opencv的lib,dll
1.3 配置opencv的系统环境变量
1.4 新建qt项目
1. MSVC版本
1.1 使用cmake编译opencv
1.2 再使用visual studio 2019生成opencv的lib,dll
1.3 配置opencv的系统环境变量
D:\code\envs\opencv450-cpu\x64\vc16\bin
D:\Qt\5.15.2\msvc2019_64\bin
D:\Qt\Tools\QtCreator\bin
1.4 新建qt项目
main文件内容。
#include <QCoreApplication>
#include "opencv2/opencv.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
using namespace cv;
Mat image=imread("D:/1.png");//改成自己图片的路径,注意使用的是'/'
imshow("Output",image);
return a.exec();
}
pro文件添加opencv
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#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
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
INCLUDEPATH += D:\code\envs\opencv450-cpu\include \
D:\code\envs\opencv450-cpu\include\opencv2
# MSVC
Debug: {
LIBS += -lD:\code\envs\opencv450-cpu\x64\vc16\lib\opencv_world450d
}
Release: {
LIBS += -lD:\code\envs\opencv450-cpu\x64\vc16\lib\opencv_world450
}
# # MinGW
# Debug: {
# LIBS += -D:\code\envs\opencv450-cpu\x64\vc16\lib\opencv_world450d.lib
# }
# Release: {
# LIBS += -D:\code\envs\opencv450-cpu\x64\vc16\lib\opencv_world450.lib
# }
debug小知识:
使用MSVC编译器编译github下载的代码时字符串含有中文,编译报错,而且是上千个错误时,一般就是环境问题、或者就是格式问题。
格式问题解决办法,在pro文件中添加:
win32-msvc*: {
QMAKE_CFLAGS *= /utf-8
QMAKE_CXXFLAGS *= /utf-8
}