Wednesday, December 25, 2013

Getting started with OpenCV via JavaCV

Couple of weeks back when I was watching Person of Interest's (My all time favorite TV show) latest episode I was getting a crazy idea of what if I can build a machine like that. (Of course it so damn crazy but seriously I got that feeling) Then I thought of why don't I give a try on some image processing stuffs that are heavily used in the so called "Machine".


I've never tried on image processing before. Even I didn't follow the course module at uni. So I started as most of us do Google it. But I didn't google it like 'Image processing for beginners'. I knew it is some what advanced stuffs and I didn't want to get touch with advanced stuffs with some languages that I have rarely used. So what I thought of was to try out image processing with Java first. As I mentioned earlier, since I was inspired by the Machine, I wanted to get started with my crazy idea by accessing webcam. I had some previous experience with JMF in 1st year project. But now JMF is almost a dead project. Therefore I had to look for something new. On my way I have found another couple of libraries that are used to access webcam via Java. But what caught my eyes was JavaCVI have heard about OpenCV but never tried that before. But this time it's with Java. So why to wait ;)

As some of you may know OpenCV is stands for Open source computer vision library. (I don't know how they shorten it to OpenCV) It was started my Intel way back in late 1990's
"OpenCV project was initially an Intel Research initiative to advance CPU-intensive applications, part of a series of projects including real-time ray tracing and 3D display walls." - wikipedia
"It has C++, C, Python, Java and MATLAB interfaces and supports Windows, Linux, Android and Mac OS. OpenCV leans mostly towards real-time vision applications and takes advantage of MMX and SSE instructions when available. A full-featured CUDA and OpenCL interfaces are being actively developed right now. There are over 500 algorithms and about 10 times as many functions that compose or support those algorithms. OpenCV is written natively in C++ and has a templated interface that works seamlessly with STL containers." - OpenCV developer team

I think that is enough about background and my nonsense. It is time to get touch with JavaCV.
In order to get start with JavaCV download following things to your machine

  1. JavaCV from https://code.google.com/p/javacv/downloads/list --> javacv-0.6-bin.zip
  2. OpenCV from http://sourceforge.net/projects/opencvlibrary/files/ install this to any place that you like 
  3. JavaCPP from https://code.google.com/p/javacv/downloads/detail?name=javacv-0.6-cppjars.zip

I'm working with Netbeans IDE so if you are not having it download and install it as well. But it is not necessary as long as you are OK with setting up class paths and stuffs when compiling and running.

First I'm going to show you how to capture a photo from your webcam and save it. I'm amusing that all are familiar with Netbeans.
First create a new Java project and add following libraries to your project

  1. javacv.jar
  2. javacpp.jar (These 2 can be found in JavaCV .zip file that you downloaded)
  3. opencv-2.4.6.1-..jar (Use the library that is appropriate to your pc. If you are having x64 machine then use x64 version) This can be found in the JavaCPP .zip file that you download
Then create a new java file and have following code in that
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage; 
public class JavaCV {
   public static void main(String[] args) {
     OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
     try {
       grabber.start();
       IplImage img = grabber.grab();
       if (img != null) {
         cvSaveImage("D:/Photos/capture.jpg", img); // give a path that you like
       }
       grabber.stop();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }

Now run this out. As you run the application you can see that your webcam get open and get close quickly. Check the path that you specified on the code and you can see a image being save there.


No comments:

Post a Comment