1. 准备工作
(1)更新下环境
bash
sudo apt-get update
sudo apt-get upgrade
(2)安装编译器和构建工具等,若已安装,跳过即可
bash
sudo apt-get install -y g++ # Ubuntu自带gcc
sudo apt-get install -y cmake
sudo apt-get install -y make
sudo apt-get install -y wget
sudo apt-get install -y unzip
sudo apt-get install -y git
(3)安装各种依赖库
bash
sudo apt-get install build-essential pkg-config
sudo apt-get install libgtk2.0-dev libgtk-3-dev libglib2.0-dev libavcodec-dev libavformat-dev libswscale-dev libavutil-dev libv4l-dev liblapacke-dev libxvidcore-dev libx264-dev
sudo apt-get install python-dev python-numpy
sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper1 libjasper-dev libdc1394-22-dev libopenexr-dev libwebp-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install ffmpeg
安装依赖库libjasper1 和 libjasper-dev时,若报错E:Unable to locate package libjasper-dev ,可以命令行执行以下命令,然后再安装依赖库 libjasper1 libjasper-dev
bash
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main" # 国内可能需要换国内的镜像
sudo apt-get update
2. 下载OpenCV的源文件
可以在官网下载,也可以使用GitHub,这里选择git clone方式,下载的opencv文件夹在home目录下
bash
git clone https://github.com/opencv/opencv.git # 默认下载最新的版本
git -C opencv checkout 4.x
3. 生成构建脚本和编译
(1)cd切换到 opencv目录下,创建build文件夹,并切换到build目录下
bash
mkdir -p build && cd build
(2)在build目录下使用cmake来生成构建脚本,使用如下命令,
bash
cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D WITH_FFMPEG=ON ..
# .. 不要漏掉
bash
# 编译成一个o
cmake -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_opencv_python2=OFF -DBUILD_opencv_python3=OFF -DBUILD_SHARED_LIBS=OFF -DBUILD_opencv_java=OFF -DBUILD_opencv_world=ON -DBUILD_JAVA=OFF -DBUILD_ANDROID_EXAMPLES=OFF -DWITH_OPENCL=OFF -DWITH_IPP=OFF -DWITH_TBB=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_DOCS=OFF ..
(3)在build目录下编译opencv
bash
make -j8
编译时可能遇到的错误
bash
fatal error: openipeg.h: No such file or directory,
#include <openjpeg.h>
需要安装openjpeg包
sudo apt install libopenjp2-7-dev
4. 安装OpenCV
编译成功后,在build目录下执行安装命令,等待安装完成
bash
sudo make install
5. 配置环境
(1)配置环境变量:PKG_CONFIG_PATH
前面进行cmake时,已经生成了opencv4.pc配置文件,该文件的安装路径为:/usr/local/lib/pkgconfig/opencv4.pc
在build目录下使用命令查看一下(该命令报错,忽略)
bash
sudo find / -iname opencv4.pc
需要将opencv4.pc的路径添加到环境变量PKG_CONFIG_PATH中,创建一个名为pkgconfig.sh的脚本文件,放置位置在/etc/profile.d/pkgconfig.sh
bash
sudo gedit /etc/profile.d/pkgconfig.sh
在pkgconfig.sh文件中添加下面一行语句,保存后关闭。
bash
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
然后刷新环境变量,使之生效。
source /etc/profile
配置环境变量PKG_CONFIG_PATH 完成后,就可以使用pkg-config命令来查看和管理opencv的配置文件(也就是opencv4.pc)
bash
pkg-config --cflags opencv4
# -I/usr/local/include/opencv4
pkg-config --libs opencv4
#-L/usr/local/lib -lopencv_gapi -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
上述报错:
bash
cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo touch opencv.pc
然后在opencv.pc中添加以下信息,注意这些信息需要与自己安装opencv时的库路径对应:
bash
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version:4.0.1
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
原文链接:https://blog.csdn.net/PecoHe/article/details/97476135
(2)配置OpenCV的动态库环境
我们需要将OpenCV的libs的安装目录添加到动态库加载配置文件中,这样在编译时,才能找到OpenCV的各种库文件,下面创建opencv4.conf
bash
sudo gedit /etc/ld.so.conf.d/opencv4.conf
在文件中添加下面一行
/usr/local/lib
刷新一下动态库环境
sudo ldconfig
6. 测试安装及配置是否成功
使用OpenCV自带的示例,按照下图操作
bash
cd /opencv-master/samples/cpp/example_cmake
cmake .
make
./opencv_example
执行完以上命令后会调用摄像头,显示hello world
7.上述情况编译报错
https://blog.csdn.net/qq_38904904/article/details/131164283
https://blog.csdn.net/PecoHe/article/details/97476135
[WSL安装软件报错/sbin/ldconfig.real: /usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link-CSDN博客](