How to install OpenCV with Java and GStreamer support on MacOs

Muratcan Yeldan
Testinium Tech
Published in
5 min readJul 6, 2021
Logo of OpenCV
OpenCV (Open Source Computer Vision Library) is a library of programming functions aimed at real-time computer vision which was originally developed by Intel.

STEP 1 : Installing Homebrew

The easiest way to install OpenCV on your macOs is to use Homebrew. If you don’t have homebrew on your system you can install it from here. As it says in the related link, running the following command will be enough to install Homebrew on your system. Open the Terminal and execute :

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

STEP 2 : Installing XCode Command Line Tools

After Homebrew’s installation process is complete check if you have XCode Command Line Tools already installed on your system. Open the Terminal and execute:

xcode-select --install

If it replies with xcode-select: error: command line tools are already installed, use “Software Update” to install updates then you can proceed with this tutorial. Otherwise, follow the prompts that ask for the installation of these tools.

Also you need to install Apache Ant. You can check if you have Ant already installed on your system. Open the Terminal and execute :

brew install ant

If it replies with Warning: ant (VersionNumber) is already installed and up-to-date then you can proceed with next step of this tutorial. Otherwise, Homebrew will install ant on your system.

You should have XCode already installed on your device to complete this step. If you didn’t install XCode on your system yet you can install it from App Store. After installation process complete you can continue with Step 2. Remember, installation of XCode is take time depending on your network speed.

STEP 3 : Setting Up OpenCV

To add Java and GStreamer support to your OpenCV installation you need to edit opencv formula in Homebrew. Open the Terminal and execute :

brew edit opencv

In the text editor that will open, there will be line which starting with args = std_cmake_args + %W[ and there will be many options here. You need to make two changes here. First change this line :

-DBUILD_opencv_java=OFF

To this line :

-DBUILD_opencv_java=ON

With this step you added Java support to your installation. If you want to use GStreamer you need to change this line :

-DWITH_GSTREAMER=OFF

To this line :

-DWITH_GSTREAMER=ON

If you don’t have -DWITH_GSTREAMER option in your file simply just copy it from here and paste in your file. You can paste it anywhere in between args = std_cmake_args + %W[ and closing tag ] .

After you have completed line changes don’t forget to save changes. After you have finished your changes your options should look like this :

Options for opencv formula of Homebrew
Note : Some of your options may be different from this image according to your usage plan.

-DOPENCV_JAVA_TARGET_VERSION=1.8 is also an important option to consider. You should enter your target Java version. Otherwise when you compile your project using IDE you will get errors like “Class file has wrong version 55.0, should be 52.0 ”

STEP 4 : Installing OpenCV

After you completed previous steps you can continue with installing OpenCV on your system. Remember this process will take time depending on your device. This process takes approximately 20 to 25 minutes. Open Terminal and execute :

brew install --build-from-source opencv

This command will be download and install latest available opencv. If you want to download specific version of OpenCV you can search it via this command :

brew search opencv

This command will show available versions of OpenCV. You can install specific OpenCV version from this list with using this command :

brew install --build-from-source opencv@[VersionNumber]

If you don’t want to install older version of OpenCV you can just execute brew install — build-from-source opencv command and skip other steps.

You can check if you installation is complete with executing this command :

brew info opencv

Then you will see a screen like that :

Brew info screen of opencv formula
You can get path from here if you need it. Here is in my example path is “/usr/local/Cellar/opencv/4.5.2_4

STEP 5 : GStreamer Plugins

This step is totally optional. You are perfectly safe with these step to follow in any order or not follow at all. I will not go into details of each plugin, but I will give you an overview so that you have a general idea and can see the available options.

gst-plugins-base :

Base plugins are well supported basic plugin set for GStreamer. You can install them with Homebrew. To install this plugins in your system open Terminal and execute :

brew install gst-plugins-base

gst-plugins-good :

Good plugins are also well supported plugins for GStreamer, under the LGPL(Lesser General Public License). To install this plugins in your system open Terminal and execute :

brew install gst-plugins-good

gst-plugins-ugly :

Ugly plugins are for constructing graphs of media-handling components. To install this plugins in your system open Terminal and execute :

brew install gst-plugins-ugly

gst-plugins-bad :

Bad plugins are less supported and not fully tested libraries for GStreamer. To install this plugins in your system open Terminal and execute :

brew install gst-plugins-bad

As I mentioned earlier you don’t have to install all or any of these plugins. If you need a specific feature with GStreamer you can google it and install necessary plugin.

STEP 6 : Setting Up Intellij IDEA

In this tutorial I will show you how to use OpenCV with your Java project using Intellij IDEA. Other IDE ‘s are have same logic but their menu options may differ from Intellij IDEA.

Firstly open existing project or create a new Java project in Intellij IDEA. Then open File -> Project Structure. Under Project Settings click on Libraries.

Click the + button on the top and select Java.

Add new library screen
You will need OpenCV installation path for this step. If you didn’t note that path from step 4 you can get path with execute this command : brew info opencv

Navigate to the OpenCV installation folder. Inside this folder navigate to share/java/opencv4 and select opencv-452.jar.

Then select + button on the right then select libopencv_java452.dylib. In the end you should see a screen like that :

After you successfully completed earlier steps your Project Structure screen should look like that.

Remember, opencv-452.jar name and libopencv_java452.dylib name can be different on your system depending on your installed OpenCV version.

After that your Intellij IDEA is ready for development with OpenCV. When you are developing a project with OpenCV don’t forget to initialize OpenCV native libraries. You should initialize OpenCV native libraries before calling any OpenCV method. You can do it using:

System.loadLibrary(NATIVE_LIBRARY_NAME);

Thank you for reading my article about OpenCV and GStreamer. I hope it will be helpful with your projects.

--

--