OpenCV 4.2.0 + opencv_contrib安装 (ubuntu16.04)

官方发布版本:
https://opencv.org/releases/

官方教程地址:
http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/tutorials.html

ubuntu16.04 下安装openCV 4.2.0 步骤

环境 ubuntu16.04 + opencv4.2.0+opencv_contrib4.2.0

1、 安装依赖

# ubuntu16.04 默认自带安装
sudo apt-get install build-essential
# ubuntu16.04 除了git,其他默认自带安装
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
# libdc1394-22-dev 需要安装,其他默认自带安装
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

build-essential是什么
linux操作系统上面开发程序, 光有了gcc 是不行的它还需要一个 build-essential软件包
作用是提供编译程序必须软件包的列表信息也就是说 编译程序有了这个软件包它才知道 头文件在哪 才知道库函数在哪
还会下载依赖的软件包 最后才组成一个开发环境
当然 build-essential包 安装时 需要的依赖包 有些你用不上

2、 下载源代码

# 克隆源代码
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
# 选择4.2.0版本
cd opencv
git checkout 4.2.0
cd opencv_contrib
git checkout 4.2.0

注意: opencv与opencv_contrib的版本需要要匹配一致

3、编译opencv

使用命令cmake编译
cd opencv
mkdir build
cd build
cmake cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -DINSTALL_PYTHON_EXAMPLES=ON \
    -DINSTALL_C_EXAMPLES=ON \
    -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib-4.2.0>/modules \
    -DBUILD_EXAMPLES=ON ..
make
使用cmake-gui编译
# 安装cmake-gui
sudo apt-get install cmake-qt-gui
# 进入opencv源代码目录
cd opencv
# 创建一个build文件夹,用于存放生成的代码
mkdir build
cd build
# 启动cmake-gui
cmake-gui ..

OpenCV 4.2.0 + opencv_contrib安装 (ubuntu16.04)_第1张图片
step.1 点击 “Browse Source” ,选择源代码根路劲opencv
step.2 点击 “Browse Build”,选择目标代码目录 build
step.3 点击 “Configure”,弹出CMakeSetup窗口,选择Unix Makefiles,选择Use default native compilers生成配置项
step.4 配置编译参数

Name Value 备注
CMAKE_BUILD_TYPE Release
CMAKE_INSTALL_PREFIX /usr/local 安装目录
OPENCV_EXTRA_MODULES_PATH opencv-4.2.0/opencv_contrib/modules opencv_contrib目录
BUILD_DOCS ON 构建文档
BUILD_EXAMPLES ON 构建所有示例
INSTALL_PYTHON_EXAMPLES ON
INSTALL_C_EXAMPLES ON
OPENCV_GENERATE_PKGCONFIG ON 务必勾选
WITH_OPENGL ON
WITH_QT ON 启用qt

OPENCV_GENERATE_PKGCONFI 忘记勾选怎么办?认真走完本贴吧,本贴就是未勾选,导致测试失败的
血的教训,良心之作,献给各位程序猿,让大伙少走弯路。如果已勾选,后面就无需自己造opencv4.pc

step.5 再次点击"Configure",然后点击"Generate"
step.6 开始编译

cd opencv/build
make

等待半个钟后,出现如下错

...
Scanning dependencies of target opencv_xfeatures2d
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/xfeatures2d_init.cpp.o
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/pct_signatures.cpp.o
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/affine_feature2d.cpp.o
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/gms.cpp.o
[ 69%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o
~/opencv-4.2.0/opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:654:37: fatal error: boostdesc_bgm.i: No such file or directory
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/build.make:163: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o' failed
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o] Error 1
CMakeFiles/Makefile2:12636: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all' failed
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

原因:的Configure时,已经出现提示 多个服务器连接不上的红色错误提示,如:
CMake Warning at /home/bing/study/opencv-4.2.0/opencv/cmake/OpenCVDownload.cmake:202 (message):
xfeatures2d/vgg: Download failed: 7;“Couldn’t connect to server”

解决办法
进行make时的,关键错误提示 opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:654:37: fatal error: boostdesc_bgm.i: No such file or directory compilation terminated.

进入build目录,查看CmakeDownloadLog.txt,查找boostdesc_bgm.i关键词,下载所需要的文件到opencv_contrib/modules/xfeatures2d/src/下,包含的文件有:

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

boostdesc_bgm.i下载地址

随后继续报错,如下

~/opencv-4.2.0/opencv_contrib/modules/xfeatures2d/test/test_features2d.cpp:51:62: fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/build.make:134: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_features2d.cpp.o' failed
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_features2d.cpp.o] Error 1
CMakeFiles/Makefile2:12703: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/all' failed
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/all] Error 2
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2

解决办法
step.1 进入opencv源码目录~/opencv/modules/features2d/test/,拷贝test_detectors_regression.impl.hpptest_descriptors_regression.impl.hpp~/opencv_contrib/modules/xfeatures2d/test/
step.2 编辑 test_features2d.cpp ,修改include文件地址

// #include "features2d/test/test_detectors_regression.impl.hpp"
// #include "features2d/test/test_descriptors_regression.impl.hpp"
// 修改文件地址如下
#include "test_detectors_regression.impl.hpp"
#include "test_descriptors_regression.impl.hpp"

修改完成后,build目录下继续make

ps: make 会继续上一次编译的进度,无需重头开始编译

cd opencv/build
make

后续会继续报错:

~/opencv-4.2.0/opencv_contrib/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp:7:82: fatal error: features2d/test/test_detectors_invariance.impl.hpp: No such file or directory
In file included from ~/opencv-4.2.0/opencv_contrib/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp:7:0:
~/opencv-4.2.0/opencv_contrib/modules/xfeatures2d/test/test_detectors_invariance.impl.hpp:5:37: fatal error: test_invariance_utils.hpp: No such file or directory

继续按上面方法,根据提示的缺少文件从~/opencv/modules/features2d/test中拷贝如下文件到~/opencv_contrib/modules/xfeatures2d/test/

test_detectors_invariance.impl.hpp
test_descriptors_invariance.impl.hpp
test_invariance_utils.hpp

然后修改test_rotation_and_scale_invariance.cpp文件

// #include "features2d/test/test_detectors_invariance.impl.hpp" // main OpenCV repo
// #include "features2d/test/test_descriptors_invariance.impl.hpp" // main OpenCV repo
#include "test_detectors_invariance.impl.hpp" // main OpenCV repo
#include "test_descriptors_invariance.impl.hpp" // main OpenCV repo

继续执行make
顺利完成安装,上个段100%的安装进度信息

[100%] Linking CXX executable ../../bin/example_tapi_ufacedetect
[100%] Built target example_tapi_ufacedetect
Scanning dependencies of target example_tapi_dense_optical_flow
[100%] Building CXX object samples/tapi/CMakeFiles/example_tapi_dense_optical_flow.dir/dense_optical_flow.cpp.o
[100%] Linking CXX executable ../../bin/example_tapi_dense_optical_flow
[100%] Built target example_tapi_dense_optical_flow
Scanning dependencies of target example_tapi_camshift
[100%] Building CXX object samples/tapi/CMakeFiles/example_tapi_camshift.dir/camshift.cpp.o
[100%] Linking CXX executable ../../bin/example_tapi_camshift
[100%] Built target example_tapi_camshift
Scanning dependencies of target example_tapi_squares
[100%] Building CXX object samples/tapi/CMakeFiles/example_tapi_squares.dir/squares.cpp.o
[100%] Linking CXX executable ../../bin/example_tapi_squares
[100%] Built target example_tapi_squares
  1. 安装及配置
sudo make install

安装好后,include目录在/usr/local/include/opencv4下,库目录在/usr/local/lib

4.1 配置环境变量

sudo gedit /etc/ld.so.conf.d/opencv.conf

添加如下内容,并保存:

/usr/local/lib

配置库

sudo ldconfig

4.2 添加修改环境变量

sudo gedit /etc/bash.bashrc

在末尾添加如下内容

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig 
export PKG_CONFIG_PATH

重启系统

reboot

测试安装情况,查看安装版本

pkg-config opencv4 --modversion 
# or
pkg-config --cflags --libs opencv4

Package opencv 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

原因:在configrue时,为指定 OPENCV_GENERATE_PKGCONFIG为NO(OFF为关闭),而安装后并未自动生成响应的opencv4.pc文件
网上的许多的教程都未给出该选项
当然,如果你勾选吧,那么,就没有下面4.3的问题啦

4.3 创建opencv4.pc

sudo gedit /usr/local/lib/pkgconfig/opencv4.pc

添加如下内容

# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv4/opencv
includedir_new=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.2.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -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_video -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}

再次pkg-config测试

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_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -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_video -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

测试成功! (不容易呐,安装了一天)

pkg-config 命令介绍:用于获得某一个库/模块的所有编译相关的信息
所有用opencv的其他程序,在编译时,只需要写“pkg-config opencv –libs –cflags”,而不需要自己去找opencv的头文件在哪里,要链接的库在哪里!省时省力!
pkg-config介绍参考

sudo ldconfig 命令介绍: ldconfig是一个动态链接库管理命令,其目的为了让动态链接库为系统所共享。
主要是在默认搜寻目录/lib/usr/lib以及动态库配置文件/etc/ld.so.conf内所列的目录下,否则需要 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local//lib
搜索出可共享的动态链接库(格式如lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件,
缓存文件默认为/etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表。
linux下的共享库机制采用了类似高速缓存机制,将库信息保存在/etc/ld.so.cache,程序连接的时候首先从这个文件里查找,然后再到ld.so.conf的路径中查找。
为了让动态链接库为系统所共享,需运行动态链接库的管理命令ldconfig,此执行程序存放在/sbin目录下。
ldconfig介绍参考 https://blog.csdn.net/winycg/article/details/80572735

  1. 终极测试,写一个opencv的demo
    创建一个文件showEagle.cpp
    同级目录下存一张雄鹰的图片eagle.jpg
    OpenCV 4.2.0 + opencv_contrib安装 (ubuntu16.04)_第2张图片
    showEagle.cpp代码如下:
#include 
#include 
using namespace cv;
int main(int argc, char const *argv[])
{
	Mat src = imread("eagle.jpg");
	imshow("src", src);
	Mat src_gray;
	cvtColor(src, src_gray, COLOR_BGR2GRAY); // 注意,有的教程CV_BGR2GRAY,opencv4下会报错
	imshow("src_gray", src_gray);
	imwrite("eagle_gray.jpg", src_gray);
	waitKey(0);
	return 0;
}

开始编译

g++ showEagle.cpp -o showEagle `pkg-config --cflags --libs opencv4`

无限报错报错:error “OpenCV 4.x+ requires enabled C++11 support”
再来一次

g++ -std=c++11 showEagle.cpp -o showEagle `pkg-config --cflags --libs opencv4`

完美!效果如下
OpenCV 4.2.0 + opencv_contrib安装 (ubuntu16.04)_第3张图片
OpenCV 4.2.0 + opencv_contrib安装 (ubuntu16.04)_第4张图片

你可能感兴趣的:(OpenCV)