Install OpenCV for Java on Mac OS X

Computer Vision is like deep space, the more you learn about it you realise the field is vast, mesmerising and will leave you in awe of what can be achieved.

Recently I came across a work related opportunity to implement a prototype application based on computer vision and object recognition. This led me to OpenCV which is an open source computer vision library available for various platforms Mac OS X, Windows, Linux, iOS and Android.

But before I could start playing around with OpenCV, I had to first install it on my Macbook which was supposed to be easy but while going through different tutorials, 5 to be accurate it became clear that each tutorial was missing one or the other critical step which was preventing me to complete the OpenCV installation. Finally, I got through the installation and decided on writing a tutorial which had the precise steps to install OpenCV on Mac OS X that should help anyone looking to install OpenCV on their Mac OS X’s without any hassle or confusion.

The tutorial will provide the steps to install and test OpenCV for Java.

We will be using Homebrew to install Ant, CMake to compile and generate OpenCV package.

Using Homebrew makes its easy to install Ant but I will also provide an alternate installation procedure for Ant.

Lets begin.

JDK (Java SE Development Kit)
Download the latest JDK from The current JDK version is Java SE 8u102.

Command Line Tools
Either install Xcode on your laptop which will install Command Line Tools along with the Xcode App else you can paste this command in the terminal and it will install only the Command Line Tools

xcode-select install

Install Homebrew using the following command in the terminal:

/usr/bin/ruby -e “$(curl -fsSL”

Then run:

brew update

Even though this is not required since we just installed Homebrew. Its a good practice to run ‘brew update’ on a regular basis which allows Homebrew to download updates and patches.

After ‘brew update’ runs successfully, now run:

brew doctor

‘brew doctor’ makes sure that any package that has been installed via Homebrew is in place and will not cause an issue with the system. If any package might cause an issue then at the end of the test ‘brew doctor’ will show either warning/error mentioning the package that might cause an issue/is causing an issue and will also provide a hint for fixing the issue.

To install Ant, run

brew install ant

Now we are ready to go to the next step that is compile and generate OpenCV for Mac OS X.

Download OpenCV files from For the purpose of this tutorial we will using OpenCV version 3. As of today the current version is 3.1. Once the zip file is downloaded, unzip it. Create a folder called ‘OpenCV-Bundle‘ in your User Directory ~/username. To find your username type:


in the terminal and you will get your user nameLogged In Username

Place the unzipped folder in the ‘OpenCV-Bundle‘ folder that you have created.

Also download OpenCV Contrib from, it contains extra modules for OpenCV. Download and place ‘OpenCV Contrib’ folder in the same ‘OpenCV-Bundle‘ that was created earlier.

Now we will use CMake Application to compile and generate OpenCV package that can be used on our laptop for development. CMake is an open-source, cross-platform family of tools designed to build, test and package software.

Download CMake for Mac OS X via Download the .dmg file from the Platforms section. Once the download is complete open the dmg and move CMake to Applications Folder.CMake

Open CMake App

1. Click on ‘Grouped’ and ‘Advanced’ so that both are active.BrowseSource

2. Click on Browse Source and go to the OpenCV-version_number folder within ‘~/username/OpenCV-Bundle‘ folder and select Open.SourceFolder

3. Clink on Browse Build and go to the ‘OpenCV-version_number’ folder within ‘~/username/OpenCV-Bundle‘ folder. Click on ‘New Folder’ and name the folder ‘build’. This folder will be where we compile and generate OpenCV package.SourceFolder-CreateBuildFolder

4. Select the ‘build’ folder and click OpenBuildFolder

Before we go ahead and configure make sure ‘Grouped’ and ‘Advanced’ are selected.

5. Click Configure and a pop up will open.
Select ‘Unix Makefiles’ under ‘Specify the generator for this project’.
Select ‘Use default native compilers’.ConfigureSettings

Click Done

6. The configuration process will take 1-2 minutes. Once its complete you will see the following output on your screen.ConfigureResult1

Don’t worry about the red warnings. We will fix that in the upcoming steps.

7. We will now select/deselect options that are needed to install OpenCV for Java.

  • Expand CMake and enter ‘Release’ for CMAKE_BUILD_TYPE
  • Expand OpenCV and select the ‘modules’ folder OPENCV_EXTRA_MODULES_PATH

8. Click Configure. The configuration process will take 1-2 minutes. Once its complete you will see the following output on your screen.Configure2

9. Without making any changes click Configure again. Once its complete you will see the following output on your screen.Configure3

10. All the warnings have been cleared. Now click Generate. Once the process is complete go to ‘build’ folder in the terminal using the following command.

cd ~/username/OpenCV-Bundle/OpenCV-version_number path/build

build folder

11. Now type:



this process will take couple of minutes. Meanwhile grab a cup of coffee. 😉

Once the ‘make’ process is completemake process complete


Go to

cd ~/username/OpenCV-Bundle/OpenCV-version_number path/build/bin folder

and you should find ‘opencv-310.jar’ filejar file

Before we go ahead and write some code to check out OpenCV. Open the terminal and go to

cd ~/username/OpenCV-Bundle/OpenCV-version_number path/build/lib folder

and type

ln -s libopencv_java310.dylib

This will create a symbolic link for

Download Eclipse IDE for Java EE Developers

12. Open Eclipse and go to Preferences.Preferences

13. Inside Preferences, go to Java > Build Path > User LibrariesJava-BuildPath-UserLibraries

14. Click New and enter name. Since we are using OpenCV version 3, you can enter ‘opencv3’ in the pop up box. Leave ‘System Library’

15. Click Add External JARs and go to

~/username/OpenCV-Bundle/OpenCV-version_number path/build/bin folder

and select ‘opencv-310.jar’ filejar

16. Expand ‘opencv-310.jar’ within ‘opencv3’ and select ‘Native library location:’ and click Edit.edit native

In the pop up click External Folderexternal selection

And go to

~/username/OpenCV-Bundle/OpenCV-version_number path/build/lib folder


and click Open

Create and Test OpenCV
It’s time to finally reap the rewards of the setting up OpenCV

17. Create a new ‘Java Project’new project

18. Give the project name ‘HelloCV-Test’ with the following configurationproject name

19. Right click on project name ‘HelloCV-Test’ > Build Path > Add Librariesadd library to project

Select ‘User Library’ and click Nextuser library

Check ‘opencv’, in your case it will be called ‘opencv3’ and click Finishselect opencv

20. Right click on project name ‘HelloCV-Test’ > New > Classnew class main

and give the class name ‘HelloCV’
create new class

21. Enter the following code:

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

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

Build and Run. If everything went well you will see the following output in the console

mat = [  1,   0,   0;

         0,   1,   0;

         0,   0,   1]

run code

Source Code: Download

Coming Soon:
Alternate installation procedure for Ant

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s