DEV Community

Amit Misra
Amit Misra

Posted on • Updated on

Install OpenCV for Java using brew on Ubuntu

Learning new stuff over the weekend is something we all do and this weekend, I started exploring OpenCV. What follows next is my walkthrough for setting up my system with OpenCV 4.6.0 on Ubnutu 20.04.

Overview

The installation process wasn't that straightforward, specially if you want to use Java as your programming language of choice. There are a few documentation around but I ended up taking a different route which worked like charm without any major issues.

Setup brew on ubuntu

This step is trivial all you need to do is follow the steps mentioned on their website -
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once the installation completes the script will ask you to execute the following commands in the terminal. Please be advised, these commands are not optional.

$ echo '# Set PATH, MANPATH, etc., for Homebrew.' >> $HOME/.zprofile
$ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> $HOME/.zprofile
$ eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

## Additional commands
$ sudo apt-get install build-essential
$ brew install gcc
Enter fullscreen mode Exit fullscreen mode

Additionally if you are using zsh like I am, add the following line to your $HOME/.zshrc file - source $HOME/.zprofile

Install openCV using brew

  • Let's start by installing ant using brew like so - brew install ant. This is mandatory in case you want the final installation step to generate the opencv-java460.jar file and the associated shared library.
  • Next execute the following command and update the flag -DBUILD_opencv_java to be ON (source)
  • You can check if your system is ready for opencv to be installed by executing the following command - brew doctor. The output should look something like so, which is understandable as we changed the flag in the last step
$ brew doctor       
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: You have uncommitted modifications to Homebrew/homebrew-core.
If this is a surprise to you, then you should stash these modifications.
Stashing returns Homebrew to a pristine state but can be undone
should you later need to do so for some reason.
  cd /home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-core && git stash && git clean -d -f

Uncommitted files:
   M Formula/opencv.rb
Enter fullscreen mode Exit fullscreen mode
  • Finally execute the command to install openCV on your system like so - brew install opencv --build-from-source

Checking the installation

Once the install command finishes, you should be all good. Check the output of the command brew info opencv it should look something like so -

$ brew info opencv                       
==> opencv: stable 4.6.0 (bottled)
Open source computer vision library
https://opencv.org/
/home/linuxbrew/.linuxbrew/Cellar/opencv/4.6.0_1 (865 files, 324MB) *
  Built from source on 2022-11-20 at 18:33:21
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/opencv.rb
License: Apache-2.0
==> Dependencies
Build: cmake ✔, pkg-config ✔
Required: ceres-solver ✔, eigen ✔, ffmpeg ✔, glog ✔, harfbuzz ✔, jpeg-turbo ✔, libpng ✔, libtiff ✔, numpy ✔, openblas ✔, openexr ✔, openjpeg ✔, protobuf ✔, python@3.10 ✔, tbb ✔, vtk ✔, webp ✔, zlib ✔
==> Analytics
install: 16,116 (30 days), 47,026 (90 days), 194,148 (365 days)
install-on-request: 15,595 (30 days), 45,590 (90 days), 187,137 (365 days)
build-error: 96 (30 days)
Enter fullscreen mode Exit fullscreen mode

Confirm you have the necessary shared library and jar file created under the installation folder like so -

$ ls /home/linuxbrew/.linuxbrew/Cellar/opencv/4.6.0_1/share/java/opencv4 
libopencv_java460.so  opencv-460.jar
Enter fullscreen mode Exit fullscreen mode

Prep opencv-java460.jar for maven

Once you have the final opencv_java460.jar, you can install it to your local repository for development purposes. This ensures you are not making multiple copies of the jar file or referring to an external jar with unusual path for linuxbrew home directory. I used the following command for the same -

mvn org.apache.maven.plugins:maven-install-plugin:3.1.0:install-file  -Dfile=/home/linuxbrew/.linuxbrew/Cellar/opencv/4.6.0_1/share/java/opencv4/opencv-460.jar \
-DgroupId=org.opencv \
-DartifactId=opencv-java \
-Dversion=4.6.0_1 \
-Dpackaging=jar \
-DlocalRepositoryPath=$HOME/.m2/repository
Enter fullscreen mode Exit fullscreen mode

The output of the above command should something like so -

...
...
[INFO] pom.xml not found in opencv-460.jar
[INFO] Installing /home/linuxbrew/.linuxbrew/Cellar/opencv/4.6.0_1/share/java/opencv4/opencv-460.jar to $HOME/.m2/repository/org/opencv/opencv-java/4.6.0_1/opencv-java-4.6.0_1.jar
[INFO] Installing /tmp/mvninstall7616729450445116135.pom to $HOME/.m2/repository/org/opencv/opencv-java/4.6.0_1/opencv-java-4.6.0_1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.325 s
[INFO] Finished at: 2022-11-20T18:41:42-05:00
[INFO] ------------------------------------------------------------------------
Enter fullscreen mode Exit fullscreen mode

Post this you can add opencv-java as a dependency in your favorite project like so -

<dependency>
    <groupId>org.opencv</groupId>
    <artifactId>opencv-java</artifactId>
    <version>4.6.0_1</version>
<dependency>
Enter fullscreen mode Exit fullscreen mode

Gotchas

During the installation I ran into an issue with too many open files when I executed the brew install opencv --build-from-source command. In order to resolve that I have to increase the ulimit like so - ulimit -n 2048.

Top comments (0)