- 安装依赖
一定要安装全,避免之后出现各种奇怪的问题
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install ffmpeg
- 下载opencv和opencv_contrib源码
推荐下载.tar.gz
格式并解压,之后把opencv_contirb放到opencv文件夹中
我使用的是4.2.0版本
Releases · opencv/opencv · GitHubReleases · opencv/opencv_contrib · GitHub
tar -zxvf opencv-4.2.0.tar.gz
tar -zxvf opencv_contirb-4.2.0.tar.gz
mv opencv_contirb-4.2.0/ opencv-4.2.0/
- 编译
sudo mkdir build && cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/Developer/opencv-4.2.0/opencv_contrib-4.2.0/modules/ ..
sudo make -j${nproc}
sudo make install
注意第二步要更换成自己的路径
第三步${nproc}
为使用所有CPU核进行编译
【报错:找不到boostdesc_bgm.i等一系列.i
文件】
解决方案:GitHub - omega-Lee/OpenCV_boostdesc_vgg_file: 在编译OpenCV的时候经常出现缺少.i文件,所以一次性把他们做个集合 把这个repo中文件放到opencv_contrib/modules/xfeatures2d/src/
中
【报错:找不到xxx.impl.hpp
文件】
解决方案:直接去Opencv官方repo中找到这些文件放到对应位置即可 GitHub - opencv/opencv: Open Source Computer Vision Library
【报错:找不到 feature2d/test/test_detectors_regression.impl.hpp】
解决方案:将这些文件复制到编译文件夹下:cp opencv/modules/features2d build
- 配置路径
-
vim/etc/ld.so.conf.d/opencv.conf
,在行尾添加/usr/local/lib
-
ldconfig
让其生效 -
vim /etc/bash.bashrc
,在行尾添加export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
-
source /etc/bash.bashrc
更新bash
- 检验是否安装成功:
pkg-config opencv --modversion
【报错:Package opencv was not found in the pkg-config search path.】
解决方案:
cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo vim opencv.pc
在opencv.pc
中添加以下信息
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version:4.2.0
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
~
将文件导出到环境变量
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
Resources
在Linux下用正确的姿势安装OpenCV - 知乎