OpenCV学习笔记(1)-----OpenCV 3.1.0环境安装

时间:2022-12-27 08:20:37

    本文记录了基于Ubuntu Desktop 16.04 安装opencv 3.1.0的过程。


1 按照OpwnCV官网介绍,按照在Ubuntu环境按照你黄OpenCV需要如下一些安装包:

  • GCC 4.4.x or later
  • CMake 2.6 or higher
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkg-config
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [optional] libtbb2 libtbb-dev
  • [optional] libdc1394 2.x
  • [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev

2 安装opencv依赖的package

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

3 下载opencv源码包并编译安装

3.1 下载地址

3.2 编译和安装OpenCV

    1)解压下载的opencv源码包opencv-3.1.0.zip

unzip opencv-3.1.0.zip 
    2)创建release目录

在opencv-3.1.0目录下创建release目录(mkdir release) 
   3)编译opencv

在opencv-3.1.0目录下创建release目录(mkdir release)
   4)配置opencv

    在release目录下执行如下命令:

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
    5)编译

    在release目录下执行如下命令:

make
    6)安装

    在release目录下执行如下命令:

sudo make install


4 验证安装环境

    简单读取一张图片并显示:


1)主程序

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;

int main( int argc, char** argv )
{
    
    if( argc != 2 )
    {
        printf( "command error\n " );
        return -1;
    }
    char* imagefile = argv[1];

    Mat image;
    image = imread( imagefile, 1 );

    if(  !image.data)
    {
        printf( "No image found\n " );
        return -1;
    }

    namedWindow( imagefile, CV_WINDOW_AUTOSIZE );
    imshow( imagefile, image );

    waitKey(0);

    return 0;
}

2)CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(test)
add_executable(test test.cpp)
find_package(OpenCV REQUIRED)
target_link_libraries(test ${OpenCV_LIBS})

3)运行并显示结果


./test ../../test.JPG


OpenCV学习笔记(1)-----OpenCV 3.1.0环境安装