来源:http://cnblogs.com/cxyxm/p/12382990.html
1.下载opencv的安装包并解压。下载网址https://sourceforge.net/projects/opencvlibrary/

图1
2.打开opencv文件,目录如下:

图2 open文件目录
3.右键单击“此电脑”,点击“属性”,高级系统配置 -> 环境变量,如图3所示:

图3
4.在“系统变量”中新建“OPENCV_HOME”,输入F:opencvuildx64vc14in,在“Path”中添加%OPENCV_HOME%,点击确定。

图4
5.打开VS2017,新建空项目,自定义项目名称和路径,点击确定。

图5.1

图5.2
6.点击: 视图 -> 其他窗口 -> 属性管理器

图6
7.如图,Project1 -> Debug|x64 -> 右键 -> 属性

图7
8. 在“VC++目录”中,点击“包含目录”,点击编辑,添加:
F:opencvuildinclude
F:opencvuildincludeopencv2

图8
9. 在“VC++目录”中,点击“库目录”,点击编辑,添加:
F:opencvuildx64vc14lib

图9
10. 点击“链接器”,“附加依赖项”,添加:opencv_world401d.lib,只添加后缀为d的debug文件即可。

图10
11. opencv_world401d.lib文件路径为F:opencvuildx64vc14lib

图11 opencv_world401d.lib文件路径
12.配置结束,重启VS2017,写入测试代码测试

图12 测试代码
测试代码如下:
1 #include <opencv2/opencv.hpp> 2 3 using namespace std; 4 using namespace cv; 5 6 int main(int argc, char* argv[]) 7 { 8 const char* imagename = "F:\test.bmp";//此处为你自己的图片路径 9 10 //从文件中读入图像 11 Mat img = imread(imagename, 1); 12 13 //如果读入图像失败 14 if (img.empty()) 15 { 16 fprintf(stderr, "Can not load image %s ", imagename); 17 return -1; 18 } 19 //显示图像 20 imshow("image", img); 21 22 //此函数等待按键,按键盘任意键就返回 23 waitKey(); 24 return 0; 25 }