Novembre 21, 2024

Compilare il wrapper Java di OpenCV su CentOS

OpenCV è una libreria open source molto utilizzata in progetti che si occupano di computer vision. OpenCV nasce con l’obiettivo di fornire un modello e un’infrastruttura di base per applicazioni dove è necessario acquisire, analizzare, processare e manipolare immagini.
OpenCV è scritta nativamente in C++ ma mette a disposizione API in C, Python, Java e Matlab con supporto multipiattaforma per sistemi Windows, Linux, Android e MacOS. Non mi dilungo su OpenCV ulteriori dettagli si trovano sul sito uffciale. In questo articolo riporto la procedura per generare il wrapper Java di OpenCV su CentOS a partire dal sorgente.

APPLICATION STACK

– SO CentOS 6.5 x86_64 Minimal
– OpenCV source 2.4.9
– JDK 1.7.0_60
– ANT 1.7.1
– FFMPEG git-2014-07-09

### Required packages
$ 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
$ yum install cmake28
$ yum install ant
$ pip install numpy
### Setup JDK from Oracle
$ wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u60-b19/jdk-7u60-linux-x64.tar.gz

### Untar jdk in root directory
$ tar -zxf jdk*.tar.gz
### Setup FFMPEG from source
$ 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.2.0.tar.gz

$ tar xzvf yasm-1.2.0.tar.gz
$ cd yasm-1.2.0
$ ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
$ make
$ make install
$ make distclean
$ export "PATH=$PATH:$HOME/bin"

$ cd ~/ffmpeg_sources
$ git clone --depth 1 git://source.ffmpeg.org/ffmpeg
$ cd ffmpeg
$ PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
$ export PKG_CONFIG_PATH
$ ./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="$HOME/bin" --extra-libs=-ldl
$ make
$ make install
$ make distclean
$ hash -r
$ . ~/.bash_profile

$ ./ffmpeg -version
ffmpeg version git-2014-07-09-d6e7881 Copyright (c) 2000-2014 the FFmpeg developers
built on Jul  9 2014 13:10:23 with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-4)
configuration: --prefix=/root/ffmpeg_build --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --bindir=/root/bin --extra-libs=-ldl
libavutil      52. 91.100 / 52. 91.100
libavcodec     55. 68.102 / 55. 68.102
libavformat    55. 45.100 / 55. 45.100
libavdevice    55. 13.102 / 55. 13.102
libavfilter     4. 10.100 /  4. 10.100
libswscale      2.  6.100 /  2.  6.100
libswresample   0. 19.100 /  0. 19.100
# Compiling OpenCV
$ wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip?r=http%3A%2F%2Fopencv.org%2Fdownloads.html&ts=1406043986&use_mirror=skylink
$ unzip opencv-2.4.9.zip
$ cd opencv-2.4.9

$ export JAVA_HOME=/root/jdk1.7.0_07

$ mkdir build
$ cd build

### !!! PAY ATTENTION TO THIS: I use cmake28 instead of cmake
$ cmake28 -DBUILD_SHARED_LIBS=OFF ../

giuseppe-urso-how-to-compile-opencv-on-CentOS-for-Java-support

# Here is some other important output messages
Found PythonInterp: /usr/bin/python2 (found suitable version "2.6.6", minimum required is "2.0") 
-- Update variable PYTHON_LIBRARY from environment: /usr/lib64/libpython2.6.so.1.0
-- Found PythonLibs: /usr/lib64/libpython2.6.so.1.0 (found suitable exact version "2.6.6") 
-- Found apache ant 1.7.1: /usr/bin/ant
-- Found JNI: /root/jdk1.7.0_60/jre/lib/amd64/libjawt.so  
-- Performing Test HAVE_CXX_WNO_DEPRECATED_DECLARATIONS
-- Performing Test HAVE_CXX_WNO_DEPRECATED_DECLARATIONS - Success

[...]

-- Java:
--     ant:                         /usr/bin/ant (ver 1.7.1)
--     JNI:                         /root/jdk1.7.0_60/include /root/jdk1.7.0_60/include/linux /root/jdk1.7.0_60/include
--     Java tests:                  NO

 

# Start the OpenCV build
$ make
#  When OpenCV is succesfully built you can find the jar Java interface and the native dynamic library 
$ ls -l build/lib
cv2.so
libopencv_java249.so

$ ls -l build/bin
opencv-249.jar

 

Related posts

Leave a Reply

Your email address will not be published.