OpenCV installation with Java on MAC OS Sierra

1.3k views Asked by At

Trying to install OpenCV3.1 for Java on MAC OS 10.12.2 (Sierra). Followed the commands from this link. I would like to install this so that I can configure Eclipse with OpenCV.

Couple of errors while installing (Tried more than once)

Attempt 1: brew install opencv3

Error Snippet

[ 55%] Built target opencv_imgcodecs make: *** [all] Error 2

Attempt 2: brew install opencv3 --HEAD --with-java

Error Snippet:

[ 58%] Linking CXX shared library ../../lib/libopencv_features2d.dylib cd /tmp/opencv3-20161216-63759-hur0m6/macbuild/modules/features2d && /usr/local/Cellar/cmake/3.7.1/bin/cmake -E cmake_link_script CMakeFiles/opencv_features2d.dir/link.txt --verbose=1 /usr/local/Homebrew/Library/Homebrew/shims/super/clang++ -I/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/include/darwin -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mavx -fvisibility=hidden -fvisibility-inlines-hidden -mf16c -DNDEBUG -DNDEBUG -dynamiclib -Wl,-headerpad_max_install_names -compatibility_version 3.1.0 -current_version 3.1.0 -o ../../lib/libopencv_features2d.3.1.0.dylib -install_name @rpath/libopencv_features2d.3.1.dylib CMakeFiles/opencv_features2d.dir/src/agast.cpp.o CMakeFiles/opencv_features2d.dir/src/agast_score.cpp.o CMakeFiles/opencv_features2d.dir/src/akaze.cpp.o CMakeFiles/opencv_features2d.dir/src/bagofwords.cpp.o CMakeFiles/opencv_features2d.dir/src/blobdetector.cpp.o CMakeFiles/opencv_features2d.dir/src/brisk.cpp.o CMakeFiles/opencv_features2d.dir/src/draw.cpp.o CMakeFiles/opencv_features2d.dir/src/dynamic.cpp.o CMakeFiles/opencv_features2d.dir/src/evaluation.cpp.o CMakeFiles/opencv_features2d.dir/src/fast.cpp.o CMakeFiles/opencv_features2d.dir/src/fast_score.cpp.o CMakeFiles/opencv_features2d.dir/src/feature2d.cpp.o CMakeFiles/opencv_features2d.dir/src/gftt.cpp.o CMakeFiles/opencv_features2d.dir/src/kaze.cpp.o CMakeFiles/opencv_features2d.dir/src/kaze/AKAZEFeatures.cpp.o CMakeFiles/opencv_features2d.dir/src/kaze/KAZEFeatures.cpp.o CMakeFiles/opencv_features2d.dir/src/kaze/fed.cpp.o CMakeFiles/opencv_features2d.dir/src/kaze/nldiffusion_functions.cpp.o CMakeFiles/opencv_features2d.dir/src/keypoint.cpp.o CMakeFiles/opencv_features2d.dir/src/main.cpp.o CMakeFiles/opencv_features2d.dir/src/matchers.cpp.o CMakeFiles/opencv_features2d.dir/src/mser.cpp.o CMakeFiles/opencv_features2d.dir/src/orb.cpp.o CMakeFiles/opencv_features2d.dir/opencl_kernels_features2d.cpp.o -Wl,-rpath,/tmp/opencv3-20161216-63759-hur0m6/macbuild/lib ../../lib/libopencv_flann.3.1.0.dylib ../../lib/libopencv_ml.3.1.0.dylib ../../lib/libopencv_highgui.3.1.0.dylib ../../../3rdparty/ippicv/unpack/ippicv_osx/lib/libippicv.a ../../lib/libopencv_videoio.3.1.0.dylib ../../lib/libopencv_imgcodecs.3.1.0.dylib ../../lib/libopencv_imgproc.3.1.0.dylib ../../lib/libopencv_core.3.1.0.dylib cd /tmp/opencv3-20161216-63759-hur0m6/macbuild/modules/features2d && /usr/local/Cellar/cmake/3.7.1/bin/cmake -E cmake_symlink_library ../../lib/libopencv_features2d.3.1.0.dylib ../../lib/libopencv_features2d.3.1.dylib ../../lib/libopencv_features2d.dylib [ 58%] Built target opencv_features2d make: *** [all] Error 2

The suggestions given on github for the issues didn't seem to fix the problem. Can someone help me out with this?

Also I tried installing openCV 2.4.13 using command brew install opencv which also threw the same error.

2

There are 2 answers

0
Srikkanth Govindaraajan On BEST ANSWER

I was finally able to install it. The problem was that Java requires ANT to be setup. Though I had ANT installed, the ANT_HOME was not set properly in my ENV. Once I set that properly I was able to resolve this installation issue.

0
j_rothmans On

I would like to install this so that I can configure Eclipse with OpenCV.

Adding for posterity of how I got opencv working in Eclipse. I was getting an error in Eclipse on this line,

import org.opencv.core.CvType;

because my Build > Config didn't have the opencv External JAR files. So I converted my Java project to Maven:

Right click project > Convert To Maven

Then follow these steps to add a Maven dependency: How do I add a Maven dependency in Eclipse?

and finally I added the bolded code to my pom.xml

<project
...

<dependencies>
<!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/opencv -->
<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>opencv</artifactId>
    <version>4.0.1-1.4.4</version>
</dependency>
</dependencies>

<modelVersion 
...

Then Right Click project > Run As > Java Application ... and it works!

Try this sample Java OpenCV program to check if yours works:

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class HelloCV {
        public static void main(String[] args){
                System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
                Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
                System.out.println("mat = " + mat.dump());
        }
}