![1ab0595d2f99cbfd6d61578a9c366f5e.png](https://i-blog.csdnimg.cn/blog_migrate/35fa83b5e0cc5057f5337000ced3a962.jpeg)
SCDN博客: https:// blog.csdn.net/weixin_41 923658
微信公众号:「汤姆鱼」
-------------------------------------手动分割线-------------------------------------------
一、前期准备工作:
(1)、下载Opencv4.0.1;
https://opencv.org/releases.html
(2)、下载opencv4.0.1扩展模块(Opencv_contrib-4.0.1);
opencv/opencv_contrib
(3)、下载Cmake3.13;
https://cmake.org/files/LatestRelease/
(4)、将Opencv4.0.1解压到指定盘(F:Opencv4.0.1);
(5)、将opencv4.0.1扩展模块解压到指定盘(F:Opencv_contrib-4.0.1);
(6)、将Cmake3.13解压到指定盘(F:Cmake3.13);
ps:我用的VS是2015版的;
二、开始Cmake编译;
(1)、打开Cmake软件,开始配置;
![159327c47c9d17da2542ecc0110dc5c9.png](https://i-blog.csdnimg.cn/blog_migrate/5daee2bcb64b2110bcb765e26253a654.png)
(2)、添加Opencv源码的存放位置--》选择Cmake编译后文件保存的位置--》配置;
![ed960712fcfcb44273b091d91ba5cbe5.png](https://i-blog.csdnimg.cn/blog_migrate/d0e59e84b6bfed30a0d02711524c61b5.jpeg)
(3)、选择安装的VS的版本--》点击finish--》等待配置完成;
![a1cfdddd7a8a7efc8254e6db4036ffea.png](https://i-blog.csdnimg.cn/blog_migrate/9f15f22a0d644cb3fb5ef5280dc6fcdc.png)
![043272907e41517addd623152c3fd1f7.png](https://i-blog.csdnimg.cn/blog_migrate/65dbf473c360032de385f900df6a4bb9.jpeg)
(4)、勾选掉不想编译的选项(比如,我勾选掉 python3 --》点击Generate,等待编译完成;
![a820e9bb92143dd0c029d44e4c4754cc.png](https://i-blog.csdnimg.cn/blog_migrate/191d7af7276c84a0f5451b30d4b70617.jpeg)
(5)、找到OPENCV_EXTER_MODULES_PATH,添加opencv扩展模块的路径,并勾选OPENCV_ENABLE_NONFREE选项(PS:Opencv4.0.x以后的版本一定要勾选)--》点击Configure;
![0af0a76e492593e73aad146ecb5924b7.png](https://i-blog.csdnimg.cn/blog_migrate/4fa5dc92209ee37aeeff9808f8d8f515.jpeg)
(6)、点击Generate,等待完成;
![56f2cbb063c9ee817df6e428ac0b9c58.png](https://i-blog.csdnimg.cn/blog_migrate/3b04746cffcf382de4f69ced896bd9c2.jpeg)
![60631306bf4f4a046279192d97594e86.png](https://i-blog.csdnimg.cn/blog_migrate/70d9485e0088a4fa056d56a0e6cf8fa0.jpeg)
(7)、找到编译生成的文件夹,双击OpenCV.sln;
![2cfc2b0d23bda77568746010ccd07950.png](https://i-blog.csdnimg.cn/blog_migrate/b454a8641427d69e5064013fe616a55a.jpeg)
(8)、找到CMakeTargets--》右击INSTALL--》点击生成,等待完成,然后关闭VS;
![76d3fd26c074ed0a97b2f16351ecbebf.png](https://i-blog.csdnimg.cn/blog_migrate/098ea6f78fb0893317e4e089c4ca05de.jpeg)
![5a276cc59d57220677fdf9438d7fc91e.png](https://i-blog.csdnimg.cn/blog_migrate/1bb3f1cca0bbdd9dcf242ff864842eff.jpeg)
(9)、找到install这个文件夹,这个是我们最终想要的(其他的都是多余文件,我把多余的文件删除了,并且我把install里面的文件全部复制出来重新命名,如下图),然后查看生成的lib(路径:Opencv_Compilex64vc14lib);
![f105a7dde95bd083da1d327f3083e647.png](https://i-blog.csdnimg.cn/blog_migrate/2b4292fc90a704358d2b1685929de230.png)
![cd0dfaa9e9300a37e4277b73370c32ad.png](https://i-blog.csdnimg.cn/blog_migrate/9534d2b4c4d719794e47c7faec042eab.jpeg)
三、配置Opencv运行环境;
(1)、打开VS,新建项目,步骤如下:
![bd709b89a1d7f61b616900d859568dab.png](https://i-blog.csdnimg.cn/blog_migrate/2735dc5317f3f050175f128e74b98e24.jpeg)
![358dd2e01d111753779a1803583da766.png](https://i-blog.csdnimg.cn/blog_migrate/b9adb371b5989807bdafd62185bd5d8f.jpeg)
![4f938fbc89e8d2f83b9d0e39c99e9710.png](https://i-blog.csdnimg.cn/blog_migrate/b602bc2d0285d2579e5ffdfa32e3afa7.png)
![995b562326373c7757d6b11e9bfc2440.png](https://i-blog.csdnimg.cn/blog_migrate/4bed81b8408a349e5c13cff479f16f81.png)
(2)、点击Debugx64-->添加新项目属性表;
![b62d3518fec982f7225792e93968102c.png](https://i-blog.csdnimg.cn/blog_migrate/a1ff35012be86e5c9f7ce7e6bc7aa45e.jpeg)
(3)、设置属性表的名称-->添加;
![75a98ad94ef6b7eb84a16b1a7417e2eb.png](https://i-blog.csdnimg.cn/blog_migrate/e4daca779527395db83b5f34d23f0871.jpeg)
(4)、右击Opencv_New_Debug-->属性;
![d15bc2c38840702c53989ef7b0569ada.png](https://i-blog.csdnimg.cn/blog_migrate/74a9edacb896d3eeebcd16fa32e6eaa4.jpeg)
(5)、VC++目录-->包含目录-->编辑;
![00ba839c382abdae08b8686cae9475b9.png](https://i-blog.csdnimg.cn/blog_migrate/49fdb44186d99c389c0d956d22e75fa1.jpeg)
添加完毕;
![4fc4d79b3976f1ff0d2eb17bb8b34457.png](https://i-blog.csdnimg.cn/blog_migrate/90a105fb782f3a9b8a5b2dd68e494968.png)
(7)、VC++目录-->库目录-->编辑;
![9a87271f535f86b3ac795e7e85611145.png](https://i-blog.csdnimg.cn/blog_migrate/9112a703f5c8112f4fbb4a751e40cecf.jpeg)
添加完毕;
![32d1a46448cdce166579578c8f2a1f8f.png](https://i-blog.csdnimg.cn/blog_migrate/d536510ad73966fd29111340248e4f78.png)
(8)、链接器-->输入-->附加依赖项-->编辑;
![537d62bb11b83d833c4953b7a5af771c.png](https://i-blog.csdnimg.cn/blog_migrate/ed54ee197ebbd938ba4acb640912b044.jpeg)
附加依赖项(Debug版本):
opencv_xphoto401d.lib
opencv_xobjdetect401d.lib
opencv_ximgproc401d.lib
opencv_xfeatures2d401d.lib
opencv_videostab401d.lib
opencv_videoio401d.lib
opencv_video401d.lib
opencv_tracking401d.lib
opencv_text401d.lib
opencv_surface_matching401d.lib
opencv_superres401d.lib
opencv_structured_light401d.lib
opencv_stitching401d.lib
opencv_stereo401d.lib
opencv_shape401d.lib
opencv_saliency401d.lib
opencv_rgbd401d.lib
opencv_reg401d.lib
opencv_plot401d.lib
opencv_photo401d.lib
opencv_phase_unwrapping401d.lib
opencv_optflow401d.lib
opencv_line_descriptor401d.lib
opencv_imgproc401d.lib
opencv_imgcodecs401d.lib
opencv_img_hash401d.lib
opencv_highgui401d.lib
opencv_hfs401d.lib
opencv_fuzzy401d.lib
opencv_flann401d.lib
opencv_features2d401d.lib
opencv_face401d.lib
opencv_dpm401d.lib
opencv_dnn401d.lib
opencv_dnn_objdetect401d.lib
opencv_datasets401d.lib
opencv_core401d.lib
opencv_ccalib401d.lib
opencv_calib3d401d.lib
opencv_bioinspired401d.lib
opencv_bgsegm401d.lib
opencv_aruco401d.lib
opencv_objdetect401d.lib
opencv_ml401d.lib
添加完毕;
![2b9e8aee939ba094fe70a6048b854a67.png](https://i-blog.csdnimg.cn/blog_migrate/1690af5c9f4cb5eeada1a941aa5eb79b.png)
四、配置系统环境变量;
(1)、找到计算机的系统属性-->高级-->环境变量;
![da131cdbd936cd383c28650a1aef1091.png](https://i-blog.csdnimg.cn/blog_migrate/8590ac424d610d3ab0190b60c2f059f4.png)
(2)、点击系统变量中的Path-->编辑;
![1c70b05e35b34c526cdbce16e1130d95.png](https://i-blog.csdnimg.cn/blog_migrate/448796c6f84cf6f2be18f2d76628a1b5.jpeg)
(3)、点击新建-->浏览添加路径;
![816d81febb4d0f4ad65f02bc7aed9ce0.png](https://i-blog.csdnimg.cn/blog_migrate/95ac64ebd2df30871a8c50eb786c5bfd.jpeg)
(4)、添加完成的路径(如图);
![2ec97b88359b4bbeb8cd920f4f661c84.png](https://i-blog.csdnimg.cn/blog_migrate/af14162cd0667cafed0f1bc87edad483.jpeg)
五、测试;
(1)、源文件-->添加-->新建项;
![1f5c4d1cfc53a4f72c9078f8e9f4bf98.png](https://i-blog.csdnimg.cn/blog_migrate/81652f4b89ebe27daf410ce2a69618f7.jpeg)
(2)、Visual C++-->C++文件-->项目名称;
![96484d3a77460c44e4e0617d53024941.png](https://i-blog.csdnimg.cn/blog_migrate/54511ffc984abc3525c0a0256eba0031.jpeg)
(3)、Debug-->X64-->添加测试代码;
![67021c9f58bc53b8c5225022ec4a9ce8.png](https://i-blog.csdnimg.cn/blog_migrate/26282e6857777677d3d12f24bdb1d6f2.jpeg)
(4)、测试结果;
![15d6c0881e7891845beb19b4612bf6f6.png](https://i-blog.csdnimg.cn/blog_migrate/7a94eabb2cb62143c41a1d45174f7b43.jpeg)
ps:Release版本的配置,请参考:
https://blog.csdn.net/weixin_41923658/article/details/86634111
附加依赖项(Release版本):
opencv_xphoto401.lib
opencv_xobjdetect401.lib
opencv_ximgproc401.lib
opencv_xfeatures2d401.lib
opencv_videostab401.lib
opencv_videoio401.lib
opencv_video401.lib
opencv_tracking401.lib
opencv_text401.lib
opencv_surface_matching401.lib
opencv_superres401.lib
opencv_structured_light401.lib
opencv_stitching401.lib
opencv_stereo401.lib
opencv_shape401.lib
opencv_saliency401.lib
opencv_rgbd401.lib
opencv_reg401.lib
opencv_plot401.lib
opencv_photo401.lib
opencv_phase_unwrapping401.lib
opencv_optflow401.lib
opencv_line_descriptor401.lib
opencv_imgproc401.lib
opencv_imgcodecs401.lib
opencv_img_hash401.lib
opencv_highgui401.lib
opencv_hfs401.lib
opencv_fuzzy401.lib
opencv_flann401.lib
opencv_features2d401.lib
opencv_face401.lib
opencv_dpm401.lib
opencv_dnn401.lib
opencv_dnn_objdetect401.lib
opencv_datasets401.lib
opencv_core401.lib
opencv_ccalib401.lib
opencv_calib3d401.lib
opencv_bioinspired401.lib
opencv_bgsegm401.lib
opencv_aruco401.lib
opencv_objdetect401.lib
opencv_ml401.lib