作业2中用到了OpenCV
注意:选择版本,我这里是x64 版本。
为什么要选x64版本原因
“因为代码中有使用std::optional这个特性,这个特性是c++17中才有的,这个坑反应了好久才找到原因。
改完之后发现使用默认的x86设置,std::optional依然会报错,所以选了x64之后,环境就彻底配好了。所以第一步对应的库也要安装64位的。”
上述在Config过程中,会报很多错误,主要是一些下载的原因:
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)
修改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
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)
https://raw.githubusercontent.com/opencv/opencv_3rdparty/xxxxxx/ippicv/ippicv_2020_win_intel64_20191018_general.zip
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)
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)
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目录下,如下所示:
在配置opencv_contrib后,点击configure,会继续报错,还是因为没有下载成功的原因,
主要是wechat_qrcode 和 xfeatures2d 相关的文件。根据CMakeDownloadLog.txt里面的提示链接,手动下载缺失的文件,最后存放到build目录下的downloads目录下对应的文件夹:最后文件夹里面的文件如下所示。
这样,最后的库文件是一个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的文件中,使以后配置工程时输入此单一文件即可。
如果成功,显示Generate done.
Install to: C:/Resource/OpenCV/OpenCV_build/install
-----------------------------------------------------------------
Configuring done
Generating done
生成OpenCV.sln工程
菜单栏->生成->选择生成的项目配置。
注意:这里如果没有安装Python的Debug版本,在编译的时候,会报错。
LINK : fatal error LNK1104: 无法打开文件“python310_d.lib”
报错的原因参考无法打开pythonxxx_d.lib
所以应该是安装Python的时候,没有选择Debug版本。后来我重新卸载了已安装的Python,重新安装时选择Python Debug版本。
重新编译后成功
系统属性->环境变量->Path->添加opencv 库路径
新建一个空的工程:
#include
#include
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();
}