vs2017社区版:https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/
opencv4.1.0:https://opencv.org/releases/
opencv_contrib4.1.0:https://github.com/opencv/opencv_contrib/releases
cmake:https://cmake.org/download/
1.解压opencv,并将opencv_contrib解压在建立的build文件中。
2.添加环境变量:
D:\opencv4.1.0\build\bin
D:\opencv4.1.0\build\x64\vc15\bin
3.将opencv里面的相关文件复制到C盘
将bin目录中的opencv_world410.dll和opencv_world410d.dll文件复制到C:\Windows\SysWOW64.
将bin目录里面的opencv_ffmpeg410_64.dll复制到C:\Windows\System32.
1.安装选择需要的工作负载:使用C++的桌面开发、Visual Studio扩展开发、Python.
2.建立空项目,选择 视图-其他窗口-属性管理器,对Debug|X64下的Microsoft.Cpp.x64.user的属性编辑。
在VC++包含目录编辑添加:
D:\opencv4.1.0\build\include
D:\opencv4.1.0\build\include\opencv2
VC++包含目录编辑添加:
D:\opencv4.1.0\build\x64\vc15\lib
在链接器-输入-附加依赖项编辑添加:
opencv_world410d.lib
注:一些教程中也会添加opencv_world410.lib,但这样后面会出错,只添加带b的即可。
Release|X64中的配置同上。
测试代码:
#include
#include
#include
using namespace cv;
int main()
{
// 读入一张图片(poyanghu缩小图)
Mat img = imread("F:\\suoxiaotu\\poyanghu缩小图.tif");
// 创建一个名为 "图片"窗口
namedWindow("图片");
// 在窗口中显示图片
imshow("图片", img);
// 等待6000 ms后窗口自动关闭
waitKey(6000);
return 0;
}
1.安装Install option选择Add Cmake to the sysyem PATH for all users.
或
添加环境变量D:\cmake\bin
2.cmd 输入nvcc -V验证。
3.打开CMake Gui
分别找路径:
D:\opencv4.1.0\sources
D:\opencv4.1.0\build
4.configure,然后选择VS2017 , platform默认win32,如果是64位,一定要更改!!!不然会出错。
再勾选4个:
BUILD_EXAMPLES
BUILD_opencv_world
OPENCV_EXTRA_MODULES_PATH:D:\opencv4.1.0\opencv_contrib\modules
WITH CUDA (以及OpenGL)
再次configure,由于使用的是CUDA10,编译错误:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_cublas_LIBRARY (ADVANCED)
…
参考该博客解决
解决方案:
(1)在opencv安装位置找到FindCUDA.cmake文件:
修改:
find_cuda_helper_libs(nppi)
为
find_cuda_helper_libs(nppial)
find_cuda_helper_libs(nppicc)
find_cuda_helper_libs(nppicom)
find_cuda_helper_libs(nppidei)
find_cuda_helper_libs(nppif)
find_cuda_helper_libs(nppig)
find_cuda_helper_libs(nppim)
find_cuda_helper_libs(nppist)
find_cuda_helper_libs(nppisu)
find_cuda_helper_libs(nppitc)
修改:
set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")
为
set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")
修改:
unset(CUDA_nppi_LIBRARY CACHE)
为
unset(CUDA_nppial_LIBRARY CACHE)
unset(CUDA_nppicc_LIBRARY CACHE)
unset(CUDA_nppicom_LIBRARY CACHE)
unset(CUDA_nppidei_LIBRARY CACHE)
unset(CUDA_nppif_LIBRARY CACHE)
unset(CUDA_nppig_LIBRARY CACHE)
unset(CUDA_nppim_LIBRARY CACHE)
unset(CUDA_nppist_LIBRARY CACHE)
unset(CUDA_nppisu_LIBRARY CACHE)
unset(CUDA_nppitc_LIBRARY CACHE)
(2)找到OpenCVDetectCUDA.cmake文件
修改:
...
set(__cuda_arch_ptx "")
if(CUDA_GENERATION STREQUAL "Fermi")
set(__cuda_arch_bin "2.0")
elseif(CUDA_GENERATION STREQUAL "Kepler")
set(__cuda_arch_bin "3.0 3.5 3.7")
...
为
...
set(__cuda_arch_ptx "")
if(CUDA_GENERATION STREQUAL "Kepler")
set(__cuda_arch_bin "3.0 3.5 3.7")
elseif(CUDA_GENERATION STREQUAL "Maxwell")
set(__cuda_arch_bin "5.0 5.2")
...
(3)将头文件cuda_fp16.h(C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\include)添加到opencv\modules\cudev\include\opencv2\cudev\common.hpp
即在common.hpp中添加
#include
configure done后生成,成功后open project
在VS2017中,点击 生成-批生成
勾选INSTALL和ALL_BUILD,生成。
tbc
6.1 CMake+opencv编译时Download: opencv_ffmpeg.dll、ippicv等文件失败。
参考:
VS2017配置opencv教程(超详细!!!)
Windows10+VS2017+cmake 编译opencv4.1 + opencv_contrib(含cuda)
opencv+CUDA9.1+vs2015环境搭建,编译opencv库,调用GPU加速运算
OpenCV3.3+CUDA9.0+Cmake3.9 环境搭建
windows10+vs2017+opencv3.4.1配置YOLOV4
使用Cmake-GUI在windows环境下编译yolov4
YOLOv4 的各种新实现、配置、测试、训练资源汇总