windows opencv compile document
本文主要是教你如何从源码编译软件包,建议你通过vcpkg安装完整版本的OpenCV4(含gpu功能)来安装使用。
1 依赖项目编译安装
在开始之前必须先安装vcpkg
1.1 准备工具
1.2 安装CUDA工具包
下载安装包,下载Latest Release版本,可能需要注册后才能下载。
1.3 安装Intel? Integrated Performance Primitives(IPP)
英特尔集成性能基元可用于提高OpenCV库的颜色转换、Haar训练和DFT函数的性能。请注意这不是一项免费服务。
在名称[Name (Click to initiate download)]后带号的表示在线安装包,不带的表示离线安装包,请下载离线安装包。
下载安装包
下载后的文件直接解压,然后再安装.
1.4 安装Intel? Fortran Compiler Classic and Intel? Fortran Compiler(Fortran)
英特尔Fortran编译器。
重点:该Fortran编译器可以集成到Visual Studio
在名称[Name (Click to initiate download)]后带号的表示在线安装包,不带的表示离线安装包,请下载离线安装包。
下载安装包
下载后的文件直接解压,然后再安装.
1.5 编译Intel Threading Building Blocks (TBB)
英特尔线程构建模块 (TBB) 在 OpenCV 内部用于并行代码片段。使用此功能将确保 OpenCV 库将利用系统 CPU 中的所有内核。 下载源码
cd D:\build\opencv\DEPENDENTS
7z x oneTBB-master.zip
cd oneTBB-master
或者
git clone https://github.com/oneapi-src/oneTBB
cd oneTBB
vcpkg search pthread
vcpkg install pthread --triplet=x64-windows
rd /S /Q build
cmake -B build -S . ^
-DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/scripts/buildsystems/vcpkg.cmake -Wno-dev ^
-DTBB_TEST=OFF ^
-DCMAKE_INSTALL_PREFIX=D:\build\opencv\dep\tbb
cmake --build build --config Release
cmake --install build
cd ..
rd /S /Q oneTBB-master
1.6 安装boost
Boost是C++的后备库,它包含非常多的库,比较稳定的库会选择一些纳入C++标准,但是它非常庞大,全部安装需要的时间较长且比较占用磁盘空间,因此我们使用vcpkg按需要安装,以后抽空单独写一篇boost编译的文章。 只要你用C++开发,那么boost
vcpkg search Boost
vcpkg install boost-core boost-filesystem --triplet=x64-windows
1.7 安装Eigen
Eigen是线性代数的C++模板库。Eigen的源码都是写在头文件中的,因此可以不编译. 这里还是编译一下,学习一下编译知识
下载源码
cd D:\build\opencv\DEPENDENTS
7z x eigen-3.4.0.zip
cd eigen-3.4.0
rd /S /Q build
cmake -B build -S . ^
-DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/scripts/buildsystems/vcpkg.cmake -Wno-dev ^
-DCMAKE_INSTALL_PREFIX=D:\build\opencv\dep\eigen
cmake-gui -B build -S . ^
-DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/scripts/buildsystems/vcpkg.cmake -Wno-dev ^
-DCMAKE_INSTALL_PREFIX=D:\build\opencv\dep\eigen
cmake -B build -S . ^
-DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/scripts/buildsystems/vcpkg.cmake -Wno-dev ^
-DCMAKE_INSTALL_PREFIX=D:\build\opencv\dep\eigen ^
-DEIGEN_COMPILER_SUPPORT_CPP11:BOOL="1" ^
-DEIGEN_TEST_CXX11:BOOL="1" ^
-DEIGEN_TEST_CUDA:BOOL="1" ^
-DEIGEN_TEST_CUDA_CLANG="1"
cmake --build build --config Release
cmake --install build
cd ..
rd /S /Q eigen-3.4.0
1.8 编译Imath
cd D:\build\opencv\DEPENDENTS
git clone https://github.com/AcademySoftwareFoundation/Imath.git
cd Imath
rd /S /Q build
cmake -B build -S . ^
-DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/scripts/buildsystems/vcpkg.cmake -Wno-dev ^
-DCMAKE_INSTALL_PREFIX=D:\build\opencv\dep\imath
cmake --build build --config Release
cmake --install build
cd ..
rd /S /Q Imath
1.9 编译OpenEXR
下载源码
cd D:\build\opencv\DEPENDENTS
7z x openexr-3.1.5.zip
cd openexr-3.1.5
rd /S /Q build
cmake -B build -S . ^
-DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/scripts/buildsystems/vcpkg.cmake -Wno-dev ^
-DImath_DIR=D:\build\opencv\dep\imath\lib\cmake\Imath ^
-DCMAKE_INSTALL_PREFIX=D:\build\opencv\dep\openexr
cmake --build build --config Release
cmake --install build
cd ..
rd /S /Q openexr-3.1.5
1.10 安装python 2.7
下载
截止发稿Python2的最新版本为2.7.18.下载Windows x86-64 MSI installer,然后运行安装包。
因为可能存在多个python版本的情况,因此不要将python2.7添加至环境变量PATH,后面使用Python完整路径运行Python 2.7。
在本例中安装位置为C:\Python27.
1.11 安装numpy
C:\Python27\python -m pip install -U pip
C:\Python27\Scripts\pip install numpy flake8 pywin32
在命令行中输入开始测试
C:\Python27\python
进入python2.7后输出现面的py代码
import numpy as np
print np.random.rand(4,4)
如果numpy正确安装后将输出一个4*4的矩阵。注意值是随机的,和本文档中不一致
>>> import numpy as np
>>> print np.random.rand(4,4)
[[0.87818158 0.1910759 0.12547178 0.89111631]
[0.20264498 0.5511512 0.08455059 0.88867172]
[0.75875045 0.96903478 0.90276493 0.13222076]
[0.99473011 0.02529853 0.29949838 0.52184444]]
>>> exit();
1.12 安装lapack
LAPACK是用Fortran编写的,它提供了用于求解联立线性方程组,线性方程组的最小二乘解,特征值问题和奇异值问题的例程。
- 在编译前需要安装Intel? Fortran编译器
下载源码
cd D:\build\opencv\DEPENDENTS
tar -xf lapack-3.10.1.tar.gz
cd lapack-3.10.1
rd /S /Q build
cmake -B build -S . ^
-DCMAKE_TOOLCHAIN_FILE=E:/vcpkg/scripts/buildsystems/vcpkg.cmake -Wno-dev ^
-DCMAKE_INSTALL_PREFIX=D:\build\opencv\dep\lapack
cmake --build build --config Release
cmake --install build
cd ..
rd /S /Q lapack-3.10.1
2 编译OpenCV
2.1 安装依赖包
这里为了简化操作,OpenCV的依赖包全部采用vcpkg安装,编译OpenCV不使用依赖包源码。
vcpkg search VTK
vcpkg search OpenBLAS
vcpkg search LAPACK
vcpkg search openjpeg
vcpkg search OpenEXR
vcpkg search BLAS
vcpkg search gstreamer
vcpkg search harfbuzz
vcpkg search gflags
vcpkg search leptonica
vcpkg install tbb eigen3 Imath openexr blas VTK openjpeg openblas gstreamer pthread harfbuzz gflags zlib protobuf leptonica tesseract --triplet=x64-windows
2.2 编译OpenCV
2.2.1 可能会出现的异常
2.2.1.1 vcpkg异常
Error: while loading libhpdf:
The port directory (E:\vcpkg\ports\libhpdf) does not exist
Error: failed to load port from E:\vcpkg\ports\libhpdf
note: updating vcpkg by rerunning bootstrap-vcpkg may resolve this failure.
解决方案
重新运行下列命令
bootstrap-vcpkg
2.2.1.2 OpenCV requires enabled ‘cudev’ module from ‘opencv_contrib’
如果构建时不指定-DOPENCV_EXTRA_MODULES_PATH=./modules参数,则构建时会报异常如下:
CMake Error at modules/core/CMakeLists.txt:40 (message):
CUDA: OpenCV requires enabled 'cudev' module from 'opencv_contrib'
repository: https://github.com/opencv/opencv_contrib
解决方案
添加OPENCV_EXTRA_MODULES_PATH
cd D:\build\opencv\DEPENDENTS\opencv-4.5.5
git clone https://github.com/opencv/opencv_contrib
cmake -B build -DOPENCV_EXTRA_MODULES_PATH=./opencv_contrib -S .
cmake -B build -DOPENCV_EXTRA_MODULES_PATH=./opencv_contrib -DBUILD_opencv_legacy=OFF -S .
2.2.1.3 Cannot determine shared/static library type
异常信息如下,报错的信息为“无法确定共享/静态库类型”
CMake Warning at E:/vcpkg/installed/x64-windows/share/vtk/vtkDetectLibraryType.cmake:54 (message):
Failed to run `dumpbin` on E:/vcpkg/installed/x64-windows/lib/jsoncpp.lib.
Cannot determine shared/static library type:
Call Stack (most recent call first):
E:/vcpkg/installed/x64-windows/share/vtk/FindJsonCpp.cmake:35 (vtk_detect_library_type)
E:/vcpkg/scripts/buildsystems/vcpkg.cmake:824 (_find_package)
E:/vcpkg/installed/x64-windows/share/vtk/VTK-vtk-module-find-packages.cmake:209 (find_package)
E:/vcpkg/installed/x64-windows/share/vtk/vtk-config.cmake:136 (include)
E:/vcpkg/scripts/buildsystems/vcpkg.cmake:824 (_find_package)
cmake/OpenCVDetectVTK.cmake:2 (find_package)
CMakeLists.txt:815 (include)
解决方案
本案例使用VS 2019,默认安装位置。
从开始菜单->"Visual Studio 2019"->“x64 Native Tools Command Prompt for VS 2019”中运行
或者
将“C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64”设置至环境变量PATH
2.2.2 opencv_contrib
opencv_contrib旨在用于开发“额外”的模块和功能。 opencv_contrib模块通常没有稳定的API,而且它们没有经过很好的测试。因此,它不作为官方OpenCV发行版的一部分发布。 opencv所有新模块都单独开发,并首先在opencv_contrib中发布。当模块成熟并流行起来时,它被移动到中央OpenCV存储库,opencv_contrib开发团队为该模块提供生产质量支持。
2.2.3 构建OpenCV
- 本章节要从“x64 Native Tools Command Prompt for VS 2019”中运行
- cmake中的路径千万不要使用"\“,应该使用”/"
cd D:\build\opencv\DEPENDENTS
7z x opencv-4.5.5.zip
cd opencv-4.5.5
git clone https://github.com/opencv/opencv_contrib
rd /S /Q "opencv_contrib\modules\text"
rd /S /Q build
cmake -B build -S . ^
-DCMAKE_BUILD_TYPE=Release ^
-DOPENCV_EXTRA_MODULES_PATH="./opencv_contrib/modules" ^
-DCMAKE_INSTALL_PREFIX="E:/library/opencvgpu" ^
-DBUILD_EXAMPLES:BOOL="1" ^
-DWITH_CUDA:BOOL="1" ^
-DPYTHON_INCLUDE_DIR="C:/Python27/include" ^
-DPYTHON_LIBRARY="C:/Python27/libs/python27.lib" ^
-DPYTHON_EXECUTABLE="C:/Python27/python.exe" ^
-DLAPACK_LIBRARIES="D:/build/opencv/dep/lapack/lib" ^
-DWITH_TBB:BOOL="1" ^
-DTBB_DIR="D:/build/opencv/dep/tbb/lib/cmake/TBB" ^
-DWITH_EIGEN:BOOL="1" ^
-DEigen3_DIR="D:/build/opencv/dep/eigen" ^
-DWITH_OPENEXR:BOOL="1" ^
-DOpenEXR_DIR="D:/build/opencv/dep/openexr"
cmake --build build --config Release
cmake --install build
cd ..
rd /S /Q opencv-4.5.5
|