OPENCV installation in Linux and Hello World

Source: Internet
Author: User
Tags git client

Http://opencv.org/quickstart.htmlInstallation in Linux

These steps has been tested for Ubuntu 10.04 and should work with other distros as well.

Required Packages
    • GCC 4.4.x or later
    • CMake 2.8.7 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

The packages can installed using a terminal and the following commands or by using Synaptic Manager:

[Compiler] sudo apt-get install build-essential[required] sudo apt-get install cmake git libgtk2.0-dev pkg-co Nfig Libavcodec-dev libavformat-dev libswscale-dev[optional] sudo apt-get install Python-dev python-numpy Libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev    
Getting OpenCV Source Code

You can use the latest stable OpenCV version or you can grab the latest snapshot from our Git repository.

Getting the Latest Stable OpenCV Version
    • Go to our downloads page.
    • Download the source archive and unpack it.
Getting the cutting-edge OpenCV from the Git Repository

Launch Git client and Clone OpenCV repository. If you need modules from OpenCV contrib repository then clone it too.

For example

CD ~/<my_working_directory>git clone Https://github.com/Itseez/opencv.gitgit clone https://github.com/Itseez/ Opencv_contrib.git
Building OpenCV from Source Using CMake
  1. Create a temporary directory, which we denote as <cmake_build_dir>, where you want to put the generated makefiles, p roject files as well the object files and output binaries and enter there.

    For example

    CD ~/opencvmkdir buildCD Build
  2. Configuring. Run CMake [<some optional parameters>] <path to the OpenCV source directory>

    For example

    Cmake_build_typecmake_install_prefix=/usr/local. 

    or Cmake-gui

    • Set full path to OpenCV source code, e.g./HOME/USER/OPENCV
    • Set full path to <cmake_build_dir> e.g./home/user/opencv/build
    • Set Optional parameters
    • Run: "Configure"
    • Run: "Generate"
  3. Description of some parameters

    • Build Type:cmake_build_type=release\debug
    • To build with modules from Opencv_contrib set Opencv_extra_modules_path to <path to opencv_contrib/modules/>
    • Set Build_docs for building documents
    • Set Build_examples to BUILD all EXAMPLES
  4. [Optional] Building python. Set the following Python parameters:

    • PYTHON2 (3) _executable = <path to Python>
    • Python_include_dir =/usr/include/python<version>
    • Python_include_dir2 =/usr/include/x86_64-linux-gnu/python<version>
    • Python_library =/usr/lib/x86_64-linux-gnu/libpython<version>.so
    • PYTHON2 (3) _numpy_include_dirs =/usr/lib/python<version>/dist-packages/numpy/core/include/
  5. [Optional] Building java.

    • Unset Parameter:build_shared_libs
    • It is useful also to unset build_examples, build_tests, build_perf_tests-as they all would be statically linked with Open CV and can take a lot of memory.
  6. Build. From build directory execute make, recomend to do it in several threads

    For example

    # runs 7 jobs in parallel
  7. [Optional] Building documents. Enter <cmake_build_dir/doc/> and run make with target "Html_docs"

    For example

    CD ~/OPENCV/BUILD/DOC/MAKE-J7 Html_docs
  8. To install libraries, from build directory Execute

    sudo make install
  9. [Optional] Running Tests

    • Get the required test data from OpenCV extra repository.

    For example

    git clone https://github.com/Itseez/opencv_extra.git
    • Set opencv_test_data_path environment variable to <path to Opencv_extra/testdata>.
    • Execute tests from build directory.

    For example

    <cmake_build_dir>/bin/opencv_test_core

Note

If the size of the created library is a critical issue (as in case of an Android build) you can use the Install/stri P command to get the smallest size as possible. The stripped version appears to be twice as small. However, we do not recommend using the unless those extra megabytes do really matter.

Using OpenCV with GCC and CMake

Note

We assume that you had successfully installed OpenCV in your workstation.

    • The easiest-in-the-OpenCV in your code are to use CMake. A few advantages (taken from the Wiki):
      1. No need to anything when porting between Linux and Windows
      2. Can easily is combined with other tools by CMake (i.e. Qt, ITK and VTK)
    • If you is not a familiar with CMake, checkout the tutorial in its website.
Stepscreate a program using OpenCV

Let's use a simple program such as DisplayImage.cpp shown below.

#include <stdio.h>#include <opencv2/opencv.hpp>UsingNamespaceCv;IntMain(Intargc,Char**Argv){If(argc!=2){Printf("Usage:DisplayImage.out <Image_Path>\ n");Return-1;}MatImage;Image=Imread(Argv[1],1);If(!image. Data ) {printf ( "No image data \n "); return -1} namedwindow ( "Display Image"  span class= "n" >window_autosize ); imshow ( "Display Image" image< Span class= "P" >); waitkey (0return 0;}        
Create a CMake file

Now there is a to create your CMakeLists.txt file. It should look like this:

 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}    
Generate the executable

This was easy, just proceed as with any other project using CMake:

CD <displayimage_directory>cmake. Make
Result

By now you should has an executable (called DisplayImage in this case). You just has to run it giving a image location as an argument, i.e.:

./displayimage lena.jpg

You should get a nice window as the one shown below:

OPENCV installation in Linux and Hello World

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.