Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Amit Misra
Amit Misra

Posted on • Edited 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.

Setupbrew on ubuntu

This step is trivial all you need to do is follow the steps mentioned on theirwebsite -
/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 notoptional.

$ 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 modeExit fullscreen mode

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

InstallopenCV usingbrew

  • Let's start by installingant usingbrew like so -brew install ant. This is mandatory in case you want the final installation step to generate theopencv-java460.jar file and the associated shared library.
  • Next execute the following command and update the flag-DBUILD_opencv_java to beON (source)
  • You can check if your system is ready foropencv 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 maintainerswith debugging if you file an issue. If everything you use Homebrew for isworking 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 undoneshould you later need to do so for some reason.  cd /home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-core && git stash && git clean -d -fUncommitted files:   M Formula/opencv.rb
Enter fullscreen modeExit fullscreen mode
  • Finally execute the command to installopenCV 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 commandbrew info opencv it should look something like so -

$ brew info opencv                       ==> opencv: stable 4.6.0 (bottled)Open source computer vision libraryhttps://opencv.org//home/linuxbrew/.linuxbrew/Cellar/opencv/4.6.0_1 (865 files, 324MB) *  Built from source on 2022-11-20 at 18:33:21From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/opencv.rbLicense: Apache-2.0==> DependenciesBuild: 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 ✔==> Analyticsinstall: 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 modeExit 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 modeExit fullscreen mode

Prepopencv-java460.jar formaven

Once you have the finalopencv_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 forlinuxbrew 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 modeExit 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 modeExit fullscreen mode

Post this you can addopencv-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 modeExit fullscreen mode

Gotchas

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Technical Architect | DevOps Enthusiast | Cloud Practitioner | Gamer
  • Location
    New Jersey
  • Work
    Staff Engineer/Architect at Weight Watchers Inc
  • Joined

More fromAmit Misra

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp