下载OpenCV源码
OpenCV的release页面
选择相应的版本,下载源码
下载opencv_contrib源码
找到与opencv版本一致的tag
https://github.com/opencv/opencv_contrib/tags
从源码安装OpenCV
下载OpenCV源码
OpenCV的release页面
Releases · opencv/opencv
选择相应的版本,下载源码
下载opencv_contrib源码
找到与opencv版本一致的tag
https://github.com/opencv/opencv_contrib/tags
安装依赖
sudo apt-get install libgtk2.0-dev pkg-config
编译安装opencv
- 新建目录
mkdir ~/opencv_build && cd ~/opencv_build
把下载好的opencv和opencv_contrib放到opencv_build目录下。
添加代理到编译过程中的下载链接
- 对下列文件进行修改
opencv_contrib-3.4.14/modules/xfeatures2d/cmake/download_boostdesc.cmake
opencv_contrib-3.4.14/modules/xfeatures2d/cmake/download_vgg.cmake
opencv_contrib-3.4.14/modules/face/CMakeLists.txt
在这些文件中的链接前加上https://ghproxy.com/
。这是一种代理下载的方式,加快下载速度。
加好的效果类似下图:
- 编译
在opencv文件夹中新建build目录
cd opencv-3.4.14
mkdir build
cd build
在build目录下执行下面的cmake命令。需要配置OPENCV_EXTRA_MODULES_PATH
参数。这个参数是opencv_contrib/modules的路径。
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/kevin/opencv_build/opencv_contrib-3.4.14/modules \
-D BUILD_EXAMPLES=ON ..
make -j8
sudo make install
编好的库会安装到/usr/local/
下。
Troubleshooting
- 执行程序时突然报错
[planner_server-6] [WARN 2022-3-13 20:43:46.149226360] [planner_server]: CleanPath plugin failed to plan.
"OpenCV(3.4.14) /home/ubuntu/opencv_build/opencv-3.4.14/modules/highgui/src/window.cpp:658: error: (-2:Unspecified
error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on
Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
处理措施:
按照提示安装libgtk2.0-dev and pkg-config
sudo apt-get install libgtk2.0-dev pkg-config
然后按照上面的步骤重新安装一下opencv
参考:https://www.daimajiaoliu.com/daima/4858c8b6c900404
- 出现Failed to load module "canberra-gtk-module"错误
sudo apt-get install libcanberra-gtk-module
参考:
https://opencv.org/
https://www.jianshu.com/p/3c15a1ad3ec6
关注公众号《首飞》回复“机器人”获取精心推荐的C/C++,Python,Docker,Qt,ROS1/2等机器人行业常用技术资料。