Recent Changes - Search:

edit SideBar

OpenCV

Java

For the Reactables project, we had a heck of a time getting OpenCV to work with Java under Mac OS X. IIRC - the problem was that compiling the C side of OpenCV was difficult.

However, that has changed

OpenCV Java Dev Intro

Building OpenCV Under RHEL

See http://www.giuseppeurso.eu/en/how-to-compile-opencv-on-centos-with-java-support/

Update the above instructions for more recent versions of the software:

yum install -y gcc gcc-c++ gtk+-devel libjpeg-devel libtiff-devel jasper-devel libpng-devel zlib-devel cmake
yum install eigen3-devel
yum install git gtk2-devel pkgconfig numpy python python-pip python-devel gstreamer-plugins-base-devel libv4l ffmpeg-devel mplayer mencoder flvtool2
cmake --version
pip install numpy
yum install autoconf automake gcc gcc-c++ git libtool make nasm pkgconfig zlib-devel
mkdir ~/ffmpeg_sources
cd ~/ffmpeg_sources/
curl -O http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install
make distclean
export "PATH=$PATH:$HOME/bin"
cd ..
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg/
export PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --enable-shared
make
make install
hash -r
. ~/.bash_profile
./ffmpeg -version
wget https://github.com/Itseez/opencv/archive/3.1.0.zip
unzip 3.1.0.zip
cd opencv-3.1.0/
mkdir build
cd build
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_65
export ANT_HOME=/usr/local/apache-ant
cmake -DBUILD_SHARED_LIBS=OFF ../
make >& make.out

RHEL: -lavcodec not found

FIXME: It turns out that this step might not be necessary because we need shared versions of these libraries for Java

While running make in OpenCV:

[ 36%] Building CXX object modules/core/CMakeFiles/opencv_perf_core.dir/perf/perf_minmaxloc.cpp.o
Linking CXX executable ../../bin/opencv_perf_core
/usr/bin/ld: cannot find -lavcodec
collect2: ld returned 1 exit status
make[2]: *** [bin/opencv_perf_core] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/all] Error 2
make: *** [all] Error 2

The fix was to create links in /usr/local/lib:

mv /root/ffmpeg_build/lib /usr/local/ffmpeg
cd /usr/local/lib
ln -s ../ffmpeg/lib/* .
cd pkgconfig
ln -s ../../ffmpeg/lib/pkgconfig/lib* .

The restart make

RHEL: After running make, there are no jars.

After running make in the OpenCV build directory, no jars are created.

Reviewing the log shows:

RHEL: Could not find or load main class org.apache.tools.ant.launch.Launcher

-- Could NOT find PythonInterp: Found unsuitable version "2.7.9", but required is at least "3.2" (found /usr/local/python/bin/python)
Error: Could not find or load main class org.apache.tools.ant.launch.Launcher
-- Found JNI: /usr/lib/jvm/jdk1.8.0_65/jre/lib/amd64/libjawt.so

ant does not work:

[root@terra build]# ant --version
Error: Could not find or load main class org.apache.tools.ant.launch.Launcher
[root@terra build]#

Solution:

export ANT_HOME=/usr/local/apache-ant
cmake -DBUILD_SHARED_LIBS=OFF ../
make >& make2.out

RHEL: libavcodec.a needs to be a shared library.

[ 88%] Building CXX object modules/java/CMakeFiles/opencv_java.dir/calib3d.cpp.o
Linking CXX shared module ../../lib/libopencv_java310.so
/usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libavcodec.a: could not read symbols: Bad value
collect2: ld returned 1 exit status

We need shared versions of the ffmpeg libraries.

The solution is to add --enable-shared to the configure command above.

Building OpenCV Under Mac OS X Using Mac Ports

Last Updated: 2016-Mar-29

The summary is to use MacPorts:

  sudo port install opencv +python27 +java

These two files will be created. Note that on 2016-Mar-29, these are OpenCV-3.0.0 files, not OpenCV-3.1.0 files. OpenCV-3.1.0 is the most recent release.

  • /opt/local/share/OpenCV/java/opencv-310.jar
  • /opt/local/share/OpenCV/java/libopencv_java310.so

Add the dylib to your DYLD_LIBRARY_PATH

  export DYLD_LIBRARY_PATH=/opt/local/share/OpenCV/java:${DYLD_LIBRARY_PATH}

As per OpenCV Java Dev Intro, create SimpleSample.java

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

class SimpleSample {

  static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

  public static void main(String[] args) {
    System.out.println("Welcome to OpenCV " + Core.VERSION);
    Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
    System.out.println("OpenCV Mat: " + m);
    Mat mr1 = m.row(1);
    mr1.setTo(new Scalar(1));
    Mat mc5 = m.col(5);
    mc5.setTo(new Scalar(5));
    System.out.println("OpenCV Mat data:\n" + m.dump());
  }

}

However, the test code above uses System.loadLibrary(), which searches the paths in the java.library.path property. So, we need to copy the shared library for this example on the Mac. Note that we need to change the extension from .so to .dylib:

  cp /opt/local/share/OpenCV/java/libopencv_java310.so ~/Library/Java/Extensions/lib/java/libopencv_java310.dylib

Compile with:

  javac -classpath /opt/local/share/OpenCV/java/opencv-310.jar SimpleSample.java

Run with:

  java -classpath /opt/local/share/OpenCV/java/opencv-310.jar:. SimpleSample

Below is a sample run:

bash-3.2$ find /opt/local -name "opencv*.jar"
/opt/local/share/OpenCV/java/opencv-310.jar
bash-3.2$ find /opt/local -name "*opencv_java*"
/opt/local/share/OpenCV/java/libopencv_java310.so
find: /opt/local/var/macports/software/omlib-buildsyspro-2015.12/mpextracthgOX21nT: Permission denied
bash-3.2$ cp /opt/local/share/OpenCV/java/libopencv_java310.so ~/Library/Java/Extensions/libopencv_java310.dylib
bash-3.2$ javac -classpath /opt/local/share/OpenCV/java/opencv-310.jar SimpleSample.java
bash-3.2$ java -classpath /opt/local/share/OpenCV/java/opencv-310.jar:. SimpleSample
Welcome to OpenCV 3.1.0
OpenCV Mat: Mat [ 5*10*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x7fdc51c592b0, dataAddr=0x7fdc51c59320 ]
OpenCV Mat data:
[  0,   0,   0,   0,   0,   5,   0,   0,   0,   0;
   1,   1,   1,   1,   1,   5,   1,   1,   1,   1;
   0,   0,   0,   0,   0,   5,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   5,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   5,   0,   0,   0,   0]
bash-3.2$

Building from OpenCV 3.1.0 sources under Mac OS X

If you don't want to use port as above for Mac OS X, here's how to build the Java sources:

  1. Download OpenCV
  2. Unzip
  3. If necessary, install cmake
    1. Under Mac OS X, install MacPorts and then do: sudo port install cmake
  4. Configure:
    cd opencv-3.0.0
    mkdir build
    cd build
    cmake -DBUILD_SHARED_LIBS=OFF ..
    http://docs.opencv.org/2.4/doc/tutorials/introduction/desktop_java/java_dev_intro.html#java-dev-intro says: "When OpenCV is built as a set of static libraries (-DBUILD_SHARED_LIBS=OFF option) the Java bindings dynamic library is all-sufficient, i.e. doesn’t depend on other OpenCV libs, but includes all the OpenCV code inside.
  5. Build

bin/opencv-310.jar will be created.

lib/libopencv_java310.so will be created. FIXME:Note that the extension is .so, not .dylib or .jnilib The file will probably need to be renamed.

Failed attempt at building using sources under Mac OS X

  1. Download OpenCV
  2. Unzip
  3. If necessary, install cmake
    1. Under Mac OS X, install MacPorts and then do: sudo port install cmake
  4. Configure:
    cd opencv-4.1.2
    mkdir build
    cd build
    cmake -DBUILD_SHARED_LIBS=OFF ..
    "
  5. Build:
    make -j8

Problems

libopencv_features2d.a(dynamic.cpp.o) has no symbols

[ 44%] Built target opencv_ts
Linking CXX static library ../../lib/libopencv_features2d.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../../lib/libopencv_features2d.a(dynamic.cpp.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../../lib/libopencv_features2d.a(dynamic.cpp.o) has no symbols
[ 44%] Built target opencv_features2d
make: *** [all] Error 2
 

Fail.

Running make without the -j8 arg:

[ 40%] Linking CXX executable ../../bin/opencv_perf_core
ld: library not found for -lavcodec
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Fail again.

Using MacPorts below seems to help with Python

Facial Recognition

Face Recognition With Python, in Under 25 Lines of Code

Installation of OpenCV under Mac OS X

Use MacPort!

  sudo port install opencv

Unfortunately, that does not install the Python Bindings

sh-3.2# /opt/local/bin/python2.7
Python 2.7.10 (default, May 25 2015, 13:05:06)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>>

Rerun:

   sudo port install opencv +python27

That worked:

bash-3.2$ /opt/local/bin/python2.7
/opt/local/bin/python2.7
Python 2.7.10 (default, May 25 2015, 13:05:06)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
import cv2
>>> quit()
quit()
bash-3.2$

Check out the FaceDetect repo:

git clone https://github.com/shantnu/FaceDetect/
cd FaceDetect
bash-3.2$ /opt/local/bin/python2.7 face_detect.py abba.png haarcascade_frontalface_default.xml
libpng warning: iCCP: profile 'Photoshop ICC profile': 'GRAY': Gray color space not permitted on RGB PNG
Traceback (most recent call last):
  File "face_detect.py", line 21, in <module>
    flags = cv2.cv.CV_HAAR_SCALE_IMAGE
AttributeError: 'module' object has no attribute 'cv'
bash-3.2$

Solution, edit face_detect.py and replace

    flags = cv2.cv.CV_HAAR_SCALE_IMAGE

    flags = cv2.CASCADE_SCALE_IMAGE

See http://stackoverflow.com/questions/30013009/opencv-3-0-0-dev-python-bindings-not-working-properly

Edit - History - Print - Recent Changes - Search
Page last modified on December 02, 2019, at 04:30 PM