Wednesday, June 13, 2012

Configuring javacv

opencv (open computer vision) is an image processing library which is introduced by intel that is very powerful in processing images in very powerful and effective manner. However opencv is written in c language so most of my friends including me faced several difficulties with programming in c. So I decided to move to javacv ; a java wrapper that can naively call opencv functions. It is a general acceptance that java is relatively slower than C but believe me people who invented javacv have designed this in a way that you can never feel that kind of slowness in your java code.

In this blog post I hope to explain how to configure javacv with your IDE (In my case I use netbeans). Though javacv is easy, configuring is bit hard to catch from the first attempt. So I think this will be very useful to java people who are intending for new technology.

To install javacv there are few requirement
JDK
opencv 2.4 http://sourceforge.net/projects/opencvlibrary/files/
Netbeans ide
javacv-0.1-bin.zip  http://code.google.com/p/javacv/downloads/list


You need to install opencv2.4 into C:/opencv folder (It is the default location). I recommend to use same locations otherwise you will have to face many difficulties.
Extract javacv-0.1-bin.zip to a location in your hard disk 
Go to netbeans and create a new java project.
Add those jar files to your project







Then run this code




package javaapplication1;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;

public class JavaApplication1 {

    public static void main(String[] args) {
        show("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
    }
    public static void show(String filename) {
        cvNamedWindow("dim");
        IplImage image = cvLoadImage(filename);
        if (image != null) {
            cvShowImage("dim", image);
            cvWaitKey(0);
            cvReleaseImage(image);
        }
     
    }
}



If you have installed opencv to default folder this code should run without any error. 

If it gives an error like this
This exception tells that it can't find opencv location in your machine. Actually this error comes from the one of the jar you have added to the project. In my case it is javacv-windows-x86.jar. It depends on your processor architecture. To resolve this error you have to download  http://www.dependencywalker.com/
to catch what are the dependencies are missing in you jar.
Then extract that jar using win rar and make sure that jniopencv_core.dll is in that folder.
Open that dll using  Dependency Walker 
As you can see there is one dll is missing. It is marked from yellow color. These dll are located in your opencv installation folder. Go to it and search for that dll. Then you can add it to system path. There are two ways to do that.
1 Easy way - you can simply copy that dll s to system32 folder in windows folder
2 Using environmental variable settings
 go to my computer -> properties -> Advanced System Settings -> Environment Variables
Select the path variable from the category of system variables

Click edit and append the path of the dll file to the end of the variable

Then click view -> refresh in Dependancy walker
If more dependency errors are shown, do the same procedure until no error is shown. I know this is hard :) But I couldn't figure out an easy way to do this

If all configurations are ok then you will be able to run that small code

References 
Any questions?