Installing on Ubuntu 14
We need the following packages:
$ sudo apt-get update $ sudo apt-get install libopencv-dev build-essential checkinstall cmake pkg-config yasm libtiff4-dev libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg
$ mkdir OpenCV $ cd OpenCV $ git clone https://github.com/Itseez/opencv.git
$ cd opencv $ mkdir release $ cd release $ cmake -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 -D WITH_OPENGL=ON .. ... Build files have been written to: ~/OpenCV/opencv/release
We may want to check if the output of cmake includes the following
FFMPEG: YES GStreamer: YES V4L/V4L2: Using libv4l (ver 0.8.9)
Now, let's make and install it:
$ make $ sudo make install
Then, we want to configure the system wide library search path. In the standard Ubuntu install, the opencv.conf file does not exist, so we need to create it. Let's open /etc/ld.so.conf.d/opencv.conf, then type in /usr/local/lib
$ sudo ldconfig
Append the following two lines to the file, /etc/bash.bashrc. We need to adjust the PKG_CONFIG_PATH environment variable since OpenCV libraries are in a nonstandard (/usr/local/lib) prefix so pkg-config can find them:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
I made my project directory ~/OpenCV/workspace/Display_image, and here is the source file, display_image.cpp, OpenCV's Hello World code:
#include <stdio.h> #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui_c.h> using namespace cv; int main( int argc, char** argv ) { Mat image; image = imread( argv[1], 1 ); if( argc != 2 || !image.data ) { printf( "No image data \n" ); return -1; } namedWindow( "Display Image", CV_WINDOW_AUTOSIZE ); imshow( "Display Image", image ); waitKey(0); return 0; }
Now we need to make our CMakeLists.txt file in "~/OpenCV/workspace/DisplayImage" and it looks like this:
cmake_minimum_required(VERSION 2.8) project( DisplayImage ) find_package( OpenCV REQUIRED ) add_executable( DisplayImage display_image.cpp ) target_link_libraries( DisplayImage ${OpenCV_LIBS} )
Let's look at what files do we have now:
$ cd ~/OpenCV/workspace/Display_image $ ls display_image.cpp lena.png CMakeLists.txt
Then, run cmake:
$ cmake . -- Configuring done -- Generating done -- Build files have been written to: /home/khong/OpenCV/workspace/DisplayImage $ ls CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt display_image.cpp lena.png Makefile $ make $ ls CMakeCache.txt cmake_install.cmake DisplayImage lena.png CMakeFiles CMakeLists.txt display_image.cpp Makefile $ ./DisplayImage lena.png init done
Now, we have a window that with an image:
By the way, what version of OpenCV am I running?
$ pkg-config --modversion opencv 3.0.0
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization