Sunday, May 26, 2013

OpenCV in Java/Eclipse - Quick install guide

I followed this:

Here I just go through my experience and some of the stuff I got caught in...

Dowload OpenCV for Windows. In my case I got 2.4.5 version. 270MB zipped that turns into 2.7GB once extracted!! It is basically a folder with the libraries inside, which I placed in the Eclipse folder (but I don't think it matters).

Open Eclipse and follow the instructions on the top link. When adding the library look for the java at eclipse/opencv/build/java. There is a moment where the instructions get cut, talking about the native library location. Click edit and enter the location of the dll. In my case, opencv_java245.dll is located in C:\eclipse\opencv\build\java\x86.If you don't this, you'll get an error (see here)

A question I got unresolved is that I can't point to the x64 directory while my system is x64... I got to use the 32 bit version... Anyhow...

The rest should work. I ran this in a brand new installation of eclipse (basically unzip the download from in my hard drive) and it worked right away. Nevertheless, initially, I had only the C/C++/QT package installed. It had even the perspective for Java, but it was not executing. I was getting "The selection cannot be launched and there are no recent launches". I found out that JDT (Java development tools) were not installed, so, went to help > install new software, select in "work with" a place to get the software from, and then browse to Programing languages. You should click this:

and see this before clicking finish.

Now everything should work. At most, you will get that same error again when you click execute, but it is because you didn't build the project. Click build and go!

Then we go into the second part of the tutorial, when they detect the face on the picture. It looks like they want us to use SBT. Not sure why, but decided to do it the traditional way with Eclipse. Create a project, like we just did. The question is what to do with the resources (the .xml and .png files). Where to put them and how to make the code see them.

The way the code is written, is looking into the CLASSPATH, which is the path where the source is. So, we need to create a resource file in that directory. The best way that I found to do that is to right click on the project name and do New > Source File. In Folder name put "resources". Then right click again in the project name and do import which brings up this window:

 Pick file system which opens:

The funny part is that even with this, I was getting an error. It was not reporting that it didn't get the path right, but that the file was empty! I.e., the portion of the line CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath());
where we get the path (getClass().getResource("/lbpcascade_frontalface.xml").getPath()) was working but the resulting string was pointing to no file. If we just wrote the full path directly, it worked:
CascadeClassifier faceDetector = new CascadeClassifier("C:/USERDATA/MEDICAL/CODE/face_detect/resources/lbpcascade_frontalface.xml");

The problem is actually a bug!!

Basically the code finds the path but returns an extra "/". So, when the code goes to fetch the file, it can not find it. This is solved with the following code:
    String str;
    str = getClass().getResource("/lbpcascade_frontalface.xml").getPath();
    str = str.replace("/C:","C:");
    CascadeClassifier faceDetector = new CascadeClassifier(str);

Do the same for the 2nd file... And voila!

PS.: Click here to see the index of these series of posts on OpenCV


  1. Detected 0 faces
    Writing faceDetection.png
    libpng warning: Image width is zero in IHDR
    libpng warning: Image height is zero in IHDR
    libpng error: Invalid IHDR data

    I am getting this problem when i run the code.. Kindly help me out....

    1. I think you basically are getting the same problem as I was getting above... The file path is not right, either because of some dummy mistake (like you put it in the wrong place) or more likely, because of the path the instruction above returns. Have you tried spelling out the full file path directly?
      See also this:

    2. create a new folder and save the image and xml in it and then take the path of that and store it an string .then replace the path in program with that string .
      that should do it