Friday, June 13, 2014

OpenCV Java with Eclipse

Working With OpenCV Eclipse.


The Open Source Computer Vision Library has >2500 algorithms, extensive documentation and sample code for real-time computer vision. It works on Windows, Linux, Mac OS X, Android and iOS.

Download OpenCV from below link:
http://sourceforge.net/projects/opencvlibrary/
Run the .exe file. The self extractor will ask you to provide location to store the extracted file.

My environment configuration
    OS: Windows 8 (64-bit)
    Eclipse: Kepler Service Release 2 , 32-bit
    OpenCV: Version 2.4.9



Follow below steps to configure OpenCV and Eclipse.
  1. Open Ecplise.
  2. Goto Window --> Preferences 
  3. Java --> Build Path --> User Libraries 
  4. Click on New... button 
  5. "New User Library" box will open. Enter the name of the Library as shown in figure. Now Click on "OK" button. 
  6. Now Click on "Add External JARs..." button 
  7. Locate and select the "opencv-249.jar" file from the "JAR selection" dialog box. See the circle for the path to 'opencv-249.jar' file. Now Click on "open" button.
  8.  "Preference" Box will look like below figure.
  9. Now select "Native library location" and click on "Edit..." button.
  10. Now click on "External Folder..." to locate the native library.
  11. See the figure to locate the native library. If the eclipse is of 64-bit, then you should choose 'x64' folder. Since I am using Eclipse 32-bit, so I choose 'x86' folder. Then click on "OK" button.
  12. Now click on the "OK" button.
  13. Now click on "OK" button on the preference box.

 Now it is the time to test. Follow the below steps to test the above configurations.


  1. Open Eclipse , and create a new Java project. (Say the project name is "testOpenCV")
  2. After creating the project, right click on the project name and select "Properties" option.
  3. Now select "Java Built Path" --> "Libraries" tab --> "Add Library" button.
  4. Select "User Library" and click on "Next" button. 
  5. Now select the add the user library, "openCV-2.4.9" and click on "Finish" button. 
  6. Now click on "OK" button. 
  7. Now create a java file. (say the file name is "test") 
  8. Now insert the following Code.

  9. package testOpenCV;
    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;

    public class test {

       public static void main(String[] args) {
       System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
       Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
       System.out.println( "mat = " + mat.dump() );

    }
    }

    Now Execute the java file. You should get the following output.





    -Thank you.
     

No comments:

Post a Comment