Setting up Mac for OpenCV Java development with IntelliJ Idea
Step 1: Installing OpenCV
The quickest and easiest way to install OpenCV on your mac is to use Homebrew. If you don’t have homebrew on your system you can download and install it from here.
After installing Homebrew 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 error: command line tools are already installed then you can proceed with installing OpenCV. If not, follow the prompts that ask for the download and installation of these tools.
Check if Apache Ant is installed. If it is not installed you can use Homebrew to install it by executing:
brew install ant
To install OpenCV Java, you need to edit the opencv formula in Homebrew. You can do this by executing:
brew edit opencv
In the text editor that will open, change the line -DBUILD_opencv_java=OFF to -DBUILD_opencv_java=ON, and save the file.
Now install the latest version of OpenCV by executing:
brew install --build-from-source opencv
If you need to install an older version, check for available versions using
brew search opencv
Install it using
brew install --build-from-source opencv@[Version]
That’s it! OpenCV is now installed on your Mac. You can check the installation by executing:
brew info opencv
It should return something like this. You can check the version of OpenCV installed and the installation status of optional dependencies you may need in your project.
Take a note of the installation location, as we will be needing this for the next step.
Step 2: Setting up IntelliJ Idea
Create a new Java Application project or open your existing application in IntelliJ Idea.
Open File -> Project Structure
. Under Project Settings
click on Libraries
.
Click the +
button on the top and select Java
.
Now navigate to the OpenCV installation folder that we had noted from the last step. Inside this folder navigate to share/java/opencv4
and select opencv-430.jar
.
The folder path, jar name, dylib name and module name that is created might be different if you installed a different version of OpenCV.
Select module opencv-430. Click the +
button on the bottom. Navigate to the same folder where we found the JAR file and select libopencv_java430.dylib
.
It should add the native libraries like the image above. Click on OK.
That’s it. OpenCV is set up and all ready for development!
You can find the code for the sample project here.
Don’t forget to initialize OpenCV native libraries before calling any OpenCV functions. You can do it using:
System.loadLibrary(NATIVE_LIBRARY_NAME);
Thank you so much for reading my article. Hope that it was helpful!
Attributions:
Heart Icon by Jemis Mali on Iconscout, Plus Sign by Freepik from Flaticon, Equal by Smashicons from Flaticon