Ubuntu下安装OpenCV的相关记录

1、安装OpenCV所需要的库

  • 1 sudo apt-get install build-essential
  • 2 sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  • 3 sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

2、从官网下载OpenCV源码

http://sourceforge.net/projects/opencvlibrary/


3、编译OpenCV

  • 1 使用unzip命令解压
  • 2 创建编译目录,编译
    cd ~/opencv-3.0.0-rc1
    mkdir release
    cd release
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
    make
    sudo make install

4、测试OpenCV

  • 1 创建工作目录
    mkdir ~/opencv-lena
    cd ~/opencv-lena
    gedit DisplayImage.cpp
  • 2 写入测试代码
    #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;
    }
  • 3 创建CMake编译文件
    gedit CMakeLists.txt
    写入如下代码
    cmake_minimum_required(VERSION 2.8)
    project( DisplayImage )
    find_package( OpenCV REQUIRED )
    add_executable( DisplayImage DisplayImage.cpp )
    target_link_libraries( DisplayImage ${OpenCV_LIBS} )
  • 4 编译
    首先进到文件目录,然后输入
    cmake .
    make
  • 5 运行
    如果编译成功,文件目录下已经生成可执行性文件,记得图片要放在文件夹下(或指明路径),然后运行
    ./DisplayImage lena.jpg

你可能感兴趣的:(Ubuntu下安装OpenCV的相关记录)