Install opencv on linux

  • If you don’t care about getting the cutting-edge version of opencv, you can do following:

    1
    2
    3
    4
    $ sudo apt-get install build-essential
    $ sudo apt-get install libavformat-dev
    $ sudo apt-get install ffmpeg
    $ sudo apt-get install libcv2.1 libcvaux2.1 libhighgui2.1 python-opencv opencv-doc libcv-dev libcvaux-dev libhighgui-dev

    To compile examples:

    1
    2
    3
    $ cd /usr/share/doc/opencv-doc/examples/c
    $ sudo su
    $ sh build_all.sh
  • If you’re geek enough to pursue newest version of everything like me, you may have to spend a little more time on this:
    To check out code from opencv svn repository: (you need to install svn first)

    1
    2
    3
    4
    5
    $ cd ~/<my_working_directory>
    $ svn co http://code.opencv.org/svn/opencv/trunk/opencv
    #or
    $ svn co http://code.opencv.org/svn/opencv/trunk/
    #to get opencv and opencv_extra (rather huge)

    To build libraries, you need to install cmake/cmake-gui first:

    1
    2
    3
    $ sudo apt-get install cmake
    #and
    $ sudo apt-get install cmake-gui

    Then we can build libraries using cmake:

    1
    2
    3
    4
    $ cd ~/<opencv source dir> #where you just check out source codes
    $ mkdir release
    $ cd release
    $ cmake-gui

    In cmake-gui,  check whatever you need(if you are not sure, you can just leave it default), click configure. After “Configuring done”, click generate. If you see “Generating done”, you are good to do next step:

    1
    2
    3
    $ cd ~/<opencv source dir>/release
    $ make
    $ sudo make install

    Wait until it’s done.
    After you build opencv libraries, we’re ready to use them:
    Let’s begin with a hello world test program named helloWorld.cpp:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #include <cv.h>
    #include <highgui.h>
    int main ( int argc, char **argv )
    {
    cvNamedWindow( "My Window", 1 );
    IplImage *img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );
    CvFont font;
    double hScale = 1.0;
    double vScale = 1.0;
    int lineWidth = 1;
    cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,
    hScale, vScale, 0, lineWidth );
    cvPutText( img, "Hello World!", cvPoint( 200, 400 ), &font,
    cvScalar( 255, 255, 0 ) );
    cvShowImage( "My Window", img );
    cvWaitKey();
    return 0;
    }

    Then we create a CMakeLists.txt in the same directory as follows:

    1
    2
    3
    4
    5
    cmake_minimum_required (VERSION 2.6)
    project (Test)
    FIND_PACKAGE( OpenCV REQUIRED )
    ADD_EXECUTABLE( helloWorld helloWorld.cpp )
    TARGET_LINK_LIBRARIES( helloWorld ${OpenCV_LIBS} )

    Then use cmake to generate makefile and use make to generate executable:

    1
    2
    3
    4
    5
    6
    7
    8
    $ cmake .
    # you will see
    # Configuring done
    # Generating done
    $ make
    #you will see
    #[100%] Building CXX object CMakeFiles/helloWorld.dir/helloWorld.cpp.o
    #[100%] Built target helloWorld

    And you are done! Run it and you’ll see a hello world window.

    1
    $ ./helloWorld
  • If you want to use opencv libraries in Eclipse, right click your project and select properties. In c/c++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Includes, add your opencv include dir into Include paths (-I). Then in  GCC C++ Linker -> Libraries, add libraries(such as opencv_core, opencv_highgui, your library names may var) to Libraries (-l) and your libraries dir to Library search path (-L).

    BTW, you can use these commands to locate your include files and libraries:

    1
    2
    3
    4
    5
    6
    $ pkg-config --cflags opencv
    # it will give sth like
    # -I/where/you/have/installed/opencv/include/opencv
    $ pkg-config --libs opencv
    # it will give sth like
    # -L/where/you/have/installed/opencv/lib -lcxcore -lcv -lhighgui -lcvaux

Reference:
OpenCV - Ubuntu Doc
OpenCV install guide