Saturday, January 19, 2013

Setting-up javaCV on NetBeans and Eclipse

JavaCV is a set of wrapper classes used for openCV. Using javaCV any C code used for Image Processing can be converted in to a java code. For a person who have an expertise knowledge on java and a little knowledge on C, javaCV will be very useful as he only have to worry about the logic of the code not the syntax. Following is a guide to install and configure javaCV in netbeans or eclipse environment.
  1. Install Microsoft Visual C++ Microsoft Visual C++ 2010 Redistributable Package (x86) or Microsoft Visual C++ 2010 Redistributable Package (x64) (If your system is 64 bit) 
  2. Download opencv 2.4.0 OpenCV windows SuperPack version 2.4.0 
      • Note- Opencv 2.4.1 does not work with this. It gives a linking error.
  3. Extract opencv to C: 
  4. Set up environment variables
    • 64-bit version of the JDK: C:\opencv\build\common\tbb\intel64\vc10\;C:\opencv\build\x64\vc10\bin\; 
    • 32-bit version of the JDK: C:\opencv\build\common\tbb\ia32\vc10\;C:\opencv\build\x86\vc10\bin\;                                       ”.
  5. Download and extract javacv Link. This contains 8 jar files
  6. When creating projects add those jar files to the libraries folder
    •  In netbeans right click Libraries and select ”add JAR/folder” and select above jar files.
    •  In eclipse right click the project folder and select properties. Select “java build path” and select the Libraries tab and add the required jar files.
  7. Check the following code. This captures an image from the web cam.
import com.googlecode.javacv.OpenCVFrameGrabber; 
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_highgui.*; 

public class CaptureImage {

private static void captureFrame() { 
  // 0-default camera, 1 - next...so on 
  final OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
  try { 
    grabber.start();
    IplImage img = grabber.grab();
    if (img != null) { 
      cvSaveImage("capture.jpg", img);
    }
  } catch (Exception e) { 
    e.printStackTrace(); 
  } 
}

public static void main(String[] args) { 
  captureFrame(); 

}
  • You can find the captured picture on the project folder. To change the camera change the number that is passed to the OpenCVFrameGrabber constructor.


Sutra Directory

11 comments:

  1. when i try this code after doing this steps,i have error that i cant resolve it .please help me
    C:\Users\MaJeSTiC\AppData\Local\Temp\javacpp867018211705\jniopencv_core.dll: Can't find dependent libraries

    ReplyDelete
  2. I got the same error when I used opencv 2.4.1 (as stated in step 2)
    But when I used 2.4.0 there was no error. Also check whether the step 4 is done correctly.

    ReplyDelete
  3. when i try this code after doing this steps, I used 2.4.0 ,i have error that i cant resolve it .please help me
    C:\Users\MaJeSTiC\AppData\Local\Temp\javacpp867018211705\jniopencv_core.dll: Can't find dependent libraries
    Please help me quickly

    ReplyDelete
  4. @nuwan
    I got the same error with 2.4.1.
    But was able to successfully run openCV using the above steps.
    Did you correctly set up environment variables ??

    ReplyDelete
  5. I've followed all the instructions you gave. But the error is-
    "java.lang.UnsatisfiedLinkError: jniopencv_core.dll: Can't find dependent libraries" can you please help me in this? It was kind of urgent.
    Thank you.

    ReplyDelete
  6. Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\Jaimin\AppData\Local\Temp\javacpp824382310842\jniopencv_core.dll: Can't find dependent libraries

    ReplyDelete
    Replies
    1. Did you correctly set up environment variables ??

      Delete
  7. I am having same Error even after setting up environment variables perfectly. OpenCV is working perfectly but not JCV, nither using 2.4.8 nor 2.4.0

    ReplyDelete
    Replies
    1. May be a problem with the version of OpenCV. When I used 2.4.1 there was some error.

      Delete
  8. I was seen error "could not find the main class?"
    plz reply me...what i do?

    ReplyDelete
    Replies
    1. may be something wrong with the code.
      I did not get such an error

      Delete