我的环境可以进行科学上网,编译的时候就不会出现各种下载错误~
sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev
sudo apt-get install build-essential cmake git libgtk2.0-dev libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
如果提示找不到libfaac-dev软件包
sudo apt-add-repository "deb http://us.archive.ubuntu.com/ubuntu trusty main multiverse"
sudo apt update
sudo apt install libjasper1 libjasper-dev
如果提示找不到libjasper-dev软件包
sudo apt-add-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
cd ~/ #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文件,要使之后添加的环境变量有效,就必须得有这个文件,很重要。
执行
sudo make -j4 #时间较长
sudo make install
sudo ldconfig #生效,可以第5步测试可不可以,不可以就继续进行第四步
sudo gedit /etc/ld.so.conf.d/opencv.conf
末尾添加(可能空的)
/usr/local/lib
生效
sudo ldconfig
sudo gedit /etc/ld.so.conf
末尾添加(若已经有了第一行就直接添加第二行)
include /etc/ld.so.conf.d/*.conf
include /usr/local/lib
生效
sudo ldconfig
sudo gedit /etc/bash.bashrc
末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
生效
source /etc/bash.bashrc
cd ../samples/cpp/example_cmake
cmake .
make
./opencv_example
或(我采用这种方式)
pkg-config opencv --modversion #opencv3
pkg-config --modversion opencv4 #opencv4
显示出版本号就说明安装完毕。
出现错误
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,然后解决了。
参考1:https://blog.csdn.net/qq_45488453/article/details/110289423
参考2:https://blog.csdn.net/qq_41169447/article/details/109139884