前言:
安装依赖包的时候,最好使用main server源,可以有效避免找不到包的问题(原因见 error 7.1);
cmake 编译的时候会自动下载一些文件,如果可以的话,最好先弄个代理;
上述两步:安装依赖包和cmake,不出问题,则后续make和install基本不会出现问题。
参考了官方文档:https://docs.opencv.org/4.1.0/d7/d9f/tutorial_linux_install.html
该方法是源码安装,C++版。
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 libgtk2.0-dev
sudo apt-get install pkg-config
sudo apt-get install libavcodec-dev
sudo apt-get install libavformat-dev
……
否则编译的时候,会出许多错误,而且有些错误是在运行程序的时候突然跳出来的,
有些错误的解决方式是卸载并重装,因为依赖包要先于opencv安装。
ubuntu20.04自带两个python版本,默认是2.7的,建议换为3.x的那个。
cd ~/<my_working_directory> #my_working_directory是你保存opencv和opencv_contrib的文件夹
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
mv opencv_contrib/ opencv/ #将opencv_contrib移动到opencv目录里
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D OPENCV_GENERATE_PKGCONFIG=ON ..
cmake参数解释:
CMAKE_INSTALL_PREFIX=/usr/local 该路径是opencv的默认安装目录,不建议修改。
OPENCV_EXTRA_MODULES_PATH=…/opencv_contrib/modules 该路径是opencv_contrib所在的文件夹。层次关系:
opencv
opencv_contrib
build
OPENCV_GENERATE_PKGCONFIG=ON 表示会生成opencv.pc文件,要使之后添加的环境变量有效,就必须得有这个文件,很重要。
在Ubuntu18.04下采用编译安装opencv4,在
make
和make install
后想用pkg-config
查找opencv发现提示没有找到:~ pkg-config --libs opencv4 Package opencv4 was not found in the pkg-config search path. Perhaps you should add the directory containing `opencv4.pc' to the PKG_CONFIG_PATH environment variable No package 'opencv4' found
后来查找资料发现在
cmake
编译配置时候需要添加-D OPENCV_GENERATE_PKGCONFIG=ON
,这样编译安装后,就可以使用pkg-config检测。
还有一个更详细版的cmake参数配置(不怎么推荐):
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..
编译和安装
sudo make -j3 #时间较长
sudo make install
sudo vim /etc/ld.so.conf.d/opencv.conf
追加:
/usr/local/lib
使生效:
sudo ldconfig
bash.bashrc
文件:sudo vim /etc/bash.bashrc
#或 sudo gedit /etc/bash.bashrc
追加2行:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
使生效:
source /etc/bash.bashrc
sudo gedit /etc/ld.so.conf
添加2行:
include /etc/ld.so.conf.d/*.conf
include /usr/local/lib
使生效:
sudo ldconfig
cd ../samples/cpp/example_cmake
cmake .
make
./opencv_example
出现一张图,里面写着“Hello Opencv”,则配置完成。
或者
pkg-config opencv --modversion
显示出版本号就说明安装完毕。
关于opencv.pc文件:
ERROR: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
意思是它找不到opencv.pc这个文件,实际上该文件在/usr/local/lib/pkgconfig目录下,我在该目录下发现文件名字是opencv4.pc,复制了一个名为opencv.pc,然后解决了。
什么是.pc文件?
每个库的开发者都会提供一个.pc文件,在安装该库时生成,主要是声明库的路径、头文件路径以及编译时的链接参数和需要链接的库。比如我的opencv-3.3.1-dev.pc文件:
如果确定没有问题,则可以删除opencv和opencv_contrib文件夹了。
1.在sudo apt-get install libjasper-dev
时出现:
error E: Unable to locate package libjasper-dev
解决:
sudo vim /etc/apt/sources.list
# 最后一行加入:
deb http://security.ubuntu.com/ubuntu xenial-security main
# 保存并退出
sudo apt-get update
如果出现“无法获得锁“的问题:
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?那么:
ps -A|grep apt sudo kill PID #PID是进程ID sudo apt update
解释:找到占用的进程并杀死该进程。
如果继续出现:
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
那么:
sudo rm /var/lib/dpkg/lock sudo dpkg --configure -a sudo apt update
解释:删除锁文件,重新分配软件包,更新包源文件。
或者依次执行:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
2.在./opencv_example
时候出现:
Failed to load module “canberra-gtk-module”
解决:
sudo apt-get install libcanberra-gtk-module
3.errorE: unable to locate libjasper-dev
解决:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
3.error:unable …libdc1394-22
解决:换回默认源,再次执行:
sudo apt-get install libdc1394-22
ubuntu 20.04默认源是:
deb http://archive.ubuntu.com/ubuntu/ focal main restricted
deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted
deb http://archive.ubuntu.com/ubuntu/ focal universe
deb http://archive.ubuntu.com/ubuntu/ focal-updates universe
deb http://archive.ubuntu.com/ubuntu/ focal multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-updates multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ focal-security main restricted
deb http://security.ubuntu.com/ubuntu/ focal-security universe
deb http://security.ubuntu.com/ubuntu/ focal-security multiverse
4.error:fatal error: boostdesc_bgm.i: No such file or directory
文件链接:http://47.95.13.239/play/OpenCV/
参考:https://www.cnblogs.com/ZHJ0125/archive/2004/01/13/12904507.html
将下载的文件添加到opencv_contrib/modules/xfeatures2d/src/
5.fatal error: features2d/test/test_detectors_regression.impl.hpp
解决:
#在opencv总目录中查找
find -name test_detectors_regression.impl.hpp
#显示在./opencv/modules/features2d/
#将features2d/文件复制到build文件夹下
cp -r ./modules/features2d ./build/
卸载opencv?
cd build
sudo make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
或者直接使用:
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
以上。