文章目录
- Opencv 库编译
- Step 1.下载源码
- Step 2. 使用CMake编译
- Step3. 解决CMake 过程中的报错
- 错误1: 错误的Python版本:
- 错误1 解决办法
- 错误2:下载ippicv_2020_win_ia32_20191018_general.zip失败
- 错误2 解决办法
- 错误3:ffmpeg相关文件下载失败
- 错误3 解决办法
- Step 4. 配置opencv_contrib
- 继续报错
- Step 5. 选择BUILD_opencv_world选项
- Step 6. 点击Generate
- Step 7. 编译OpenCV.sln工程
- 编译时遇到的报错
- 编译成功后生成的库和dll,以及include
- 添加环境变量
- 测试是否安装成功
Opencv 库编译
作业2中用到了OpenCV
Step 1.下载源码
- 下载OpenCV 源码 OpenCV 4.5.4
- 下载OpenCV_contrib OpenCV_contrib 4.5.4
注意:这两个源码的版本号要一致
Step 2. 使用CMake编译
- 将源码解压后,不要放在有中文路径的目录
- 使用管理员权限打开CMake, 一开始未使用,发现点击Configure时会报错,使用管理员权限打开CMake后,点击Configure未报错。
- 源码目录:opencv-4.5.4
- build目录:OpenCV_build(以下简称build目录)
注意:选择版本,我这里是x64 版本。
为什么要选x64版本原因
“因为代码中有使用std::optional这个特性,这个特性是c++17中才有的,这个坑反应了好久才找到原因。
改完之后发现使用默认的x86设置,std::optional依然会报错,所以选了x64之后,环境就彻底配好了。所以第一步对应的库也要安装64位的。”
Step3. 解决CMake 过程中的报错
上述在Config过程中,会报很多错误,主要是一些下载的原因:
错误1: 错误的Python版本:
CMake’s ‘find_host_package(PythonInterp 2.7)’ found wrong Python version
Found PythonInterp: C:/Program Files/Python310/python.exe (found suitable version "3.10.11", minimum required is "2.7")
CMake Warning at cmake/OpenCVDetectPython.cmake:81 (message):
CMake's 'find_host_package(PythonInterp 2.7)' found wrong Python version:
PYTHON_EXECUTABLE=C:/Program Files/Python310/python.exe
PYTHON_VERSION_STRING=3.10.11
Consider providing the 'PYTHON2_EXECUTABLE' variable via CMake command line
or environment variables
Call Stack (most recent call first):
cmake/OpenCVDetectPython.cmake:271 (find_python)
CMakeLists.txt:622 (include)
错误1 解决办法
修改opencv-4.5.4\cmake\OpenCVDetectPython.cmake最后部分内容
if(PYTHON_DEFAULT_EXECUTABLE)
set(PYTHON_DEFAULT_AVAILABLE "TRUE")
elseif(PYTHON3_EXECUTABLE AND PYTHON3INTERP_FOUND)
# Use Python 3 as default Python interpreter
set(PYTHON_DEFAULT_AVAILABLE "TRUE")
set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
elseif(PYTHON2_EXECUTABLE AND PYTHON2INTERP_FOUND)
# Use Python 2 as fallback Python interpreter (if there is no Python 3)
set(PYTHON_DEFAULT_AVAILABLE "TRUE")
set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
endif()
参考链接1.How to use Cmake to build opencv with python 3 instead of python 2
参考链接2.Install OpenCV GPU with CUDA for Windows10
错误2:下载ippicv_2020_win_ia32_20191018_general.zip失败
Download: ippicv_2020_win_intel64_20191018_general.zip
Download failed
IPPICV: Download: ippicv_2020_win_intel64_20191018_general.zip
Try 1 failed
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
IPPICV: Download failed: 35;"SSL connect error"
For details please refer to the download log file:
C:/Resource/OpenCV/OpenCV_build/CMakeDownloadLog.txt
Call Stack (most recent call first):
3rdparty/ippicv/ippicv.cmake:37 (ocv_download)
cmake/OpenCVFindIPP.cmake:259 (download_ippicv)
cmake/OpenCVFindLibsPerf.cmake:12 (include)
CMakeLists.txt:727 (include)
错误2 解决办法
- 下载ippicv_2020_win_intel64_20191018_general.zip压缩文件
打开build目录下的CMakeDownloadLog.txt文件,里面会提示下载的链接,
https://raw.githubusercontent.com/opencv/opencv_3rdparty/xxxxxx/ippicv/ippicv_2020_win_intel64_20191018_general.zip
- 拷贝压缩文件
直接在浏览器内下载下来,然后将文件保存到opencv-4.5.4/3dparty/ippicv文件夹中
- 修改ippicv.cmake 文件
set(THE_ROOT "${OpenCV_BINARY_DIR}/3rdparty/ippicv")
# ocv_download(FILENAME ${OPENCV_ICV_NAME}
# HASH ${OPENCV_ICV_HASH}
# URL
# "${OPENCV_IPPICV_URL}"
# "$ENV{OPENCV_IPPICV_URL}"
# "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
# DESTINATION_DIR "${THE_ROOT}"
# ID IPPICV
# STATUS res
# UNPACK RELATIVE_URL)
错误3:ffmpeg相关文件下载失败
opencv_videoio_ffmpeg.dll
opencv_videoio_ffmpeg_64.dll
ffmpeg_version.cmake
FFMPEG: Download: opencv_videoio_ffmpeg.dll
Try 1 failed
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
FFMPEG: Download failed: 35;"SSL connect error"
For details please refer to the download log file:
C:/Resource/OpenCV/OpenCV_build/CMakeDownloadLog.txt
Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:20 (ocv_download)
modules/videoio/cmake/detect_ffmpeg.cmake:14 (download_win_ffmpeg)
modules/videoio/cmake/init.cmake:7 (include)
modules/videoio/cmake/init.cmake:11 (add_backend)
cmake/OpenCVModule.cmake:298 (include)
cmake/OpenCVModule.cmake:361 (_add_modules_1)
cmake/OpenCVModule.cmake:385 (ocv_glob_modules)
CMakeLists.txt:917 (ocv_register_modules)
FFMPEG: Download: opencv_videoio_ffmpeg_64.dll
Try 1 failed
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
FFMPEG: Download failed: 35;"SSL connect error"
For details please refer to the download log file:
C:/Resource/OpenCV/OpenCV_build/CMakeDownloadLog.txt
Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:20 (ocv_download)
modules/videoio/cmake/detect_ffmpeg.cmake:14 (download_win_ffmpeg)
modules/videoio/cmake/init.cmake:7 (include)
modules/videoio/cmake/init.cmake:11 (add_backend)
cmake/OpenCVModule.cmake:298 (include)
cmake/OpenCVModule.cmake:361 (_add_modules_1)
cmake/OpenCVModule.cmake:385 (ocv_glob_modules)
CMakeLists.txt:917 (ocv_register_modules)
FFMPEG: Download: ffmpeg_version.cmake
Try 1 failed
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
FFMPEG: Download failed: 35;"SSL connect error"
For details please refer to the download log file:
C:/Resource/OpenCV/OpenCV_build/CMakeDownloadLog.txt
Call Stack (most recent call first):
3rdparty/ffmpeg/ffmpeg.cmake:20 (ocv_download)
modules/videoio/cmake/detect_ffmpeg.cmake:14 (download_win_ffmpeg)
modules/videoio/cmake/init.cmake:7 (include)
modules/videoio/cmake/init.cmake:11 (add_backend)
cmake/OpenCVModule.cmake:298 (include)
cmake/OpenCVModule.cmake:361 (_add_modules_1)
cmake/OpenCVModule.cmake:385 (ocv_glob_modules)
CMakeLists.txt:917 (ocv_register_modules)
错误3 解决办法
- 下载缺失的两个文件
参考CMakeDownloadLog.txt 里面的链接下载dll文件, ffmpeg_version.cmake的链接另存为文件,最后得到三个文件,如下所示:
- 复制文件到opencv-4.5.4/3dparty/ffmepg目录
- 修改ffmpeg.cmake文件
set(status TRUE)
# foreach(id ${ids})
# ocv_download(FILENAME ${name_${id}}
# HASH ${FFMPEG_FILE_HASH_${id}}
# URL
# "$ENV{OPENCV_FFMPEG_URL}"
# "${OPENCV_FFMPEG_URL}"
# "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${FFMPEG_BINARIES_COMMIT}/ffmpeg/"
# DESTINATION_DIR ${FFMPEG_DOWNLOAD_DIR}
# ID FFMPEG
# RELATIVE_URL
# STATUS res)
# if(NOT res)
# set(status FALSE)
# endif()
# endforeach()
但仍然不够,到这里再次执行Configure时,会继续报错:
CMake Error at modules/videoio/cmake/detect_ffmpeg.cmake:16 (include):
include could not find requested file:
C:/Resource/OpenCV/OpenCV_build/3rdparty/ffmpeg/ffmpeg_version.cmake
Call Stack (most recent call first):
modules/videoio/cmake/init.cmake:7 (include)
modules/videoio/cmake/init.cmake:11 (add_backend)
cmake/OpenCVModule.cmake:298 (include)
cmake/OpenCVModule.cmake:361 (_add_modules_1)
cmake/OpenCVModule.cmake:385 (ocv_glob_modules)
CMakeLists.txt:917 (ocv_register_modules)
参考链接Error at modules/videoio/cmake/detect_ffmpeg.cmake
网友的解决方案
I have encountered the same problem,mainly because cmake can't find ffmpeg_version.cmake file:
the src code file has a 3rdparty,
and your build dir has a 3rdparty,
cmake only search your build dir for ffmpeg_version.cmake,
so just copy the src 3rdparty's ffmpeg to the build dir's 3rdparty. bingo! problem fixed.
因此,按照上述描述,将opencv-4.5.4/3rdparty/ffmpeg整个文件夹拷贝到build目录下的3rdparty目录下,如下所示:
Step 4. 配置opencv_contrib
继续报错
在配置opencv_contrib后,点击configure,会继续报错,还是因为没有下载成功的原因,
主要是wechat_qrcode 和 xfeatures2d 相关的文件。根据CMakeDownloadLog.txt里面的提示链接,手动下载缺失的文件,最后存放到build目录下的downloads目录下对应的文件夹:最后文件夹里面的文件如下所示。
-
wechat_qrcode
-
xfeatures2d
至此,再次点击Congifure后,没有出现错误。
Step 5. 选择BUILD_opencv_world选项
这样,最后的库文件是一个dll文件。
OpenCV cmake配置项BUILD_opencv_world的说明
按照默认的CMakeLists.txt配置,BUILD_opencv_world默认是OFF状态,没有勾选。
编译生成的项目,会在build\lib\Debug目录下生成大量的lib和dll文件。
例如:有时网上设置项目环境时有人在链接器附加依赖项时需添加下列lib,当照着配置后会报错找不到…lib,在你的/x64/lib文件夹下压根没这些东西是因为你在编译的时候勾选了BUILD_opencv_world。
CMake中有一个选项名为BUILD_opencv_world,默认是不勾选的,它可以把所有的组件综合到一个名为opencv_world310d.lib或opencv_world310.lib的文件中,使以后配置工程时输入此单一文件即可。
如果编译成功,会生成下面两个lib
Step 6. 点击Generate
如果成功,显示Generate done.
Install to: C:/Resource/OpenCV/OpenCV_build/install
-----------------------------------------------------------------
Configuring done
Generating done
生成OpenCV.sln工程
Step 7. 编译OpenCV.sln工程
菜单栏->生成->选择生成的项目配置。
注意:这里如果没有安装Python的Debug版本,在编译的时候,会报错。
编译时遇到的报错
LINK : fatal error LNK1104: 无法打开文件“python310_d.lib”
报错的原因参考无法打开pythonxxx_d.lib
所以应该是安装Python的时候,没有选择Debug版本。后来我重新卸载了已安装的Python,重新安装时选择Python Debug版本。
重新编译后成功
编译成功后生成的库和dll,以及include
- install
里面是我们需要的lib、dll以及头文件
添加环境变量
系统属性->环境变量->Path->添加opencv 库路径
测试是否安装成功
新建一个空的工程:
- 设置工程属性
- Demo
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main() {
Mat img = imread("C:\\Resource\\OpenCV\\OpenCV_Project\\OpenCVTest\\x64\\Debug\\scenetext01.jpg");
imshow("img", img);
waitKey();
}
-
输出
-
弹出的INFO信息
关于这些信息 -
如何避免这些错误
参考这个网友提供的,我猜是因为Configure的时候没有勾选相关的内容勾选相关选项
但是我这里没有试验,因为编译库的话又要好久~~