Installing OpenCV 4 on MacOS

Nicolas Morales
4 min readMay 28, 2020

--

Base OpenCV installation

I’ve worked with OpenCV version 3 on Windows and Ubuntu in the past, and did a single install in OSx a couple of years ago.

If you landed here, you are probably working on a Mac based system, and looking forward to work with OpenCV. If this is the case, check my previous article on getting HomeBrew installed on your system.

After getting HomeBrew installed, our next step on this journey is to get the latest version of OpenCV. OpenCV 4 was released on late 2018, and provides some new features mainly related to deep learning.

We are not particularly interested in those features (for now), but we’ll just get the latest version so we don’t need to struggle in the future with updates.

Recommended read

There’s a great site by Adrian Rosebrock where you can find tons of cool examples of different applications of OpenCV, as well as installation instructions.

For this specific scenario, as we are just focusing on Java, we’ll follow the simple instructions from the following site (https://opencv-java-tutorials.readthedocs.io/en/latest/01-installing-opencv-for-java.html), to modify HomeBrew formula for OpenCV, and install it will Java compatibility.

Installation Process

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 to -DBUILD_opencv_java=ON .

Also, make sure you have ant installed in your system, if not, install it with HomeBrew as well.

brew install ant

After modifying the brew command, and installing ant, you can effectively install OpenCV: brew install --build-from-source opencv

Installation will take some minutes. Don’t panic!

The brew install command has built OpenCV from the source code (which can actually be found at GitHub). Note OpenCV is developed in C++, but they offer lots of wrappers in different languages, even for Mobile and for the Web Browsers. This build process will generate both the wrapper library .jar file, and the native dylib library, that we’ll need to reference from our development environment.

After the installation of OpenCV is done, you’ll find the needed files in the path below:

/usr/local/Cellar/opencv/4.3.0_3/share/java/opencv4

  • note this path will vary for different OpenCV versions

Setting up the IDE

I’ll be using Eclipse for Development. So I’ll need to create a new Java Project from scratch.

Creating new Java Project

Then create an empty main class.

Right click the Project name on the project explorer, and click on properties.

Go to Java Build Path on the left menu.

Click on Add External JARs button on the right side, and browser for the jar file generated during the OpenCV Build explained above.

Pick the Jar file and Click Open.

Now click on the arrow on the left of the jar you’ve just imported, then click on native Library location, and finally click on Edit… button on the right side.

In the Dialog box, type in the path for the dylib file

/usr/local/Cellar/opencv/4.3.0_3/share/java/opencv4

Finally Click on Apply and Close.

You are done with importing OpenCV into your Java Project.

You must now initialise the Library by using the following command before using any OpenCV Function.

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

This Core library comes from Open CV, so if you already have the .jar into your project, it will automatically try to import it from org.opencv.core.Core;

A simple check will be creating an identity matrix and dumping it out to the console.

Mat mat = Mat.eye(3,3, CvType.CV_8UC1);

System.out.println(“mat = “ + mat.dump() );

You should get the following output in the terminal:

mat = [ 1, 0, 0;

0, 1, 0;

0, 0, 1]

This means you are done with OpenCV setup, and you can start developing your Computer Vision based application in Java :D.

Important:

Make sure you are compiling and executing your new project with the same JDK version the Library was compiled, otherwise you may run into JRE compatibility issues. (i.e. Library was compiled with a newer JRE, so your new Project won’t be able to use openCV’s functions from the generated Jar file.

Hopefully we can get hands-on into some real-world examples soon!

Cheers!

--

--