在Ubuntu20.04 LTS中安装OpenCV4.5.4

文章目录

    • 系统配置:
    • 一、简介
    • 二、安装方式
    • 安装依赖项
    • 三、下载OpenCV源程序
    • 四、 CMAKE配置和安装
    • 五、安装后的环境配置
    • 六、pkg-config及其应用
    • 文献

系统配置:

Intel® Core™ i5-8500 CPU ;
Ubuntu 20.04.3 LTS

一、简介

Opencv是一个开源的计算机视觉库,目前,在众多的图像处理相关领域都会使用Opencv。Opencv很多现成的图像处理函数可以帮助我们快速实现相关工程算法。
opencv的一些核心组件如下:

** core(核心库). Core functionality
imgproc(图像处理). Image Processing
imgcodecs(图像读取和保存). Image file reading and writing
videoio(视频输入输出). Video I/O
highgui(高级GUI界面). High-level GUI
video(视频分析). Video Analysis
calib3d(相机矫正和三维重建). Camera Calibration and 3D Reconstruction
features2d(二维特征框架). 2D Features Framework
objdetect(物体检测). Object Detection
dnn(深度学习). Deep Neural Network module
ml(机器学习). Machine Learning
flann(多维空间聚类和搜索). Clustering and Search in Multi-Dimensional Spaces
photo(摄影成像). Computational Photography
stitching(图像配准). Images stitching
gapi(图形api). Graph API**

二、安装方式

选择下载源程序文件CMAKE编译安装

安装依赖项

1. 安装必选项:CMAKE, GCC, Python-devel和Numpy ,将用于安装配置和软件编译;

sudo apt-get install cmake
sudo apt-get install gcc g++

为支持Python2

sudo apt-get install python-dev python-numpy

为支持Python3

sudo apt-get install python3-dev python3-numpy

为支持GUI , Camera (v4l)和Media (ffmpeg, gstreamer) 等,安装GTK,

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

为支持*GTK2**,

sudo apt-get install libgtk2.0-dev

为支持*GTK3**,

sudo apt-get install libgtk-3-dev

2. 安装可选项
可选项用于支持PNG, JPEG, JPEG2000, TIFF, WebP 等图片格式。

sudo apt-get install libpng-dev
sudo apt-get install libjpeg-dev
sudo apt-get install libopenexr-dev
sudo apt-get install libtiff-dev
sudo apt-get install libwebp-dev

三、下载OpenCV源程序

安装git 应用。
源程序下载地址: GitHub Repository

sudo apt-get install git
git clone https://github.com/opencv/opencv.git

克隆下载后,将在home的当前目录建立文件夹opencv。
进入opencv文件夹,建立build目录,用于存放编译的文件。
进入刚建立的build文件夹,

mkdir build
cd build

四、 CMAKE配置和安装

cmake 编译配置,用于选择安装路径、安装模块和附加库等,配置完成后,编译按照配置参数自动进行。
1. 预编译

cmake ../

OpenCV的默认安装路径是**/usr/local**。

General configuration for OpenCV 4.5.4-dev =====================================
--   Version control:               4.5.4-138-g2f6d2b08aa
-- 
--   Platform:
--     Timestamp:                   2021-11-17T08:28:35Z
--     Host:                        Linux 5.11.0-40-generic x86_64
--     CMake:                       3.16.3
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               Release
C/C++:
--     Built as dynamic libs?:      YES
--     C++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 9.3.0)

OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python2 python3 stitching ts video videoio
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 java
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 

--   GUI:                           GTK3
--     GTK+:                        YES (ver 3.24.20)
--       GThread :                  YES (ver 2.64.6)
--       GtkGlExt:                  NO
--     VTK support:                 NO
Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   build (ver 2.4.0)

Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.18)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.18)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.5)
--     install path:                lib/python2.7/dist-packages/cv2/python-2.7
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.8.10)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.10)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
--     install path:                lib/python3.8/dist-packages/cv2/python-3.8
-- 
--   Python (for build):            /usr/bin/python2.7

Install to:                    /usr/local

2.编译安装

make
sudo make install
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/share/licenses/opencv4/ippicv-readme.htm
-- Installing: /usr/local/share/licenses/opencv4/ippicv-EULA.txt
-- Installing: /usr/local/share/licenses/opencv4/ippicv-third-party-programs.txt
-- Installing: /usr/local/share/licenses/opencv4/ippiw-support.txt
-- Installing: /usr/local/share/licenses/opencv4/ippiw-third-party-programs.txt
-- Installing: /usr/local/share/licenses/opencv4/ippiw-EULA.txt
-- Installing: /usr/local/share/licenses/opencv4/opencl-headers-LICENSE.txt
-- Installing: /usr/local/share/licenses/opencv4/ade-LICENSE
-- Installing: /usr/local/include/opencv4/opencv2/cvconfig.h
-- Installing: /usr/local/include/opencv4/opencv2/opencv_modules.hpp
-- Installing: /usr/local/lib/cmake/opencv4/OpenCVModules.cmake
-- Installing: /usr/local/lib/cmake/opencv4/OpenCVModules-release.cmake
-- Installing: /usr/local/lib/cmake/opencv4/OpenCVConfig-version.cmake
-- Installing: /usr/local/lib/cmake/opencv4/OpenCVConfig.cmake
-- Installing: /usr/local/bin/setup_vars_opencv4.sh
-- Installing: /usr/local/share/opencv4/valgrind.supp
-- Installing: /usr/local/share/opencv4/valgrind_3rdparty.supp
-- Installing: /usr/local/share/licenses/opencv4/libopenjp2-README.md
-- Installing: /usr/local/share/licenses/opencv4/libopenjp2-LICENSE
-- Installing: /usr/local/share/licenses/opencv4/protobuf-LICENSE
-- Installing: /usr/local/share/licenses/opencv4/protobuf-README.md
-- Installing: /usr/local/share/licenses/opencv4/quirc-LICENSE
-- Installing: /usr/local/share/licenses/opencv4/ittnotify-LICENSE.BSD
-- Installing: /usr/local/share/licenses/opencv4/ittnotify-LICENSE.GPL
-- Installing: /usr/local/include/opencv4/opencv2/opencv.hpp
-- Installing: /usr/local/lib/libopencv_core.so.4.5.4
-- Installing: /usr/local/lib/libopencv_core.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_core.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_core.so
-- Installing: /usr/local/include/opencv4/opencv2/core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/affine.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/async.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/base.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/bindings_utils.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/bufferpool.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/check.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/core_c.h
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda.inl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/block.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/border_interpolate.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/color.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/common.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/datamov_utils.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/detail/color_detail.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/detail/reduce.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/detail/reduce_key_val.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/detail/transform_detail.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/detail/type_traits_detail.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/detail/vec_distance_detail.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/dynamic_smem.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/emulation.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/filters.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/funcattrib.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/functional.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/limits.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/reduce.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/saturate_cast.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/scan.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/simd_functions.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/transform.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/type_traits.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/utility.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/vec_distance.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/vec_math.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/vec_traits.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/warp.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/warp_reduce.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda/warp_shuffle.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda_stream_accessor.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cuda_types.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cv_cpu_dispatch.h
-- Installing: /usr/local/include/opencv4/opencv2/core/cv_cpu_helper.h
-- Installing: /usr/local/include/opencv4/opencv2/core/cvdef.h
-- Installing: /usr/local/include/opencv4/opencv2/core/cvstd.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cvstd.inl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/cvstd_wrapper.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/detail/async_promise.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/detail/exception_ptr.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/directx.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/dualquaternion.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/dualquaternion.inl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/eigen.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/fast_math.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/hal.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/interface.h
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_avx512.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_cpp.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_forward.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_msa.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_neon.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_rvv.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_rvv071.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_sse.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_sse_em.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_vsx.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/intrin_wasm.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/msa_macros.h
-- Installing: /usr/local/include/opencv4/opencv2/core/hal/simd_utils.impl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/mat.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/mat.inl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/matx.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/neon_utils.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/ocl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/ocl_genbase.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/ocl_defs.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/opencl_info.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/opencl_svm.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_clblas.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_clfft.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_core_wrappers.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/opencl_clblas.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/opencl_clfft.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/opencl_core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/opencl_core_wrappers.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/opencl_gl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/opencl_gl_wrappers.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/opencl_svm_20.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/opencl_svm_definitions.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opencl/runtime/opencl_svm_hsa_extension.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/opengl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/operations.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/optim.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/ovx.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/parallel/backend/parallel_for.openmp.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/parallel/backend/parallel_for.tbb.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/parallel/parallel_backend.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/persistence.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/quaternion.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/quaternion.inl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/saturate.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/simd_intrinsics.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/softfloat.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/sse_utils.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/traits.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/types.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/types_c.h
-- Installing: /usr/local/include/opencv4/opencv2/core/utility.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/allocator_stats.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/allocator_stats.impl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/filesystem.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/instrumentation.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/logger.defines.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/logger.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/logtag.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/tls.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/utils/trace.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/va_intel.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/version.hpp
-- Installing: /usr/local/include/opencv4/opencv2/core/vsx_utils.hpp
-- Installing: /usr/local/share/licenses/opencv4/SoftFloat-COPYING.txt
-- Installing: /usr/local/lib/libopencv_flann.so.4.5.4
-- Installing: /usr/local/lib/libopencv_flann.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_flann.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_flann.so
-- Installing: /usr/local/include/opencv4/opencv2/flann.hpp
-- Installing: /usr/local/include/opencv4/opencv2/flann/all_indices.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/allocator.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/any.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/autotuned_index.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/composite_index.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/config.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/defines.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/dist.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/dummy.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/dynamic_bitset.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/flann.hpp
-- Installing: /usr/local/include/opencv4/opencv2/flann/flann_base.hpp
-- Installing: /usr/local/include/opencv4/opencv2/flann/general.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/ground_truth.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/hdf5.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/heap.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/hierarchical_clustering_index.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/index_testing.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/kdtree_index.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/kdtree_single_index.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/kmeans_index.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/linear_index.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/logger.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/lsh_index.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/lsh_table.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/matrix.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/miniflann.hpp
-- Installing: /usr/local/include/opencv4/opencv2/flann/nn_index.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/object_factory.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/params.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/random.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/result_set.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/sampling.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/saving.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/simplex_downhill.h
-- Installing: /usr/local/include/opencv4/opencv2/flann/timer.h
-- Installing: /usr/local/lib/libopencv_imgproc.so.4.5.4
-- Installing: /usr/local/lib/libopencv_imgproc.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_imgproc.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_imgproc.so
-- Installing: /usr/local/include/opencv4/opencv2/imgproc.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgproc/bindings.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgproc/detail/gcgraph.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgproc/hal/hal.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgproc/hal/interface.h
-- Installing: /usr/local/include/opencv4/opencv2/imgproc/imgproc.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgproc/imgproc_c.h
-- Installing: /usr/local/include/opencv4/opencv2/imgproc/segmentation.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgproc/types_c.h
-- Installing: /usr/local/lib/libopencv_ml.so.4.5.4
-- Installing: /usr/local/lib/libopencv_ml.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_ml.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_ml.so
-- Installing: /usr/local/include/opencv4/opencv2/ml.hpp
-- Installing: /usr/local/include/opencv4/opencv2/ml/ml.hpp
-- Installing: /usr/local/include/opencv4/opencv2/ml/ml.inl.hpp
-- Installing: /usr/local/lib/libopencv_photo.so.4.5.4
-- Installing: /usr/local/lib/libopencv_photo.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_photo.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_photo.so
-- Installing: /usr/local/include/opencv4/opencv2/photo.hpp
-- Installing: /usr/local/include/opencv4/opencv2/photo/cuda.hpp
-- Installing: /usr/local/include/opencv4/opencv2/photo/legacy/constants_c.h
-- Installing: /usr/local/include/opencv4/opencv2/photo/photo.hpp
-- Installing: /usr/local/lib/libopencv_dnn.so.4.5.4
-- Installing: /usr/local/lib/libopencv_dnn.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_dnn.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_dnn.so
-- Installing: /usr/local/include/opencv4/opencv2/dnn.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/all_layers.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/dict.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/dnn.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/dnn.inl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/layer.details.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/layer.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/shape_utils.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/utils/debug_utils.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/utils/inference_engine.hpp
-- Installing: /usr/local/include/opencv4/opencv2/dnn/version.hpp
-- Installing: /usr/local/lib/libopencv_features2d.so.4.5.4
-- Installing: /usr/local/lib/libopencv_features2d.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_features2d.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_features2d.so
-- Installing: /usr/local/include/opencv4/opencv2/features2d.hpp
-- Installing: /usr/local/include/opencv4/opencv2/features2d/features2d.hpp
-- Installing: /usr/local/include/opencv4/opencv2/features2d/hal/interface.h
-- Installing: /usr/local/lib/libopencv_imgcodecs.so.4.5.4
-- Installing: /usr/local/lib/libopencv_imgcodecs.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_imgcodecs.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_imgcodecs.so
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs/imgcodecs.hpp
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs/imgcodecs_c.h
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs/ios.h
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs/legacy/constants_c.h
-- Installing: /usr/local/include/opencv4/opencv2/imgcodecs/macosx.h
-- Installing: /usr/local/lib/libopencv_videoio.so.4.5.4
-- Installing: /usr/local/lib/libopencv_videoio.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_videoio.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_videoio.so
-- Installing: /usr/local/include/opencv4/opencv2/videoio.hpp
-- Installing: /usr/local/include/opencv4/opencv2/videoio/cap_ios.h
-- Installing: /usr/local/include/opencv4/opencv2/videoio/legacy/constants_c.h
-- Installing: /usr/local/include/opencv4/opencv2/videoio/registry.hpp
-- Installing: /usr/local/include/opencv4/opencv2/videoio/videoio.hpp
-- Installing: /usr/local/include/opencv4/opencv2/videoio/videoio_c.h
-- Installing: /usr/local/lib/libopencv_calib3d.so.4.5.4
-- Installing: /usr/local/lib/libopencv_calib3d.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_calib3d.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_calib3d.so
-- Installing: /usr/local/include/opencv4/opencv2/calib3d.hpp
-- Installing: /usr/local/include/opencv4/opencv2/calib3d/calib3d.hpp
-- Installing: /usr/local/include/opencv4/opencv2/calib3d/calib3d_c.h
-- Installing: /usr/local/lib/libopencv_highgui.so.4.5.4
-- Installing: /usr/local/lib/libopencv_highgui.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_highgui.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_highgui.so
-- Installing: /usr/local/include/opencv4/opencv2/highgui.hpp
-- Installing: /usr/local/include/opencv4/opencv2/highgui/highgui.hpp
-- Installing: /usr/local/include/opencv4/opencv2/highgui/highgui_c.h
-- Installing: /usr/local/lib/libopencv_objdetect.so.4.5.4
-- Installing: /usr/local/lib/libopencv_objdetect.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_objdetect.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_objdetect.so
-- Installing: /usr/local/include/opencv4/opencv2/objdetect.hpp
-- Installing: /usr/local/include/opencv4/opencv2/objdetect/detection_based_tracker.hpp
-- Installing: /usr/local/include/opencv4/opencv2/objdetect/face.hpp
-- Installing: /usr/local/include/opencv4/opencv2/objdetect/objdetect.hpp
-- Installing: /usr/local/lib/libopencv_stitching.so.4.5.4
-- Installing: /usr/local/lib/libopencv_stitching.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_stitching.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_stitching.so
-- Installing: /usr/local/include/opencv4/opencv2/stitching.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/autocalib.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/blenders.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/camera.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/exposure_compensate.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/matchers.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/motion_estimators.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/seam_finders.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/timelapsers.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/util.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/util_inl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/warpers.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/detail/warpers_inl.hpp
-- Installing: /usr/local/include/opencv4/opencv2/stitching/warpers.hpp
-- Installing: /usr/local/lib/libopencv_video.so.4.5.4
-- Installing: /usr/local/lib/libopencv_video.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_video.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_video.so
-- Installing: /usr/local/include/opencv4/opencv2/video.hpp
-- Installing: /usr/local/include/opencv4/opencv2/video/background_segm.hpp
-- Installing: /usr/local/include/opencv4/opencv2/video/detail/tracking.detail.hpp
-- Installing: /usr/local/include/opencv4/opencv2/video/legacy/constants_c.h
-- Installing: /usr/local/include/opencv4/opencv2/video/tracking.hpp
-- Installing: /usr/local/include/opencv4/opencv2/video/video.hpp
-- Installing: /usr/local/lib/libopencv_gapi.so.4.5.4
-- Installing: /usr/local/lib/libopencv_gapi.so.4.5
-- Set runtime path of "/usr/local/lib/libopencv_gapi.so.4.5.4" to "/usr/local/lib"
-- Installing: /usr/local/lib/libopencv_gapi.so
-- Installing: /usr/local/include/opencv4/opencv2/gapi.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/cpu/core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/cpu/gcpukernel.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/cpu/imgproc.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/cpu/stereo.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/cpu/video.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/fluid/core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/fluid/gfluidbuffer.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/fluid/gfluidkernel.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/fluid/imgproc.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/garg.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/garray.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gasync_context.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gcall.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gcommon.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gcompiled.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gcompiled_async.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gcompoundkernel.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gcomputation.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gcomputation_async.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gframe.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gkernel.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gmat.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gmetaarg.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gopaque.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gproto.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gpu/core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gpu/ggpukernel.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gpu/imgproc.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gscalar.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gstreaming.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gtransform.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gtype_traits.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/gtyped.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/imgproc.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/infer.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/infer/bindings_ie.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/infer/ie.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/infer/onnx.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/infer/parsers.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/media.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/ocl/core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/ocl/goclkernel.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/ocl/imgproc.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/opencv_includes.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/operators.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/own/assert.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/own/convert.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/own/cvdefs.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/own/exports.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/own/mat.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/own/saturate.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/own/scalar.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/own/types.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/plaidml/core.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/plaidml/gplaidmlkernel.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/plaidml/plaidml.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/python/python.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/render.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/render/render.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/render/render_types.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/rmat.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/s11n.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/s11n/base.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/stereo.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/streaming/cap.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/streaming/desync.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/streaming/format.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/streaming/meta.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/streaming/source.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/streaming/sync.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/util/any.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/util/compiler_hints.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/util/copy_through_move.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/util/optional.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/util/throw.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/util/type_traits.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/util/util.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/util/variant.hpp
-- Installing: /usr/local/include/opencv4/opencv2/gapi/video.hpp
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/__init__.py
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/load_config_py2.py
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/load_config_py3.py
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/config.py
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/misc/__init__.py
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/misc/version.py
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/mat_wrapper/__init__.py
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/utils/__init__.py
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/gapi/__init__.py
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/python-2.7/cv2.so
-- Set runtime path of "/usr/local/lib/python2.7/dist-packages/cv2/python-2.7/cv2.so" to "/usr/local/lib"
-- Installing: /usr/local/lib/python2.7/dist-packages/cv2/config-2.7.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/__init__.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/load_config_py2.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/load_config_py3.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/config.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/misc/__init__.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/misc/version.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/mat_wrapper/__init__.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/utils/__init__.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/gapi/__init__.py
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so
-- Set runtime path of "/usr/local/lib/python3.8/dist-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so" to "/usr/local/lib"
-- Installing: /usr/local/lib/python3.8/dist-packages/cv2/config-3.8.py
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_eye.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_eye_tree_eyeglasses.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_frontalcatface.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_frontalcatface_extended.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_frontalface_alt.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_frontalface_alt_tree.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_frontalface_default.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_fullbody.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_lefteye_2splits.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_licence_plate_rus_16stages.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_lowerbody.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_profileface.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_righteye_2splits.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_russian_plate_number.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_smile.xml
-- Installing: /usr/local/share/opencv4/haarcascades/haarcascade_upperbody.xml
-- Installing: /usr/local/share/opencv4/lbpcascades/lbpcascade_frontalcatface.xml
-- Installing: /usr/local/share/opencv4/lbpcascades/lbpcascade_frontalface.xml
-- Installing: /usr/local/share/opencv4/lbpcascades/lbpcascade_frontalface_improved.xml
-- Installing: /usr/local/share/opencv4/lbpcascades/lbpcascade_profileface.xml
-- Installing: /usr/local/share/opencv4/lbpcascades/lbpcascade_silverware.xml
-- Installing: /usr/local/bin/opencv_annotation
-- Set runtime path of "/usr/local/bin/opencv_annotation" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_visualisation
-- Set runtime path of "/usr/local/bin/opencv_visualisation" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_interactive-calibration
-- Set runtime path of "/usr/local/bin/opencv_interactive-calibration" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_version
-- Set runtime path of "/usr/local/bin/opencv_version" to "/usr/local/lib"
-- Installing: /usr/local/bin/opencv_model_diagnostics
-- Set runtime path of "/usr/local/bin/opencv_model_diagnostics" to "/usr/local/lib"

五、安装后的环境配置

  1. 打开配置文件:/etc/ld.so.conf.d/opencv4.conf
    在文件后加上一行 :/usr/local/lib
    其中,/user/local 就是opencv的安装路径,也是makefile中默认的安装路径。
sudo gedit /etc/ld.so.conf.d/opencv4.conf

使配置生效,

sudo ldconfig

2. 修改bash.bashrc文件

sudo gedit /etc/bash.bashrc

在文件末加入路径变量,

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

使修改生效,

source /etc/bash.bashrc

此时,测试OpenCV,

pkg-config opencv --modversion

弹出错误,提示:

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

然后,在/usr/local/lib/中建立目录pkgconfig

sudo mkdir pkgconfig 
cd pkgconfig

手动创建文件opencv4.pc

sudo gedit opencv4.pc

在opencv4.pc中输入:

                          
prefix=/usr/local
exec_prefix=${prefix}
includedir=/usr/local/include
libdir=/usr/local/lib

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.5.5
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videos>
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}

测试,

>>> python3
Traceback (most recent call last):
  File "", line 1, in <module>
NameError: name 'python3' is not defined
>>> import cv2 as cv
>>> print(cv.__version__)
4.5.4-dev

测试通过,安装完成。

六、pkg-config及其应用

configure根据传入的配置项目检查程序编译时所依赖的环境及对程序编译安装进行配置,最终生成编译所需的Makefile文件供Make读入使用进而调用相关编译器(gcc等)编译目标程序。而configure在检查相应依赖环境时(依赖软件的版本、相应库版本等),通常会通过工具pkg-config来检测相应依赖环境。
1. pkg-config功能
一般来说,如果库的头文件不在/usr/include目录中,那么在编译时需要用-I参数指定其路径。由于同一个库在不同系统上可能位于不同的目录下,用户安装库的时候也可以将库安装在不同的目录下,所以即使同一个库,由于路径不同,造成了用-I参数指定的头文件路径和在连接时用-L参数指定lib库的路径都可能不同,结果造成了编译命令界面的不统一。pkg-config 就是用来解决编译连接界面不统一问题的一个工具。
  pkg-config通过库提供的一个.pc文件获得库的各种必要信息,包括版本信息、编译和连接需要的参数等。这样,不管库文件安装在哪里,通过.pc文件都可以准确定位。主要功能有:
  <1> 检查库的版本号。
  <2> 获得编译预处理参数,如宏定义,头文件的路径。
  <3> 获得编译参数,如库及其依赖的其他库的位置,文件名及其他一些连接参数。
  <4> 自动加入所依赖的其他库的设置。
  2. pc文件
  在默认情况下,每个支持 pkg-config 的库对应的.pc文件在安装后都位于安装目录中的lib/pkgconfig目录下。如安装OpenCV时创建的opencv.pc 文件包含下面的代码:

prefix=/usr/local
exec_prefix=${prefix}
includedir=/usr/local/include
libdir=/usr/local/lib

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.5.5
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videos>
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}

新软件一般都会安装.pc文件,没有可以自己创建,并且设置环境变量PKG_CONFIG_PATH寻找.pc文件路径。
使用pkg-config工具提取库的编译和连接参数有两个基本的前提:
  <1> 库本身在安装的时候必须提供一个相应的.pc文件。不这样做的库说明不支持pkg-config工具的使用。
  <2> pkg-config必须知道要到哪里去寻找此.pc 文件。
  
3. 环境变量PKG_CONFIG_PATH
环境变量PKG_CONFIG_PATH用来设置.pc文件的搜索路径,pkg-config按照设置路径的先后顺序进行搜索,直到找到指定的.pc 文件为止。所以,库的头文件的搜索实际上就变成了对.pc文件搜索。
在安装完一个库后,一是要将相应的.pc文件,拷贝到/usr/lib/pkgconfig目录下;二是要通过设置环境变量PKG_CONFIG_PATH添加.pc文件的搜索路径。

添加环境变量PKG_CONFIG_PATH,在bash中应该如下设置并使之生效:如,

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

另外,环境变量的设置只对当前的终端窗口有效。没有进行上述设置的终端窗口,pkg-config将找不到新安装的.pc文件,可能使后面的安装无法进行。所以,当系统重新启动、或者新开一个终端窗口,如果想使用新安装的库,需要如上面那样重新设置PKG_CONFIG_PATH。
为避免使用库之前上述设置的麻烦,可以把上面环境变量的设置在系统的配置文件或用户配置文件中(如 ~/.bashrc) 。库的搜索路径也可以设置在 /etc/ld.so.conf 文件中,这种设置在系统启动时会生效。如本文OpenCV案例中,在/etc/bash.bashrc文件中增加下列代码:

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

然后,使修改生效,

source /etc/bash.bashrc

4. 运行时库的连接
库文件在连接(静态库和共享库)和运行(仅限于使用共享库的程序)时,其搜索路径是在系统中进行设置的。一般Linux系统把/lib和/usr/lib两个目录作为默认的库搜索路径,所以使用这两个目录中的库不需要设置搜索路径即可直接使用。对于默认库搜索路径之外的库,需要将库的位置添加到库的搜索路径之中。有两种方式,可任选其一:
  <1> 在环境变量 LD_LIBRARY_PATH 中指明库的搜索路径。
  <2> 在/etc/ld.so.conf 文件中添加库的搜索路径。

将可能存放库文件的路径都加入到/etc/ld.so.conf中是明智的选择 。添加方法也极其简单,将库文件的绝对路径直接写进去就OK了,一行一个。
在/etc/ld.so.conf.d/opencv4.conf文件后加上一行 代码:/usr/loacal/lib。

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

并使配置生效,

sudo ldconfig

文献

[1]: Install OpenCV-Python in Ubuntu
[2]: 在Ubuntu18.04中安装OpenCV4.5
[3]: Ubuntu安装opencv4.5
[4]: 简述configure、pkg-config、pkg_config_path三者的关系

你可能感兴趣的:(Ubuntu,opencv)