OpenCV环境配置:Ubuntu18.04编译OpenCV4.2.0和contrib模块记录
1.下载源码和安装依赖包2.解压源码文件和配置路径3.配置Cmake并编译OpenCV4.导入OpenCV到环境变量5.使用编译好的库6.在Qt工程下使用OpenCV1.下载源码和安装依赖包
OpenCV4.2.0
和contrib
源码下载链接如下:
OpenCV
源码下载: /opencv/opencv/tar.gz/4.2.0contrib
模块源码下载: /opencv/opencv_contrib/tar.gz/4.2.0
首先在终端中输入如下命令来安装依赖包:
sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev sudo apt install build-essential qt5-default ccache libv4l-dev libavresample-dev libgphoto2-dev libopenblas-base libopenblas-dev doxygen openjdk-8-jdk pylint libvtk6-dev
Tips:在之前几次编译OpenCV
的过程中我发现安装上面的依赖包的时候基本都会出现一些问题,这次我的Ubuntu
软件源用的是南邮源,输入上面的命令安装包时居然一点问题没有报,因此如果出现问题的时候,可以尝试将你的Ubuntu
软件源切换到NJUPT
试试。
2.解压源码文件和配置路径
使用如下命令解压源码文件:
tar -zxvf opencv-4.2.0.tar.gz.gztar -zxvf opencv_contrib-4.2.0.tar.gz.gz
将解压后的opencv_contrib-4.2.0
复制到opencv-4.2.0
目录下,并且在opencv-4.2.0
目录下新建build
目录并进入:
cp opencv_contrib-4.2.0 -rf opencv-4.2.0cd opencv-4.2.0/ && mkdir buildcd build
最终opencv-4.2.0
目录内如下:
3.配置Cmake并编译OpenCV
在build
目录内执行以下命令(注意contrib
路径换成自己的):
cmake -D CMAKE_BUILD_TYPE=Release \-D CMAKE_INSTALL_PREFIX=/usr/local \-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.2.0/modules \-D OPENCV_GENERATE_PKGCONFIG=YES \ ..
其中CMAKE_INSTALL_PREFIX
指定了编译好的库的目录,也就是说编译完成的OpenCV
库文件会在该目录下,OPENCV_GENERATE_PKGCONFIG
指定了生成pkgconfig
配置文件,这个文件在后续创建OpenCV工程的时会很有用。
完成后结果如下:
-- ------------------------------------------------------------------- -- Configuring done-- Generating done-- Build files have been written to: /home/peco/Documents/opencv-4.2.0/build
这个时候在执行:
sudo make -j4
就开始编译了,编译时间较长,但是会有进度提示,所以等着就好了。
如果报错:
fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory#include "features2d/test/test_detectors_regression.impl.hpp"
只需在该文件中将
#include "features2d/test/test_detectors_regression.impl.hpp"
改成:
#include "../../../../modules/features2d/test/test_detectors_invariance.impl.hpp
其他头文件缺失解决方法与此类似,其实上面的features2d目录
就在opencv-4.2.0/modules/
目录下,重新指定一下就行,编译完成输出如下:
[100%] Built target opencv_python2[100%] Building CXX object modules/optflow/CMakeFiles/opencv_perf_optflow.dir/perf/perf_rlof.cpp.o[100%] Building CXX object modules/optflow/CMakeFiles/opencv_perf_optflow.dir/perf/perf_tvl1optflow.cpp.o[100%] Linking CXX executable ../../bin/opencv_test_xfeatures2d[100%] Built target opencv_test_xfeatures2d[100%] Building CXX object modules/superres/CMakeFiles/opencv_perf_superres.dir/perf/perf_superres.cpp.o[100%] Linking CXX executable ../../bin/opencv_perf_optflow[100%] Built target opencv_perf_optflow[100%] Linking CXX executable ../../bin/opencv_perf_superres[100%] Built target opencv_perf_superres
安装编译好的库:
sudo make install
4.导入OpenCV到环境变量
还记得之前的-D OPENCV_GENERATE_PKGCONFIG=YES \
吗,该选项可以帮助我们导出库的信息方便引用,该过程主要有两步:
sudo vim /etc/ld.so.conf
首先编辑ld.so.conf
文件,在末尾加上:
include /usr/loacal/lib
然后终端执行命令:
sudo ldconfig
然后修改.bashrc
文件:
sudo vim ~/.bashrc
输入:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfigexport PKG_CONFIG_PATH
最后source
一下,让更改立即生效:
source ~/.bashrc
5.使用编译好的库
输入如下命令:
pkg-config --cflags --libs opencv4
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4 -L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
该输出可以作为g++
等编译器指定头文件,库文件等目录参数。
写一个简单的测试代码test_opencv.cpp
:
#include<iostream>#include<opencv2/opencv.hpp>using namespace std;using namespace cv;int main(int argc,char**argv){Mat input = imread("test.jpg");imshow("input",input);waitKey(0);return 0;}
使用如下命令编译该文件:
g++ test_opencv.cpp -I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4 -L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
编译命令比较长比较冗余,但是此处仅仅是为了测试,一般也没有谁会这么来编译C++
代码。编译过程没有问题,生成a.out
可执行文件,运行该文件显示出该目录下名为test.jpg
的图片,说明至此整个编译安装完成。
6.在Qt工程下使用OpenCV
要问Ubuntu
下好用的C++ IDE
有哪些,Qt
是我最推荐的,因为整个Qt
在Ubuntu
下也就一个多G,相比Windows
下VS
动辄十几二十个G,Qt
使用起来真的很清爽,而且Qt
也可以创建纯C
和纯C++
项目,因此非常推荐。
在Qt
中使用OpenCV
,其实只要配置.pro
文件就可以了,只需要在.pro
文件中添加上库的路径就可以:
## opencv libINCLUDEPATH+=/usr/local/include/opencv4/ \/usr/local/include/ \/usr/local/include/opencv4/opencv2LIBS+=/usr/locao/lib/*.so.*