Steps to write OpenCV application in Java ( In mac)

Jeril Kuruvila
4 min readJan 5, 2019

--

Open CV site has a very good tutorial, but this one is more direct like in 5 minutes setup.

Install OpenCV 3.x under macOS

The quickest way to obtain OpenCV under macOS is to use Homebrew. After installing Homebrew, you have to check whether the XCode Command Line Tools are already installed on your system.

To do so, open the Terminal and execute: xcode-select --install If macOS asks for installing such tools, proceed with the download and installation. Otherwise, continue with the OpenCV installation.

As a prerequisite, check that Apache Ant is installed. Otherwise, install it with Homebrew:brew install ant. Ant should be available at /usr/local/bin/ant.

To install OpenCV (with Java support) through Homebrew, you need to edit the opencv formula in Homebrew, to add support for Java: brew edit opencv In the text editor that will open, change the line: -DBUILD_opencv_java=OFF in -DBUILD_opencv_java=ON then, after saving the file, you can effectively install OpenCV: brew install --build-from-source opencv

After the installation of OpenCV, the needed jar file and the dylib library will be located at /usr/local/Cellar/opencv/3.x.x/share/OpenCV/java/, e.g., /usr/local/Cellar/opencv/3.4.5/share/OpenCV/java/.

Please, notice that this method doesn’t work if you update OpenCV from a previous version: you need to uninstall OpenCV and install it again.

Note: you need to wait for a long time, hang in there and installation will finish in a while

Get up and running with your Java Application

  • Create a new Java Project
  • Add a User Library to the project
  • Write some OpenCV code
  • Build and Run the application

Create a New Project

Open Eclipse and create a new Java project; open the File menu, go to New and click on Java Project.

Add a User Library

If you followed the previous tutorial (Installing OpenCV for Java), you should already have the OpenCV library set in your workspace’s user libraries; if not please check out the previous tutorial. Now you should be ready to add the library to your project. Inside Eclipse’s Package Explorer just right-click on your project’s folder and go to Build Path --> Add Libraries....

And then

Ignore the already existing opencv, i am doing this now for the second time :-)

Click on the New

Once created use Add External Jars option

And you are done.

Now then

Write some code and run it

This just some code to test open cv is working or not.

Oh yes , Do not remember to not forget the final step before running

Your run configuration should have

in Run configuration mention the opencv java path.

And Now run and make sure that it is working fine.

Reach out for any clarifications

Now you are up and running with OpenCV. If you want you can checkout my post on Image Pattern Matching [Place Holder]

Thanks for reading . And make sure you clap if you like it .

--

--