Installing OpenCV 2.4.13 on Ubuntu 16.04

This post is (for the most part) note to self. Please follow with caution.

Optional Pre-Clean Up

If created with make install then make uninstall
Else if created with checkinstall then dpkg -r release
Else sudo apt-get purge libopencv* && sudo apt-get autoremove

Additional files to remove:
sudo rm -rf /usr/local/include/opencv/
sudo rm -rf /usr/local/include/opencv2/
sudo rm -rf /usr/local/share/OpenCV/
sudo find /var/cache/apt/archives/ -name \*opencv\* -exec rm -rf {} \;
sudo find /usr/local/lib/ -name \*opencv\* -exec rm -rf {} \;
sudo find /usr/local/bin/ -name \*opencv\* -exec rm -rf {} \;


Install Script

# source: http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html

# install dependencies
sudo apt-get update
sudo apt-get install -y build-essential checkinstall cmake git libopencv-dev libgtk2.0-dev pkg-config libavcodec-dev libpng12-dev libavformat-dev libswscale-dev yasm libxine2 libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libqt4-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils

# optional packages
sudo apt-get install -y python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev

# download opencv-2.4.13
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.13/opencv-2.4.13.zip
unzip opencv-2.4.13.zip && cd opencv-2.4.13
mkdir release && cd release

# compile and install
cmake -G "Unix Makefiles" -D CMAKE_CXX_COMPILER=/usr/bin/g++ -D CMAKE_C_COMPILER=/usr/bin/gcc -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -DWITH_OPENGL=ON -D BUILD_FAT_JAVA_LIB=ON -D INSTALL_TO_MANGLED_PATHS=ON -D INSTALL_CREATE_DISTRIB=ON -D INSTALL_TESTS=ON -D ENABLE_FAST_MATH=ON -D WITH_IMAGEIO=ON -D BUILD_SHARED_LIBS=OFF -D WITH_GSTREAMER=ON ..
make all -j2
sudo make install && sudo checkinstall

Sanity Check

~$ pkg-config --cflags opencv
-I/usr/local/include/opencv -I/usr/local/include

~$ pkg-config --libs opencv
-L/usr/local/lib -lopencv_contrib -lopencv_stitching -lopencv_nonfree -lopencv_superres -lopencv_ocl -lopencv_ts -lopencv_videostab -lopencv_gpu -lopencv_photo -lopencv_objdetect -lopencv_legacy -lopencv_video -lopencv_ml -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_imgproc -lopencv_flann -lopencv_core -lQtCore -lQtTest -lQtGui -lQtOpenGL -lswscale-ffmpeg -lavutil-ffmpeg -lavformat-ffmpeg -lavcodec-ffmpeg -lv4l2 -lv4l1 -ldc1394 -lgstpbutils-0.10 -lgstriff-0.10 -lgstapp-0.10 -lgstvideo-0.10 -lxml2 -lglib-2.0 -lgthread-2.0 -lgmodule-2.0 -lgobject-2.0 -lgstreamer-0.10 -lgstbase-0.10 -lIlmThread -lHalf -lIex -lIlmImf -lImath -ljasper -ltiff -lpng -ljpeg -latomic -ltbb -lGL -lGLU -lrt -lpthread -lm -ldl -lstdc++ -lz

~$ whereis opencv
opencv: /usr/include/opencv /usr/share/opencv

~$ whereis opencv2
opencv2: /usr/include/opencv2

Run Sample OpenCV program

Save code below as DisplayImage.cpp

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\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;
}



~$ sudo g++ DisplayImage.cpp -o DisplayImage `pkg-config --cflags --libs opencv`
~$ ./DisplayImage [path-to-image]

Compile using CMake

Create CMakeLists.txt on the same level as the main source file with the following code:

cmake_minimum_required( VERSION 2.8 )
project( DisplayImage )
set( OpenCV_DIR "/usr/share/OpenCV/" )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Create a build folder on the same level as the other two files. You can name it however you want. cd into the folder and cmake .. then make.

~$ mkdir build && cd build
~/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/build
~/build$ make
Scanning dependencies of target DisplayImage
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[100%] Linking CXX executable DisplayImage
[100%] Built target DisplayImage

Overall project structure looks like below:

~$ tree -L 2
.
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── cmake_install.cmake
│   ├── DisplayImage
│   └── Makefile
├── CMakeLists.txt
└── DisplayImage.cpp

Qt Creator integration

Install Qt 5.7
File > New file or project
Choose Application > Qt Console Application

Append these to the .pro files.

INCLUDEPATH += .
INCLUDEPATH += /usr/local/include
INCLUDEPATH += /usr/local/include/opencv
INCLUDEPATH += /usr/local/include/opencv2
INCLUDEPATH += /usr/local/include/opencv2/calib3d
INCLUDEPATH += /usr/local/include/opencv2/contrib
INCLUDEPATH += /usr/local/include/opencv2/core
INCLUDEPATH += /usr/local/include/opencv2/features2d
INCLUDEPATH += /usr/local/include/opencv2/flann
INCLUDEPATH += /usr/local/include/opencv2/gpu
INCLUDEPATH += /usr/local/include/opencv2/highgui
INCLUDEPATH += /usr/local/include/opencv2/imgproc
INCLUDEPATH += /usr/local/include/opencv2/legacy
INCLUDEPATH += /usr/local/include/opencv2/ml
INCLUDEPATH += /usr/local/include/opencv2/nonfree
INCLUDEPATH += /usr/local/include/opencv2/objdetect
INCLUDEPATH += /usr/local/include/opencv2/ocl
INCLUDEPATH += /usr/local/include/opencv2/photo
INCLUDEPATH += /usr/local/include/opencv2/stitching
INCLUDEPATH += /usr/local/include/opencv2/superres
INCLUDEPATH += /usr/local/include/opencv2/ts
INCLUDEPATH += /usr/local/include/opencv2/video
INCLUDEPATH += /usr/local/include/opencv2/videostab
LIBS += `pkg-config opencv --cflags --libs`

Paste this slightly modified code to .cpp file.

#include <QCoreApplication>
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char** argv )
{
    Mat image;
    image = imread("/path/to/your/image/file");

    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);

    waitKey(0);

    return 0;
}

Build and Run.

Comments

posted @ 2018-01-21 17:52  Roger_Ceng  阅读(469)  评论(0编辑  收藏  举报