Monday 5 August 2013

Installing opencv for java on MAC OS X and Configuring Eclipse

Opencv is an open source library for computer vision, Now i am going to show you how to install it on Mac OS X.
Steps:

  • Download a fresh copy of opencv-2.4.6.1 from here.
  • Extract it to some location in your Mac.
  • now open terminal go inside the extracted folder opencv-2.4.6.1.
  • Type following commands
  • mkdir build
  • cd build
  • Make sure your JAVA_HOME environment variable is set or explicitly type export JAVA_HOME=<location of java home folder>
  • cmake -G "Unix Makefiles" -D CMAKE_CXX_COMPILER=/usr/bin/g++ -D CMAKE_C_COMPILER=/usr/bin/gcc -D WITH_CUDA=ON .. 
  • make -j4 // here number 4 means the number of cores in your processor
  • make install

Configuring Eclipse with opencv


  • Open eclipse
  • Click on File-> New Project-> Java Project
  • Give an appropriate Project Name
  • Now right click on Project and choose Properties.

  • Select Java Build Path and then select Libraries.


  • Click on Add Library.


  • Choose User Library.
  • Click on User Libraries, then Select New and give a name to your library.


  • Now Select your library and click on Add External JARs, go to your build folder and then open bin folder, there you will find opencv-246.jar, Select this file.
  • If you are not able to find .jar then make sure your JAVA_HOME variable is set in the environment variables at the time of build.
  • Now Select Native Library Location and click on Edit.


  • Insert the path of your cv.so file, mine is in /build/lib.


  • Click on Finish, Now your project is configured with openCV library.


19 comments:

  1. This post was so helpful! Thanks for sharing!

    ReplyDelete
    Replies
    1. Hello Sumit, I'm facing problems when I try to run a .jar of my application, that I generated with Eclipse. It throws the following error:
      Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java246 in java.library.path
      at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1878)
      at java.lang.Runtime.loadLibrary0(Runtime.java:849)
      at java.lang.System.loadLibrary(System.java:1087)
      at Main.main(Main.java:11)

      I need that the .jar runs in any computer. I would appreciate your help a lot!
      Thank you very much!

      Delete
    2. Go to File ---> Export ---> Chooose-> Java Runnable Jar
      Then choose ->Copy required libraries into a subfolder next to the generated jar
      You can also save it as ANT script.

      Delete
    3. Hi, thanks you for your help. However, I tried that and I still have the same exception when I run a .jar generated...

      Delete
    4. Hi Sumit, maybe the problem is how I load the library of OpenCV in the program... Because, I generated the jar as you mentioned... How do you load the library of OpenCV? Thank you so much for your help.

      Delete
    5. I am using this method to load libraries
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

      Delete
    6. Hi

      I have the same problem. In Eclipse everything works fine but no exporting option works.
      I'm using as well System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
      and did everything like you said.

      Do you have any more hints for me where to search?

      Delete
    7. Solved it like this:

      https://stackoverflow.com/questions/32192866/executable-jar-with-opencv-lib-eclipse-mac/32255233#32255233

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. hi,
    i follow the command above, but i cannot find my opencv-247.jar,
    do u know where is the build path ?

    thanks : )

    ReplyDelete
  4. Hi,
    I cannot find the .jar file as well. All I found is a bunch of unix executable files inside the bin folder of build.
    Any idea why this happens?

    Thanks~

    ReplyDelete
    Replies
    1. i again build it with the same instructions and it is successfully generating the .jar files in the build/bin directory

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. okay - for all the people who don't find their .jar file in the build/bin folder, you need to install ant first. i just had the same problem and it all worked afterwards.
    ant is easily installed via the terminal command "brew install ant". for this to work you need to install the package manager hombrew first. you can find the instructions here: http://brew.sh

    ReplyDelete
  7. Thanks for the detailed instructions and comments

    ReplyDelete
  8. Hi, I follow the steps, but when I run my project, this says:
    Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1764)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1044)
    at visionporcomputadora.main.main(main.java:46)

    the line 46 says: System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    I have a macbook. hope you can help me, sorry for my english. thanks!

    ReplyDelete
    Replies
    1. Take a look at this link, if System.loadLibrary(Core.NATIVE_LIBRARY_NAME) doesn't work
      https://stackoverflow.com/questions/32192866/executable-jar-with-opencv-lib-eclipse-mac/32255233#32255233

      Delete