基础条件
ubuntu 20.04
opencv 3.4.3
opencv 源码编译的安装步骤
第一步, 首先clone源码
git clone https://github.com/opencv/opencv.git
第二步,依赖包,执行下面的命令
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
第三步, 编译安装,依次运行下面的命令
cd ~/opencv-3.4.3 #进入opencv文件夹
mkdir build # 创建build文件夹
cd build # 进入build文件夹
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j7 # 多线程执行编译命令
sudo make install #安装完成,
下面的内容显示了安装成功后的信息反馈。 最后一句话, 说明ubuntu系统将opencv的运行路径,设置成了/usr/local/lib, 也就意味着opencv.pc也在这个路径下面。
-- Installing: /usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface_improved.xml
-- Installing: /usr/local/share/OpenCV/lbpcascades/lbpcascade_profileface.xml
-- Installing: /usr/local/share/OpenCV/lbpcascades/lbpcascade_silverware.xml
-- Installing: /usr/local/bin/opencv_traincascade
-- Set runtime path of "/usr/local/bin/opencv_traincascade" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_createsamples
-- Set runtime path of "/usr/local/bin/opencv_createsamples" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_annotation
-- Set runtime path of "/usr/local/bin/opencv_annotation" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_visualisation
-- Set runtime path of "/usr/local/bin/opencv_visualisation" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_interactive-calibration
-- Set runtime path of "/usr/local/bin/opencv_interactive-calibration" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_version
-- Set runtime path of "/usr/local/bin/opencv_version" to "/usr/local/lib"
接下来要配置环境变量
在/etc/bash.bashrc文件中添加opencv.pc的环境变量,
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
看最后两行。 然后运行命令source /etc/bash.bashrc使配置生效
在/etc/ld.so.conf文件中添加opencv的库路径/usr/local/lib, 记得还要运行sudo ldconfig。
通过这个命令来查看opencv的版本
pkg-config --modversion opencv
如果在运行上面命令的过程中, 出现下面的问题, 我们该怎么解决呢?
Package opencv was not found in the pkg-config search path. Perhaps you should add the directory containing `opencv.pc' to the PKG_CONFIG_PATH environment variable No package 'opencv' found /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/12/../../../a...
这个错误信息提示系统无法在 pkg-config 搜索路径中找到 opencv 包。你可以尝试将包含 ‘opencv.pc’ 文件的目录添加到 PKG_CONFIG_PATH 环境变量中。
opencv.pc就在pkconfig文件夹中。
所以我们的环境变量,必须能找到opencv.pc
环境变量配置好后, 再次运行 pkg-config --modversion opencv , 就出现了3.4.3的回复
接下来就是实例测试了。
转载一个感觉特别好的
opencv安装测试
网上常见的简单测试例子,用下面的命令编译。
g++ test.cpp -o test `pkg-config --cflags --libs opencv`