OpenCV is an open-source library that supports real-time computer vision. Unlike PCL, however, OpenCV is a 2D imaging library, making OpenCV just as useful to have for machine vision. In the following blog post, we will go through the steps necessary to compile OpenCV 3.0 from source on Ubuntu 14.04 and capture a video through your computer’s own webcam.
Dependencies
OpenCV requires a host of various dependencies to function properly. In order to save time, type in the following commands in a terminal to install the necessary prerequisites:
sudo apt-get update sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
In addition to the required dependencies, OpenCV also has a list of optional dependencies it uses. To install these dependencies, type in the following:
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
Installation
Open up your working directory and use the following commands inside that directory to clone the OpenCV repository via git:
git clone https://github.com/Itseez/opencv.git
Once the cloning has finished, we must build OpenCV through CMake. First, make a new directory called “Release” inside the “opencv” directory and open it:
cd OpenCV && mkdir Release && cd Release
Next, compile with CMake. We tell CMake to build in Release mode fully optimized, no debugging capabilities):
cmake -D CMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local ..
Finally, to install OpenCV:
make sudo make install
Demo
To test your OpenCV, make a new directory named “Video_Capture” anywhere inside your disk. Inside that directory, make a new file called “CMakeLists.txt”. Open it with an editor and paste the following:
cmake_minimum_required(VERSION 2.8) project( VideoCapture ) find_package( OpenCV REQUIRED ) add_executable( VideoCapture VideoCapture.cpp ) target_link_libraries( VideoCapture ${OpenCV_LIBS} )
The first two lines tell CMake that we require a minimum of CMake version 2.8 and declares a new project named “VideoCapture”. Next, we tell CMake to check for the presence of OpenCV. Then, we create a new binary to be compiled from the source file and link it with the OpenCV library.
After that, create a new c++ file called VideoCapture.cpp. Inside that file, paste the following:
#include "opencv2/highgui/highgui.hpp" #include using namespace cv; using namespace std; int main(int argc, char* argv[]) { VideoCapture cap(0); // open the video camera no. 0 if (!cap.isOpened()) // if not success, exit program { cout << "Cannot open the video cam" << endl; return -1; } double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video cout << "Frame size : " << dWidth << " x " << dHeight << endl; namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo" while (1) { Mat frame; bool bSuccess = cap.read(frame); // read a new frame from video if (!bSuccess) //if not success, break loop { cout << "Cannot read a frame from video stream" << endl; break; } imshow("MyVideo", frame); //show the frame in "MyVideo" window if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop { cout << "esc key is pressed by user" << endl; break; } } return 0; }
Save and exit.
To compile your code, open a terminal inside the “Video_Capture” directory and type the following:
cmake . make
By now, you should have an executable called VideoCapture. To run the executable, simply type:
./VideoCapture
A screen should pop up and display a video capture live from your webcam.
Helpful Resources
Here are some additional guides on how to install and run OpenCV:
http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation: This is the official tutorial for installing OpenCV on Linux.
http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html#linux-gcc-usage: This is the official tutorial on running OpenCV files with CMake.
http://opencv-srf.blogspot.com/2011/09/capturing-images-videos.html: This tutorial describes how to capture videos from both file and camera. It also describes the function of the various parts of code we used in the program above.