在Ubuntu上使用Brew安装Java的OpenCV
#ubuntu #java #opencv #brew

在周末学习新东西是我们所有人都在做的事情,这个周末,我开始探索OpenCV。接下来是我在Ubnutu上使用OpenCV 4.6.0设置系统的演练。

概述

安装过程并不是那么简单,特别是如果您想使用Java作为您选择的编程语言。周围有一些文档,但我最终采取了一条不同的路线,这些路线像魅力一样,没有任何重大问题。

ubuntu上的设置brew

这个步骤是微不足道的,您需要做的就是遵循其website上提到的步骤 -
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

安装完成后,脚本将要求您在终端中执行以下命令。请注意,这些命令不是可选

$ 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

另外,如果您像我一样使用zsh,请在$HOME/.zshrc文件中添加以下行-source $HOME/.zprofile

使用brew安装openCV

  • 让我们从使用brew so -so -brew install ant安装ant开始。如果您希望最终安装步骤生成opencv-java460.jar文件和关联的共享库。
  • ,这是强制性的。
  • 下一步执行以下命令,然后更新flag -DBUILD_opencv_javaONsource
  • 您可以通过执行以下命令-brew doctor来检查系统是否已准备好安装opencv。输出应该看起来像这样,这是可以理解的,因为我们在最后一步中更改标志
$ 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
  • 最终执行命令以在系统上安装openCV这样的命令-brew install opencv --build-from-source

检查安装

安装命令完成后,您应该很好。检查命令brew info opencv的输出,它应该看起来像这样 -

$ 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)

确认您具有在安装文件夹下创建的必要共享库和JAR文件,如So -

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

opencv-java460.jar的prep maven

拥有最终的opencv_java460.jar后,您可以将其安装到本地存储库中以进行开发目的。这样可以确保您不会制作jar文件的多个副本,也可以参考具有linuxbrew主目录的异常路径的外部罐子。我使用以下命令进行相同的命令 -

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

上面命令的输出应该像这样 -

...
...
[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/org/opencv/opencv-java/4.6.0_1/opencv-java-4.6.0_1.jar
[INFO] Installing /tmp/mvninstall7616729450445116135.pom to $HOME/.m2/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] ------------------------------------------------------------------------

发布此信息,您可以将opencv-java添加为您喜欢的项目中的依赖性 -

<dependency>
    <groupId>org.opencv</groupId>
    <artifactId>opencv-java</artifactId>
    <version>4.6.0_1</version>
<dependency>

陷阱

在执行brew install opencv --build-from-source命令时,我在安装过程中遇到了too many open files问题。为了解决我必须像So -ulimit -n 2048一样增加ulimit