OpenCV4.2.0+opencv_contrib安装(ubuntu16.04)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 github/opencv/opencv.git
git clone github/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 ..
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/xfeatures2
d_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/ake: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⽬录,查看,查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
随后继续报错,如下
~/opencv-4.2.0/opencv_contrib/modules/xfeatures2d/test/test_features2d.cpp:51:62: fatal error: features2d/test/test_detectors_regression.impl.hpp: No su ch file or directory
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/build.make:134: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures 2d.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.hpp 及
test_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_invarianc e.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 dire ctory
继续按上⾯⽅法,根据提⽰的缺少⽂件从~/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
4. 安装及配置
sudo make installubuntu怎么安装python
安装好后,include⽬录在/usr/local/include/opencv4下,库⽬录在/usr/local/lib下
4.1 配置环境变量
sudo gedit /etc/f.f
添加如下内容,并保存:
/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 -lopenc
v_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_sur face_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_xfeatures2d -l opencv_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测试
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论