1.安装visual studio profession 2015 或者visual studio profession community 2015
安装visual c ++工具箱,如下图所示,确保选择所有这些工具箱
2.安装调试器(Debugging Tools for Windows)
我的是win7 64位旗舰版+sp1,所以从这里下载了调试器(dbg_x86.msi,dbg_amd64.msi)
3.安装Qt Creator
从这里下载qt-opensource-windows-x86-5.9.5.exe
选择要安装的Qt版本,每个版本的Qt(Qt5.x)都有很多要下载的二进制文件,只选择你需要的那个。我们更喜欢在这里安装Qt5.9.5。为什么Qt5.9.5?因为Qt5.9是Qt的长期支持版本,理论上长期支持应该更稳定。可以把msvc2015 32-bit也选上。
4.安装OpenCV 3.4.2
到source forge下载OpenCV 3.4.2
5.用Qt Creator创建一个新项目
a.创建项目
b.你将看到很多选项,为简单起见,让我们选择“Application-> Non-Qt project-> Plain c ++ application”。告诉QtCreator,我们想要在不使用任何Qt组件的情况下创建一个c ++程序。
c.输入文件夹的路径和项目名称
d.单击Next按钮并立即使用qmake作为您的构建系统(您也可以选择cmake,但我在使用Qt时总是更喜欢qmake)。
e.你会看到一个页面要求你选择你的套件,套件是QtCreator用来分组不同设置的工具,如设备,编译器,Qt版本等。
f.单击下一步,QtCreator可能会要求您添加版本控制,为简单起见,请选择无。点击完成。
g.如果你看到这样的屏幕,那就意味着你成功了。
h.编写代码以通过opencv读取图像
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
//propose of namespace are
//1. Decrease the chance of name collison
//2. Help you organizes your codes into logical groups
//Without declaring using namespace std, everytime when you are using
//the classes, functions in the namespace, you have to call with the
//prefix "std::".
using namespace cv;
using namespace std;
/**
* main function is the global, designated start function of c++.
* @param argc Number of the parameters of command line
* @param argv Content of the parameters of command line.
* @return any integer within the range of int, meaning of the return value is
* defined by the users
*/
int main(int argc, char *argv[])
{
if(argc != 2){
cout<<"Run this example by invoking it like this: "<<endl;
cout<<"./step_02.exe lena.jpg"<<endl;
cout<<endl;
return -1;
}
//If you execute by Ctrl+R, argv[0] == "step_02.exe", argv[1] == lena.jpg
cout<<argv[0]<<","<<argv[1]<<endl;
//Open the image
auto const img = imread(argv[1]);
if(!img.empty()){
imshow("img", img); //Show the image on screen
waitKey(); //Do not exist the program until users press a key
}else{
cout<<"cannot open image:"<<argv[1]<<endl;
return -1;
}
return 0; //usually we return 0 if everything are normal
}
i.在.pro文件中增加如下内容:
INCLUDEPATH += your_install_path_of_opencv/opencv/opencv_3_4_2/opencv/build/include
LIBS += your_install_path_of_opencv/opencv/opencv_3_4_2/opencv/build/x64/vc14/lib/opencv_world342.lib
j.修改项目构建方式从debug改为release
单击红色区域包围的图标,然后将其从debug更改为release。我们为什么这样做?
在许多情况下,release模式比debug模式快得多。
我们链接到的库是作为发布库构建的,除非您想找麻烦,否则不要在项目中混合调试和发布库。
6.执行应用程序
将 opencv_world342.dll和opencv_ffmpeg342_64.dll(它们放在/opencv/build/x64/vc14/bin中)复制到shadow build(也就是生成的exe)的文件夹里。
注:由于我的系统是win7 64位旗舰版,虽然已经安装了sp1,但是执行exe时仍然提示缺少api-ms-win-downlevel-shlwapi-l1-1-0.dll:
这个问题已经解决了,参考https://github.com/opencv/opencv/issues/12010 只需要下载安装这个补丁就可以了,安装好后需要重启系统。
7.在QtCreator中添加命令行参数,没有它,当您单击Ctrl + R执行程序时,应用程序不知道图像在哪里。
8.如果成功,您应该看到应用程序打开命令行参数列表中指定的图像。
亲测通过,故记录下来。
参考:
http://qtandopencv.blogspot.com/2018/04/qt-and-computer-vision-0-setup.html
http://qtandopencv.blogspot.com/2018/08/qt-and-computer-vision-2-build-simple.html