L2 / 知能システムツール演習a(2017年度)第5回

Back to the index. 2017/06/23b,18:47

Step L2:Run c++ samples

You need to set environment variable as this configuration is not ordinary.
Sample codes can be found at
http://docs.opencv.org/3.2.0/
or simply
/tmp/iit/usr/share/OpenCV/samples/cpp

http://docs.opencv.org/3.2.0/
Click: OpenCV Tutorials
Click: Instruction to OpenCV
Click: Using OpenCV with gcc and CMake
http://docs.opencv.org/3.2.0/db/df5/tutorial_linux_gcc_cmake.html

  1. Playing around samples
    Original samples are found at
    file:///tmp/iit/work/opencv/samples/cpp
    And they are copied on installation procedure to
    file:///tmp/iit/usr/share/OpenCV/samples/cpp

    The compiled executables that you made are found at
    file:///tmp/iit/work/opencv/build-2017mdd--hhss/bin
    You should aware that the name of the executables are with "cpp-example-" prefix.

    To enjoy sample programs, you should run them at the source tree.
    (As the data files are there.)
    Terminal: Shell
    cd /tmp/iit/work/openv/build-2017mmdd-hhss/bin
    ls 
    ls cpp-examples-*
    ls ../../samples/cpp/
    
    cd ../../samples/cpp
    ls ../../build-2017mmdd-hhss/bin
    
    # Ready?
    # Try one by one, some works well, some are not.
    # Without options, they may show help.
    # (if you bring a USB camera, you should have more fun!)
    
    # for example,
    less squares.cpp
    # and run 
    ../../build-2017mmdd-hhss/bin/cpp-examples-squares
    
    

  2. Compile by Cmake
    Let's do that in same way in
    http://docs.opencv.org/3.2.0/db/df5/tutorial_linux_gcc_cmake.html
    The only difference is to add one line in CMakeLists.txt
    CMakeLists.txt
    cmake_minimum_required(VERSION 2.8)
    project( IIT-DisplayImage )
    # One of two lines is needed
    set( CMAKE_PREFIX_PATH "/tmp/iit/usr/share" )
    set( OpenCV_DIR "/tmp/iit/usr/share/OpenCV" )
    find_package( OpenCV REQUIRED )
    include_directories( ${OpenCV_INCLUDE_DIRS} )
    add_executable( DisplayImage DisplayImage.cpp )
    target_link_libraries( DisplayImage ${OpenCV_LIBS} )
    
    If you have more than one targets, you can copy and edit the last two lines for each new target.

    Terminal: Shell
    cd 
    cd iit-tool-a
    mkdir 05
    cd    05
    mkdir DisplayImage
    cd    DisplayImage
    
    # write a program (or just download it)
    gedit DisplayImage.cpp
    
    # prepare CMakeLists.txt
    gedit CMakeLists.txt
    
    # down one step
    mkdir build-0
    cd    build-0
    cmake ../
    make
    
    # check 1
    ls -l
    # check 2: what is "ldd"?
    ldd DisplayImage
    
    # prepare data
    cp /tmp/iit/work/opencv/samples/data/lena.jpg ..
    
    # run
    ./DisplayImage ../lena.jpg
    

  3. Compile by Hand
    Of course you can compile the source manually.
    Terminal: Shell
    cd 
    cd iit-tool-a/05/DisplayImage
    
    # pkg-config, a tool to know proper compile options
    # (only once for a shell)
    PKG_CONFIG_PATH=/tmp/iit/usr/lib/pkgconfig
    export PKG_CONFIG_PATH
    
    # On compile
    pkg-config opencv --cflags
    
    # On link
    pkg-config opencv --libs
    
    # For simple file
    pkg-config opencv --cflags --libs
    
    # So, fully
    g++ -Wall DisplayImage.cpp \
    -I/tmp/iit/usr/include/opencv \
    -I/tmp/iit/usr/include \
    -L/tmp/iit/usr/lib \
    -lopencv_stitching -lopencv_superres \
    -lopencv_videostab -lopencv_aruco \
    -lopencv_bgsegm -lopencv_bioinspired \
    -lopencv_ccalib -lopencv_dpm \
    -lopencv_freetype -lopencv_fuzzy \
    -lopencv_hdf -lopencv_line_descriptor \
    -lopencv_optflow -lopencv_reg \
    -lopencv_saliency -lopencv_stereo \
    -lopencv_structured_light \
    -lopencv_phase_unwrapping -lopencv_rgbd \
    -lopencv_surface_matching -lopencv_tracking \
    -lopencv_datasets -lopencv_text \
    -lopencv_face -lopencv_plot \
    -lopencv_dnn -lopencv_xfeatures2d \
    -lopencv_shape -lopencv_video \
    -lopencv_ximgproc -lopencv_calib3d \
    -lopencv_features2d -lopencv_flann \
    -lopencv_xobjdetect -lopencv_objdetect \
    -lopencv_ml -lopencv_xphoto \
    -lopencv_highgui -lopencv_videoio \
    -lopencv_imgcodecs -lopencv_photo \
    -lopencv_imgproc -lopencv_core
    
    # apparently, most of them are not needed...
    
    # In bash style, same procedure is like
    g++ -Wall DisplayImage.cpp `pkg-config opencv --cflags` `pkg-config opencv --libs`
    
    # Or in shorter form, 
    g++ -Wall DisplayImage.cpp `pkg-config opencv --cflags --libs`
    
    # But you cannot run this time (error)
    ./a.out ./lena.jpg
    
    # because some dynamic libraries cannot be found
    ldd ./a.out
    
    # You youself have to tell where the OpenCV dynaic libraries are
    # (only once for a shell)
    LD_LIBRARY_PATH=/tmp/iit/usr/lib
    export LD_LIBRARY_PATH
    
    # and see all fine
    ldd ./a.out
    
    # Go! 
    ./a.out lena.jpg
    

  4. Let's try
    Find some cpp files from "samples" directoy.

kameda[at]iit.tsukuba.ac.jp