Ubuntu20.04
安装opencv4.5.5+opencv_contrib4.5.5
sudo apt update
sudo apt install libopencv-dev python3-opencv
python3 -c "import cv2; print(cv2.__version__)"
4.2.0
sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
OpenCV
和OpenCV_contrib
源(速度慢可以用代理)opencv_build
,不然可能报错。mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
opencv
文件夹并新建build
文件夹并切入cd ~/opencv_build/opencv
mkdir -p build && cd build
CMake
命令配置 OpenCV
构建(PATH 设置为自己的目录)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=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vagrant/opencv_build/opencv/build
make -j16
根据你的处理器修改-f值
。如果你不知道你的处理器核心数,你可以输入nproc找到。
sudo make install
pkg-config --modversion opencv4
报错方式可能会有所不同,不妨把所有的报错解决方案都试一下,总归有一个是你所需要的。
在你感到绝望的时候,不要放弃,坚持再坚持,相信自己一定可以的。
Ubuntu18.04
安装opencv3.4.5+opencv_contrib3.4.5
注意opencv与opencv_contrib的版本应该一致
opencv下载:opencv官网
opencv_contrib下载:GitHub
这里需要下载opencv的source,opencv_contrib可以下载zip或者tar.gz,但本文下载的是zip
以上两个文件已整理到百度网盘
提取码:jdic
sudo apt-get update
sudo apt-get upgrade
//依赖包
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 qt5-default ccache libv4l-dev libavresample-dev libgphoto2-dev libopenblas-base libopenblas-dev doxygen openjdk-8-jdk pylint libvtk6-dev
sudo apt-get install pkg-config
其中可能会遇到无法安装libjasper-dev的问题,类似下面的页面
XXX@XXX:~/Files/opencv-3.1.0$sudo apt-get install libjasper-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libjasper-dev
解决方案
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
opencv
和opencv_contrib
先解压下载的opencv和opencv_contrib包,建议直接将下载下来的包直接放至home地址下,这样,zip格式的解压代码如下:
sudo unzip opencv-3.4.5.zip
sudo unzip opencv_contrib-3.4.5.zip
tar.gz格式解压代码:
sudo tar zxvf opencv_contrib-3.4.5.tar.gz
然后将解压后的contrib文件移动至opencv-3.4.5
文件下
sudo cp -r opencv_contrib-3.4.5 opencv-3.4.5
并在opencv-3.4.5
文件夹中新建build
文件夹用于后续编译和生成
cd opencv-3.4.5
sudo mkdir build
进入bulid文件夹并进行第一次cmake
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/opencv-3.4.5/opencv_contrib-3.4.5/modules/ -D OPENCV_ENABLE_NONFREE=True ..
先下载文件:
修改相应文件
将ippicv.cmake文件下的第47行替换为本地路径
cd /opencv-3.4.5/3rdparty/ippicv
sudo vim ippicv.cmake
"改为相应地址" 格式为“file:绝对路径“ #本地路径,与你自己下载存放的地址相关
#"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/" #原下载地址
#记得改为本地ippcv的地址
更改face_landmark_model配置文件,将CMakeLists.txt文件的第19行修改为本地路径
cd /opencv-3.4.5/opencv_contrib-3.4.5/modules/face
sudo vim CMakeLists.txt
"本地地址" 格式为“file:绝对路径“ #本地路径,与你自己下载存放的地址相关
#"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/" #原下载地址
# 记得改为本地face_landmark_model.dat的地址
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/opencv-3.4.5/opencv_contrib-3.4.5/modules/ -D OPENCV_ENABLE_NONFREE=True ..
cmake结束后会生成如下的类似界面
-- Install path: /usr/local
--
-- cvconfig.h is in: /home/files/opencv-3.4.5/build
-- -----------------------------------------------------------------
--
-- Configuringdone
-- Generating done
-- Build files have been written to: /home/files/opencv-3.4.5/build
sudo make -j8
opencv安装opencv_contrib出现无法打开包括文件: “opencv2/xfeatures2d/cuda.hpp”
解决方法:
解决办法: 在CMakeLists.txt增加以下内容: INCLUDE_DIRECTORIES("/works/opencv-3.4.0/opencv_contrib-3.4.0/modules/xfeatures2d/include")
CMakeLists.txt文件所在路径为: <你的路径>/opencv-3.4.0/modules/stitching/ CMakeLists.txt
继续 make
只要成功到达100即为成功
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf
/usr/local/lib
sudo ldconfig
sudo gedit /etc/bash.bashrc
export PKG_CONFIG_PATH=/usr/local/opencv/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/opencv/lib
c++ 开发案例:链接