Ubuntu 18.04 LTS 安装OpenCV (C/C++)

Ubuntu 18.04 LTS 安装OpenCV (C/C++)

前言

OpenCV提供开源图像算法和机器学习算法,是计算机视觉中使用极广的图像处理算法库。OpenCV最早由Intel创建,OpenCV实际上表示Intel® Open Source Computer Vision Library,之后由Willow Garage(2014年关闭)和Itseez(2016年被Intel收购)支持和维护。OpenCV可以应用于各个平台,如Mac, Windows, Linux (Terminal environment)。本文主要介绍如何在Ubuntu系统下,从源代码编译安装OpenCV。

第一步,更新系统

sudo apt-get update
sudo apt-get upgrade

第二步,安装依赖项

依赖项,视情况而定。事实上,OpenCV的依赖项很多,缺少某些依赖项会影响OpenCV部分功能。OpenCV会在make阶段检测依赖项是否会安装,并自我调整功能。

$ sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev 
$ sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev

某依赖项安装有问题时,请自行搜索解决。如依赖包,libjasper-dev

sudo apt-get install libjaster-dev
errorE: unable to locate libjasper-dev

解决办法,复制到这里

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

第三步,下载OpenCV

OpenCV除了主库外还有一个扩展模块,叫opencv-contrib,可以视情况选择安装与否。需要注意的是,这两个库(模块)的版本号要统一,如opencv-3.4.5和opencv_contrib-3.4.5。可以选择自行登录官网下载;也可到github下载,opencv的releases,opencv_contrib的releases;也可以使用git下载,代码如下。

sudo su # change to root
cd /opt # 3rd party software install dir, not necessary
git clone https://github.com/opencv/opencv.git
cd opencv 
git checkout 3.4.5
cd ..

git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.4.5
cd ..

第四步,编译和安装OpenCV

进入第三步下载的或者解压的opencv文件夹,利用cmake生成makefile,当然有很多的选择项设置,具体可以查看cmakelist文件中的帮助信息,对于我们来说这些就足够了,其实默认也可以,即直接使用如下选择项。

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
/opt$ cd opencv
/opt/opencv$ mkdir release
/opt/opencv$ cd release
/opt/opencv/release$ cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF -D ENABLE_AVX=OFF -D WITH_OPENGL=OFF -D WITH_OPENCL=OFF -D WITH_IPP=OFF -D WITH_TBB=ON -D BUILD_TBB=ON -D WITH_EIGEN=OFF -D WITH_V4L=OFF -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules /opt/opencv/
/opt/opencv/release$ make -j4 # multi task
/opt/opencv/release$ make install
/opt/opencv/release$ ldconfig
/opt/opencv/release$ exit # exit root
/opt/opencv/release$ cd ~

如果不修改CMakeLists文件,则Opencv默认的安装位置

/usr/local/include/opencv2/  #新版Opencv核心头文件
/usr/local/include/opencv/   #旧Opencv核心头文件
/usr/local/share/OpenCV/     #一些Opencv其他安装信息
/usr/local/lib/              #Opencv中的动态链接库存放位置

第五步,检测安装

pkg-config --modversion opencv
3.4.5

第六步,测试

需要在有一个测试图片“test.jpg”,图片的格式可以是jpg,png,bmp等,取决于第二步依赖项所支持的格式。

#include 
#include 
int main( int argc, char** argv ) {
    cv::Mat image;
    image = cv::imread("test.jpg" , CV_LOAD_IMAGE_COLOR);	      
    if(! image.data ) {
        std::cout <<  "Could not open or find the image" << std::endl ;
	    return -1;
    }

    std::cout << "image wide: "<< image.cols << ",image high: " << image.rows << ",image channels: "<< image.channels() << std::endl;
    /* display image
    cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
    cv::imshow( "Display window", image );		    
    cv::waitKey(0);
    */
    size_t y,x;// y is row, x is col
    int c;     // c is channel
    y = x = 250;
    c = 2;
    // row_ptr is the head point of y row
    unsigned char *row_ptr = image.ptr<unsigned char>(y);
    // data_ptr points to pixel data
    unsigned char *data_ptr = &row_ptr[x * image.channels()]; 
    unsigned char data =  data_ptr[c];

    // use cv::Mat::at() to get the pixel value
    // unsigned char is not printable
    // std::cout << std::isprint(data)<(y,x)[c]) << std::endl;
    std::cout << "pixel value at y, x ,c"<<static_cast<unsigned>(image.at<cv::Vec3b>(y,x)[c]) << std::endl;
    return 0;
}

编译运行

g++ main.cpp -o output `pkg-config --cflags --libs opencv`
./output

Reference

  1. How to Install OpenCV in Ubuntu 16.04 LTS for C / C++
  2. Ubuntu下源码安装Opencv完全指南
  3. Install OpenCV3 on Ubuntu
  4. 高翔, 张涛, 颜沁睿, 刘毅, 视觉SLAM十四讲:从理论到实践, 电子工业出版社, 2017

你可能感兴趣的:(Ubuntu 18.04 LTS 安装OpenCV (C/C++))