Install OpenCV 2.4.8 on Ubuntu 13.10

Following are the instructions to install OpenCV on Ubuntu machine. I am using latest version of OpenCV 2.4.8 and latest version of Ubuntu 13.10. For old versions (2.3 and earlier) of OpenCV you can follow my earlier blog post here. Installing new version of OpenCV is slightly different hence I am presenting this post.

Following steps will guide you to install OpenCV 2.4.8 on Ubuntu 13.10

1. First we will install essential tools for buidling OpenCV. Use following command in your terminal to install them.

sudo apt-get install build-essential cmake pkg-config

2. Following command will install required Image I/O libraries.

sudo apt-get install libjpeg62-dev libtiff4-dev libjasper-dev


3. Install gtk libraries for highgui

sudo apt-get install  libgtk2.0-dev

4. Install required video libraries.


sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

5. Download OpenCV from sourceforge using below link and extract zipped file. Enter into zipped directly.


http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.8/


6. Create a build directory in zipped file and enter into it using following terminal command.


mkdir build
cd build


7. Type following command in terminal window to start building process.


cmake -D CMAKE_BUILD_TYPE=RELEASE -D MAKE_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 ..


8.  Compile the source code using following command.


make

9.  Install OpenCV using following command.

sudo make install


Now your machine is ready to rock with OpenCV. 

HSV values of Image pixel using JavaCV


This post will explain how to get HSV color space values of Image pixel using JavaCV.
For JavaCV setup on Windows, follow link below
http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html

If you want to access RGB color space values, then refer to link below
http://opencvlover.blogspot.in/2013/02/rgb-values-of-image-pixel-using-javacv.html

Pixel values in HSV color space are often required in Image processing. Following gist demonstrates how to access and print pixel values in HSVcolor space. We first convert image from RGB color space to HSV color space using 'cvCvtColor()''. We can call 'cvGet2D()' function to access image matrix and use it get pixel values at desired location.

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_highgui.*;
public class getHSV{
public static int x_co;
public static int y_co;
public static void main (String[] args){
final IplImage src = cvLoadImage("resources/test.png");//Images 'test.png' located under resource folder
cvNamedWindow("Image",CV_WINDOW_AUTOSIZE);
final IplImage hsv = cvCreateImage(cvGetSize(src), 8, 3);
cvCvtColor(src, hsv, CV_BGR2HSV);
CvMouseCallback on_mouse = new CvMouseCallback() {
@Override
public void call(int event, int x, int y, int flags,
com.googlecode.javacpp.Pointer param) {
if (event == CV_EVENT_MOUSEMOVE){
x_co = x;
y_co = y;
}
CvScalar s=cvGet2D(hsv,y_co,x_co);
System.out.println( "H:"+ s.val(0) + " S:" + s.val(1) + " V:" + s.val(2));//Print values
}
};
cvSetMouseCallback("Image", on_mouse, null);
cvShowImage("Image", src);
cvWaitKey(0);
}
}
view raw getHSV.java hosted with ❤ by GitHub
This will print HSV values in console. This code comes really handy while using manual threshold.
You can fork me on github for complete project of this code.
https://github.com/nikhil9/getHSV

RGB values of Image pixel using JavaCV

This post will explain how to get RGB color space values of Image pixel using JavaCV.
For JavaCV setup on Windows, follow link below
http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html

If you want to access HSV color space values, then refer to link below
http://opencvlover.blogspot.in/2013/02/hsv-values-of-image-pixel-using-javacv.html

Pixel values in RGB color space are often required in Image processing. Following gist demonstrates how to access and print pixel values in RGB color space. We can call 'cvGet2D()' function to access image matrix and use it get pixel values at desired location.

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_highgui.*;
public class getRGB{
public static int x_co;
public static int y_co;
public static void main (String[] args){
final IplImage src = cvLoadImage("resources/test.png"); //Images 'test.png' located under resource folder
cvNamedWindow("Image",CV_WINDOW_AUTOSIZE);
CvMouseCallback on_mouse = new CvMouseCallback() {
@Override
public void call(int event, int x, int y, int flags,
com.googlecode.javacpp.Pointer param) {
if (event == CV_EVENT_MOUSEMOVE){
x_co = x;
y_co = y;
}
CvScalar s=cvGet2D(src,y_co,x_co);
System.out.println( "B:"+ s.val(0) + " G:" + s.val(1) + " R:" + s.val(2));//Print values
}
};
cvSetMouseCallback("Image", on_mouse, null);
cvShowImage("Image", src);
cvWaitKey(0);
}
}
view raw getRGB.java hosted with ❤ by GitHub

This will print RGB values in console. This code comes really handy while using manual threshold.
You can fork me on github for complete project of this code.
https://github.com/nikhil9/getRGB

Face Detection in JavaCV using haar classifier

OpenCV provides haar like feature detection algorithm which can be used for object detection. Wikipedia page http://en.wikipedia.org/wiki/Haar-like_features provides nice information about what are haar like feature.

OpenCV also provides haar training utility which can be used for training. It generates XML file from training samples which further can be used for fast object detection. Such XML file is provided with opencv package for face detection.

Gist below explains how to use haar classifier in JavaCV. Code loads classifier file haarcascade_frontalface_default.xml and uses cvHaarDetectObjects() to find faces in loaded image. More information about cvHaarDetectObjects() can be found at http://opencv.willowgarage.com/documentation/object_detection.html#haardetectobjects

import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;
public class FaceDetection{
public static final String XML_FILE =
"resources/haarcascade_frontalface_default.xml";
public static void main(String[] args){
IplImage img = cvLoadImage("resources/lena.jpg");
detect(img);
}
public static void detect(IplImage src){
CvHaarClassifierCascade cascade = new
CvHaarClassifierCascade(cvLoad(XML_FILE));
CvMemStorage storage = CvMemStorage.create();
CvSeq sign = cvHaarDetectObjects(
src,
cascade,
storage,
1.5,
3,
CV_HAAR_DO_CANNY_PRUNING);
cvClearMemStorage(storage);
int total_Faces = sign.total();
for(int i = 0; i < total_Faces; i++){
CvRect r = new CvRect(cvGetSeqElem(sign, i));
cvRectangle (
src,
cvPoint(r.x(), r.y()),
cvPoint(r.width() + r.x(), r.height() + r.y()),
CvScalar.RED,
2,
CV_AA,
0);
}
cvShowImage("Result", src);
cvWaitKey(0);
}
}
Result of this code generates window which shows loaded picture and red rectangle around detected faces.


You can fork complete eclipse project at https://github.com/nikhil9/FaceDetection/

Hough Circle detection in Javacv

Opencv provides Hough circle Detection algorithm which can be used to detect circles. Some information about how algorithm works and its example using Opencv in cpp can be found in below link

We will see how to use cvHoughCircle using Javacv. first we will have to process image to get grayscale or binary image. Using cvSmooth() helps most of the time for good detection however it depending upon kind of object  and background more image processing may be required.

First we will load image and then convert it to grayscale. Then use cvSmooth() to smooth the edges. cvHoughCircle() is used to detect circles and are stored in CvSeq. cvGetSeqElem() is used to extract each circle. We have to use each element in CvPoint3D32f. Center of circle is obtained in CvPoint type using cvPointFrom32f(). Obtained center and radius is used to draw circle on input image using cvCircle.

Following code is a demonstration of all the above processes.

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
public class circleDetection{
public static void main(String[] args){
IplImage src = cvLoadImage("img2.png");
IplImage gray = cvCreateImage(cvGetSize(src), 8, 1);
cvCvtColor(src, gray, CV_BGR2GRAY);
cvSmooth(gray, gray, CV_GAUSSIAN, 3);
CvMemStorage mem = CvMemStorage.create();
CvSeq circles = cvHoughCircles(
gray, //Input image
mem, //Memory Storage
CV_HOUGH_GRADIENT, //Detection method
1, //Inverse ratio
100, //Minimum distance between the centers of the detected circles
100, //Higher threshold for canny edge detector
100, //Threshold at the center detection stage
15, //min radius
500 //max radius
);
for(int i = 0; i < circles.total(); i++){
CvPoint3D32f circle = new CvPoint3D32f(cvGetSeqElem(circles, i));
CvPoint center = cvPointFrom32f(new CvPoint2D32f(circle.x(), circle.y()));
int radius = Math.round(circle.z());
cvCircle(src, center, radius, CvScalar.GREEN, 6, CV_AA, 0);
}
cvShowImage("Result",src);
cvWaitKey(0);
}
}

Input Image:

Output Image:

OpenCV 2.3.1 with CodeBlocks on Ubuntu 12.04

For Installing OpenCV 2.3.1 on ubuntu 12.04 please refer to previous post
http://opencvlover.blogspot.in/2012/05/install-opencv-231-on-ubuntu-1204.html

Once the installation is done, install codeblocks as mentioned in previous post http://opencvlover.blogspot.in/2011/07/installing-opencv-with-codeblocks-ide.html

Create a new console application as mentioned in above post and go to Build Options.
Make sure that GNU GCC compiler is selected in compiler drop down option.
Go to Linker Settings tab and Link libraries which are located at /usr/local/lib/ and looks like 'libopncv_*.so'
An example is shown in image below


Now go to Search directories and under Compiler tab add following location
/usr/local/include/opencv2
Here is a screenshot


Now we are ready to run OpenCV 2.3.1 on Code Blocks running on Ubuntu 12.04.
Here is a sample code to test the installtion


#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
int main()
{
cvNamedWindow("Hello", 1);
cvWaitKey(0);
cvDestroyWindow("Hello");
return 0;
}
view raw Highgui_demo.c hosted with ❤ by GitHub