ubuntu 18.04 安装opencv3.4.5+opencv_contrib
1, download the installation package
opencv download and opencv_contrib
Note that version to be consistent
Download opencv opencv official website
opencv_contrib download github
2, the installation dependencies
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
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/tjk/opencv/opencv-3.4.5/opencv_contrib-3.4.5/modules/ -D OPENCV_ENABLE_NONFREE=True ..
3, install opencv
Unzip the downloaded installation package
//解压安装包
sudo unzip opencv-3.4.5.zip
sudo tar zxvf opencv_contrib-3.4.5.tar.gz
At this point you may also download the package that folder, where the best in your home directory in a folder and then re-establish the decompressed package to move into.
The file is then decompressed contrib moved to the next opencv-3.4.5 file
sudo cp -r opencv_contrib-3.4.5 opencv-3.4.5
and build a new file opencv-3.4.5 folder in the folder for subsequent compilation and generation.
cd opencv-3.4.5 #进入opencv文件夹
sudo mkdir build #新建文件夹build
Bulid into the folder and 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 ..
The latter two points can not be omitted, the default parameters representing other.
Note OPENCV_EXTRA_MODULES_PATH represent the true address of your placement opencv_contrib of modules folder
will then execute the above command will find there are some problems, the main problem is to download some files, including ippicv and face_landmark_model.dat and fatal error: boostdesc_bgm.i:. Vgg_generated_80 vgg_generated_120 . i no such file or directory.
Completed generating following screen will appear
-- Install path: /usr/local
--
-- cvconfig.h is in: /home/files/opencv-3.4.5/build
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/files/opencv-3.4.5/build
Here's a look at the below error problem, then solve the following in the make, a successful fight
//然后进行make
sudo make -j8 #j后面的数字代表用几个线程编译,此为8线程
Then a high probability to be wrong, as follows:
In fact, this error search online, there are many solutions, I use is a direct error in the file, the header file is modified as follows:
#ifdef HAVE_OPENCV_XFEATURES2D
//(原来的头文件)
//#include "opencv2/xfeatures2d/cuda.hpp"
//(修改后的头文件)
#include"/home/tjk/opencv/opencv-3.4.5/opencv_contrib-3.4.5/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp"
#endif
然后可能还会有一个错误,反正我是遇到了,
fatal error: boostdesc_bgm.i: vgg_generated_80. vgg_generated_120.i没有那个文件或目录
这个问题是一堆文件的缺失,包括
boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_lbgm.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
vgg_generated_120.i
vgg_generated_64.i
vgg_generated_80.i
vgg_generated_48.i
所以只需要将这些文件下载然后放置到opencv_contrib/modules/xfeatures2d/src/ 路径下即可。
当然,这些文件的下载路径可以在cmake//日志中查看,直接复制其下载地址到网页可以看该到文件的源码,直接拷贝源码并生成同名文件然后放置到opencv_contrib/modules/xfeatures2d/src/ 路径下即可。
或者从百度网盘中下载,然后解压并将这些文件放置到opencv_contrib/modules/xfeatures2d/src/ 路径下即可。
百度云盘连接
链接: 密码: os8l
解决完这些错误,可以继续
sudo make -j8
,
也有的教程建议删除build文件夹,在重来一次,两种我都试过,都可以。
然后执行:sudo make install
到这里就安装完了
4、配置环境变量
打开文件
sudo gedit /etc/ld.so.conf.d/opencv.conf
在空白文档中添加
/usr/local/lib
Next, configure the library
sudo ldconfig
to change the environment variables
sudo gedit /etc/bash.bashrc
In the final document plus
export PKG_CONFIG_PATH=/usr/local/opencv/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/opencv/lib
5, test
Here that two ways:
The first:
Write directly in vim,
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0);
while(1)
{
Mat frame;
cap >> frame;
imshow("1",frame);
if(waitKey(10) == 'q')
{
break;
}
}
}
FIG:
then compile and run command
follows:
g++ main.cpp `pkg-config opencv --cflags --libs opencv`
./a.out
Then the image will open notebook camera appeared.
The second:
Use qt, this is relatively simple, and easy to
figure, note the following. written .pro file.
Then directly run just fine.