OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。
Qt是一个1991年由Qt Company开发的跨平台C++图形用户界面应用程序开发框架。
Visual Studio 2017是微软公司开发的的IDE,功能强大,使用简单,而且还有免费的社区版。虽然写这篇文章的时候,VS 2019版本已经发布了,但是,并不推荐大家使用最新版本。
OpenCV的强项是进行图像(流)处理,Qt的强项是进行界面开发,本文将Visual Studio 2017, Qt 5.12.3, OpenCV 4.1.0集成配置。
首先,将需要的软件下载好。
OpenCV下载


进入OpenCV github网站,下载opencv_contrib扩展模块。


Qt5.12.3下载
进入Qt官网,下载Qt开源版。




Visual Studio 2017下载


编译OpenCV时,要用到cmake,所以还需下载cmake4.10。

软件下载好之后,默认安装就行了,其中VS 2017安装配置如下:

各个软件安装好之后,开始使用cmake编译opencv。
打开cmake,source code选中opencv/sources目录,build libraries选中一个新的目录,方便管理 。我这里是新建一个contrib_build目录。

在下图中,第二项选择机器型号,一般选win32(32位)或x64(64位)

configure完成后,出现红色,再次点击,知道界面不再出现红色位置。接下来编译扩展模块。

在编译的过程中,需要下载ippicv_win,opencv_ffmpeg.dll,opencv_ffmpeg_64.dll,face_landmark_model.dat等文件,由于这些文件源在国外,所以下载很慢。这里我已经下载好所以的安装软件和编译文件,需要的伙伴记得私聊我哦!
configure完成且不出现红色界面后,点击Generate,至此,OpenCV编译完成,接下来在VS 2017中生成OpenCV库文件。


操作完成后,在contrib_build目录下生成一个install目录,这个目录就是我们需要的,把这个目录剪切出来,重命名为installd,表示Debug模式下生成的。
要生成Release模式的文件,只需把Debug变成Release,如下图所示,其他操作和Debug一样。

这样我们得到了Debug和Release两个模式下的库文件(installd, installr),接下来配置opencv环境变量。

系统环境变量
C:\Users\luo\opencv4.1\installd\x64\vc15\bin
C:\Users\luo\opencv4.1\installr\x64\vc15\bin

VS 2017集成Qt 5.12.3
打开VS 2017,按下安装Qt插件





配置完Qt后,新建一个Qt工程:



VS 2017工程配置OpenCV
先把ui设计成如下格式,一个简单的demo:

把x64\vc15\bin目录下的dll文件全部复制到C:/Windows/System32和C:/Windows/SysWOW64两个目录下,文件筛选脚本如下:
from shutil import copyfile
import os
def copy(fs, d1, d2):
for f in fs:
if os.path.isfile(d1 + "/" + f):
copyfile(d1 + "/" + f, d2 + "/" + f)
print("copied")
def getfile(d):
os.chdir(d)
files = os.listdir()
fs = []
for f in files:
if f.endswith(".dll"):
fs.append(f)
return fs
def main():
d = [
u"C:/Users/luo/opencv4.1/installd/x64/vc15/bin",
u"C:/Users/luo/opencv4.1/installr/x64/vc15/bin",
u"C:/Users/luo/Documents/opencvdll/debug",
u"C:/Users/luo/Documents/opencvdll/release",
]
files = getfile(d[0])
copy(files, d[0], d[2])
files = getfile(d[1])
copy(files, d[1], d[3])
main()
点击项目,选择属性

C++目录->包含目录,编辑,添加
C:\Users\luo\opencv4.1\installd\include
C:\Users\luo\opencv4.1\installd\include\opencv2

C++目录->库目录,编辑,添加
C:\Users\luo\opencv4.1\installd\x64\vc15\lib

连接器->输入->附加依赖项,编辑,添加

opencv_aruco410d.lib
opencv_bgsegm410d.lib
opencv_bioinspired410d.lib
opencv_calib3d410d.lib
opencv_ccalib410d.lib
opencv_core410d.lib
opencv_datasets410d.lib
opencv_dnn410d.lib
opencv_dnn_objdetect410d.lib
opencv_dpm410d.lib
opencv_face410d.lib
opencv_features2d410d.lib
opencv_flann410d.lib
opencv_fuzzy410d.lib
opencv_gapi410d.lib
opencv_hdf410d.lib
opencv_hfs410d.lib
opencv_highgui410d.lib
opencv_imgcodecs410d.lib
opencv_imgproc410d.lib
opencv_img_hash410d.lib
opencv_line_descriptor410d.lib
opencv_ml410d.lib
opencv_objdetect410d.lib
opencv_optflow410d.lib
opencv_phase_unwrapping410d.lib
opencv_photo410d.lib
opencv_plot410d.lib
opencv_quality410d.lib
opencv_reg410d.lib
opencv_rgbd410d.lib
opencv_saliency410d.lib
opencv_shape410d.lib
opencv_stereo410d.lib
opencv_stitching410d.lib
opencv_structured_light410d.lib
opencv_superres410d.lib
opencv_surface_matching410d.lib
opencv_text410d.lib
opencv_tracking410d.lib
opencv_video410d.lib
opencv_videoio410d.lib
opencv_videostab410d.lib
opencv_xfeatures2d410d.lib
opencv_ximgproc410d.lib
opencv_xobjdetect410d.lib
opencv_xphoto410d.lib
配置完OpenCV,编写界面按钮槽函数
QtGuiApplication1::QtGuiApplication1(QWidget *parent) : QWidget(parent)
{
ui.setupUi(this);
connect(ui.openandshowBtn, SIGNAL(clicked()), this, SLOT(OpenAndShowImage()));
connect(ui.openandshowBtn_2, SIGNAL(clicked()), this, SLOT(CloseWindow()));
waitKey(0);
}
void QtGuiApplication1::OpenAndShowImage() {
QString str_path = QFileDialog::getOpenFileName(nullptr, QObject::tr("选择文件"), QObject::tr("C:/Users/luo/Pictures/"),
QObject::tr("图像文件(*.jpg *.png *.bmp *.tiff);;所有文件(*.*);;"));
if (!str_path.isEmpty()) {
String imgpath = String(str_path.toStdString());
ui.filenameLable->setText(str_path);
String winname = String("显示图像");
image = imread(imgpath);
if (image.data) {
namedWindow(winname);
imshow(winname, image);
}
}
}
void QtGuiApplication1::CloseWindow() {
destroyAllWindows();
this->close();
}
程序运行结果如下图所示:


点击关闭窗口,全部窗口关闭。
网友评论