环境
ubuntu
cmake
cmake-gui
opencv-3.3.1 下载地址:https://opencv.org/releases.html
opencv_contrib-3.3.1 下载地址:https://github.com/opencv/opencv_contrib/releases

准备工作

  • 更新系统
    sudo apt-get update
    sudo apt-get upgrade
    
  • 安装cmake及必要依赖
    sudo apt-get install build-essential
    sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
    sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
    
  • 安装cmake-gui
    sudo apt-get install cmake-qt-gui
    
  • 解压opencv以及opecv_contrib至同一目录
  • opencv目录内新建build目录

    build目录内打开终端运行cmake-gui
    cmake-gui
    

编译 / 安装

  • 配置源码目录以及编译目录

  • 执行Configure
    Unix Makefiles - > Use default native compilers(默认) - > Finish

  • 配置编译属性
    CMAKE_BUILD_TYPE:RELEASE
    CMAKE_INSTALL_PREFIX:/home/jiaopan/app/opencv #配置安装目录 可默认

    OPENCV_EXTRA_MODULES_PATH:/home/jiaopan/app/opencv3/opencv_contrib-3.3.1/modules #opencv_contrib的modules目录

  • 执行Generate 生成配置文件

  • build目录内打开终端 执行make命令开始编译 ,100%时编译成功

  • 编译成功,执行make install 命令进行安装

  • 编译时问题

    #1.问题1
    collect2: error: ld returned 1 exit status
    modules/core/CMakeFiles/opencv_perf_core.dir/build.make:882: recipe for target 'bin/opencv_perf_core' failed
    make[2]: *** [bin/opencv_perf_core] Error 1
    CMakeFiles/Makefile2:2627: recipe for target 'modules/core/CMakeFiles/opencv_perf_core.dir/all' failed
    make[1]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/all] Error 2
    Makefile:160: recipe for target 'all' failed
    make: *** [all] Error 2
    #解决:缺少ffmpeg
    #安装ffmpeg
    sudo add-apt-repository ppa:djcj/hybrid  
    sudo apt-get update  
    sudo apt-get install ffmpeg
    
  • 命令编译和安装

sudo rpm -import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
sudo yum install -y epel-release
sudo yum install -y cmake pkgconfig gtk2-devel gtk3-devel libjpeg-devel libpng-devel libtiff-devel tbb-devel eigen3-devel tesseract-devel openblas-devel 
sudo yum install ffmpeg ffmpeg-devel
sudo yum install python-devel python3-devel
cd build
sudo cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_FFMPEG=ON ..
sudo make -j8
sudo make install

配置环境变量

sudo vim /etc/ld.so.conf.d/opencv.conf #添加 /home/jiaopan/app/opencv/lib内容 ,即opencv安装目录下的lib目录路径
sudo ldconfig #更新动态链接库
sudo vim /etc/bash.bashrc 
#末尾添加如下内容
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:opencv安装目录/lib/pkgconfig 
export PKG_CONFIG_PATH
#-----------------------------------------
source /etc/profile #更新环境变量 
pkg-config --cflags --libs opencv #测试是否安装成功

安装成功:

代码测试

  • cmake 配置
cmake_minimum_required(VERSION 3.15)
project(test)

set(CMAKE_CXX_STANDARD 14)

find_package(OpenCV REQUIRED)
include_directories(include ${OpenCV_INCLUDE_DIRS})
add_executable(test main.cpp)

target_link_libraries(test ${OpenCV_LIBS})
#include<iostream>
#include <opencv2/opencv.hpp>
int main(){
    // 读入一张图片
    cv::Mat image=cv::imread("dog.jpg");
    // 在窗口中显示
    cv::imshow("test",image);
    // 等待6000 ms后窗口自动关闭
    cv::waitKey(6000);
    return 0;
}



opencv3      opencv3

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!