安装opencv和opencv_contrib (C++接口、Ubuntu 18.04)

系统 Ubuntu 18.04
OpenCV 版本 4.5.1
接口类型 C++

文章目录

    • 1. 安装依赖
    • 2. 下载并解压opencv-4.5.1opencv_contrib-4.5.1
    • 3. opencv_contrib缺少的文件
    • 4. 开始安装
    • 5. 测试
    • 6. 安装失败
    • 7. 参考资料


1. 安装依赖

sudo apt-get update
sudo apt-get install cmake
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config

2. 下载并解压opencv-4.5.1opencv_contrib-4.5.1

  1. opencv-4.5.1.zip
    opencv_contrib-4.5.1.zip
    若要安装Python接口的,则下载opencv-python和对应版本的opencv_contrib

  2. 下载完成后两个都解压,把opencv_contrib放到opencv中,opencv可以放到任意位置

opencv-4.5.1
     ├── opencv_contrib-4.5.1

3. opencv_contrib缺少的文件

点我下载,提取码: q7n7

  1. 把下载后的文件拷贝到/opencv-4.5.1/opencv_contrib-4.5.1/modules/xfeatures2d/src/文件夹中
  2. opencv-4.5.1/modules/features2d/test/下列的文件拷贝到opencv_contrib-4.5.1/modules/xfeatures2d/test/

test_descriptors_invariance.impl.hpp
test_descriptors_regression.impl.hpp
test_detectors_invariance.impl.hpp
test_detectors_regression.impl.hpp
test_invariance_utils.hpp

  1. 打开文件opencv_contrib-4.5.1/modules/xfeatures2d/test/test_features2d.cpp
#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"
  1. 打开文件opencv_contrib-4.5.1/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp
#include "features2d/test/test_detectors_invariance.impl.hpp" 
#include "features2d/test/test_descriptors_invariance.impl.hpp"

改为

#include "test_detectors_invariance.impl.hpp"
#include "test_descriptors_invariance.impl.hpp"

4. 开始安装

  1. 打开终端,切换到opencv-4.5.1文件夹
cd opencv-4.5.1/
  1. 新建文件夹release并进入
mkdir release && cd release
  1. 注意: 这里要替换为你的opencv_contri/modules所在的位置
    不要漏掉后面两个点
    核对opencv的版本
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. -D OPENCV_EXTRA_MODULES_PATH=/home/电脑的用户名/opencv-4.5.1/opencv_contrib-4.5.1/modules ..
  1. cmake后make(可能需要几十分钟)
make
sudo make install

5. 测试

  1. 测试01
cd /home/czt/opencv-4.5.1/samples/cpp/example_cmake
cmake .
make
./opencv_example

显示Hello OpenCV字样,则表示成功。

  1. 测试02
mkdir DisplayImage && cd DisplayImage
gedit DisplayImage.cpp

DisplayImage.cpp

#include 
#include 
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out \n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}
touch CMakeLists.txt
gedit CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
cmake .
make

找一张图片放到DisplayImage

./DisplayImage 图片名称.后缀

如:

./DisplayImage girl.jpg

6. 安装失败

可能相应的依赖库未安装,或者opencv_contrib缺失文件,参考其他作者的博客。

7. 参考资料

  1. 学习OpenCV 3(书籍)
  2. 安装OpenCV时提示缺少boostdesc_bgm.i文件的问题解决方案
  3. 树莓派2020-02-13-raspbian-buster系统安装opencv4.2.0

你可能感兴趣的:(OpenCV,Ubuntu,opencv,ubuntu,c++)