首先配置好vscode,参考
下载Windows Source
解压后配置好环境变量 将bin添加到Path下。
验证:
参考这篇
第一次点connfigure之后,上面的配置项会标红,这时候自己勾选哪些不想装,比如不要java和python的,毕竟我只要最后的c++,而且编译python的有可能把anaconda下的破坏了。
最后记录的坑在于,ffmpeg要自己下,对于cmake的configure之后哪些需要下载,去编译目录下寻找CMakeDownloadLog.txt 以及download_with_curl或者download_with_wget看就行。看哪些下载失败,自己手动下载然后放.cache下,并安装md5改好名称。
有个坑是.cmake文件,用链接访问页面直接出来了,如果粘贴复制,实际上和原始的内容有差别,这样md5就会对应不上,所以自己右键另存为.txt,之后在改名字就行。
下面贴出我之前的CMakeDownloadLog.txt
#use_cache "C:/mysoft/opencv/sources/.cache"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_ADE_HASH_3rdparty_ade_v0_1_1f_zip"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_opencv_videoio_ffmpeg_dll"
#match_hash_in_cmake_cache "OCV_DOWNLOAD_FFMPEG_HASH_3rdparty_ffmpeg_opencv_videoio_ffmpeg_64_dll"
#do_copy "ffmpeg_version.cmake" "8862c87496e2e8c375965e1277dee1c7" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/40b4666d1aa374205fd61373496e15d92ecd5313/ffmpeg/ffmpeg_version.cmake" "C:/mysoft/opencv/build/x64/MinGW/3rdparty/ffmpeg"
#check_md5 "C:/mysoft/opencv/build/x64/MinGW/3rdparty/ffmpeg/ffmpeg_version.cmake"
#mismatch_md5 "C:/mysoft/opencv/build/x64/MinGW/3rdparty/ffmpeg/ffmpeg_version.cmake" "1c12632904ef677c1cc2f0887f3d154c"
#check_md5 "C:/mysoft/opencv/sources/.cache/ffmpeg/8862c87496e2e8c375965e1277dee1c7-ffmpeg_version.cmake"
#mismatch_md5 "C:/mysoft/opencv/sources/.cache/ffmpeg/8862c87496e2e8c375965e1277dee1c7-ffmpeg_version.cmake" "d41d8cd98f00b204e9800998ecf8427e"
#delete "C:/mysoft/opencv/sources/.cache/ffmpeg/8862c87496e2e8c375965e1277dee1c7-ffmpeg_version.cmake"
#cmake_download "C:/mysoft/opencv/sources/.cache/ffmpeg/8862c87496e2e8c375965e1277dee1c7-ffmpeg_version.cmake" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/40b4666d1aa374205fd61373496e15d92ecd5313/ffmpeg/ffmpeg_version.cmake"
#try 1
# timeout on name lookup is not supported
# getaddrinfo(3) failed for raw.githubusercontent.com:443
# Could not resolve host: raw.githubusercontent.com
# Closing connection 0
#
将vscode的两个task和c_cpp_properties配置好
贴一下我的:
task的
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I","C:/mysoft/opencv/build/x64/MinGW/install/include",
"-I","C:/mysoft/opencv/build/x64/MinGW/install/include/opencv2",
"-L","C:/mysoft/opencv/build/x64/MinGW/install/x64/mingw/lib",
"-l", "libopencv_calib3d454",
"-l", "libopencv_core454",
"-l", "libopencv_dnn454",
"-l", "libopencv_features2d454",
"-l", "libopencv_flann454",
"-l", "libopencv_gapi454",
"-l", "libopencv_highgui454",
"-l", "libopencv_imgcodecs454",
"-l", "libopencv_imgproc454",
"-l", "libopencv_ml454",
"-l", "libopencv_objdetect454",
"-l", "libopencv_photo454",
"-l", "libopencv_stitching454",
"-l", "libopencv_video454",
"-l", "libopencv_videoio454"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: D:\\mingw64\\bin\\g++.exe"
}
]
}
c_cpp_properties的:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/mysoft/opencv/build/x64/MinGW/install/include",
"C:/mysoft/opencv/build/x64/MinGW/install/include/opencv2"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "D:/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
其实就是配置好头文件、静态库,动态库。
最后你编译完,按照提示将vscode的两个task和c_cpp_properties配置好
开开心心的把代码一写,f5一按,点开.exe,发现报错:
无法定位程序输入点 .......... xxxxx.dll
啊哈??最后查询资料解决如下:
参考:https://blog.csdn.net/agassy/article/details/50492413
里面提到了几种可能:
1.在生成exe时所使用的lib文件的版本,和在运行exe时所调用的dll的版本不一致。
其实大概就是编译的时候,实际链接的动态库或者静态库和你想象的不一样。
2.可执行文件在运行时寻找和调用dll文件的路径搜索顺序
可能有其他同名动态库提前被搜寻而被链接。
我在排除2之后,觉得应该是1的问题,可是苦于鬼知道我编译的时候最后链接的哪儿?
最后查询资料,发现了这么一款(下载64位的就好,解压打开)可以分析形成的.exe的工具,间接的看到了exe实际链接的库,最后发现了问题所在:
具体报错的exe以及没了,我简单描述以下,就是发现实际链接好多跑anaconda下的相关库去了,顿时发觉了问题,妈蛋的,编译的时候又被anaconda截胡了。被截胡的库是mingw-w64,没想到吧,anaconda下面也有mingw,本身编译的时候应该用自己下载的那个的。
所以解决办法我采取的是把自己下载的mingw的路径放在anaconda的mingw之前:
然后重新编译opencv。
windos下opencv编译支持gstreamer
参考:
https://gstreamer.freedesktop.org/documentation/installing/on-windows.html?gi-language=c#
https://funvision.blogspot.com/2020/03/compile-opencv-4-for-visual-studio-2019.html
下载:
https://gstreamer.freedesktop.org/data/pkg/windows/1.19.1/mingw/
gstreamer-1.0-devel-mingw-x86_64-1.19.1.msi
我下的1.19.1,本来向下1.19.2的,奈何每次最后一定下不了,,
网上很多教程还顺便把runtime给装了,我这里没有,因为用不到,,
双击安装,选择custom安装,主要是看它把文件放哪儿了,我的默认给放f盘下面了
然后cmakegui下第一次configue配置后,搜索框搜索gstreamer,把路径配置好,打开withgstreamer。如下图:
环境变量配好的情况下,,很有可能识别到的是.a结尾的静态库,注意改正,不然编译会gstrreamer这块会报错。
然后正常按照之前的编译流程走
之所以想着编译支持gstreamer,是因为想尝试cv2下面通过gst进行用gpu硬解码。
当然了,这里我就会编译时打开编译python的接口了。
import cv2
pipeline = "rtspsrc location=\"rtsp://login:password@host:port/\" ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, format=(string)BGRx! videoconvert ! appsink"
capture = cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER)
while capture.isOpened():
res, frame = capture.read()
cv2.imshow("Video", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
capture.release()
cv2.destroyAllWindows()
另Ubuntu或者linux下应该基本同理,若有更新
需要下载opencv、opencv_contrib、cuda、cudnn(可选),以及
下面贴出配置,我没整那么多,主要就是WITH_CUDA 和相关库头文件。其他什么 和OPENCV_DNN_CUDA之类的就不管了,感觉也用不上,我就想硬解码而已,,,需要的参考自己添加上。。
cuda
OPENCV_EXTRA_MODULES_PATH,注意路径斜杠
gstreamer的同上面提到的图
最后:
configure,cuda还是不支持,看警告:
CUDA compilation is disabled (due to only Visual Studio compiler supported on your platform).
CMake Warning at cmake/OpenCVFindLibsPerf.cmake:45 (message):
OpenCV is not able to find/configure CUDA SDK (required by WITH_CUDA).
CUDA support will be disabled in OpenCV build.
To eliminate this warning remove WITH_CUDA=ON CMake configuration option.
醉了,mingw还搞不定,,得vs的编译器,,