Ubuntu 下编译Opencv3.1.0, 使用IDEA下Java开发

1.编译过程可以参考下面的博客,

http://milq.github.io/install-opencv-ubuntu-debian/


Install OpenCV on Ubuntu or Debian

Note: This tutorial has been tested on Ubuntu 14.04 LTS and Debian 8.0 "Jessie", both with OpenCV 3.0.0. If you have successfully installed OpenCV following this tutorial with other OS or OpenCV version, please share your configuration. Thanks!

Install OpenCV on Ubuntu or Debian is a bit long but very easy. You can install OpenCV from the Ubuntu or Debian repository or from the official site.

OPTION 1: INSTALL OPENCV FROM THE UBUNTU OR DEBIAN REPOSITORY

You can install OpenCV from the Ubuntu or Debian repository:

sudo apt-get install libopencv-dev python-opencv

However, you will probably not have installed the latest version of OpenCV and you may miss some features (for example: Python 3 bindings do not exist in the repository).

OPTION 2: INSTALL OPENCV FROM THE OFFICIAL SITE

To install the latest version of OpenCV be sure that you have removed the library from the repository with sudo apt-get autoremove libopencv-dev python-opencv and follow the steps below.

If you want to do the steps below automatically, you can execute this script with bash.

2.1. KEEP UBUNTU OR DEBIAN UP TO DATE

Open your terminal and execute:

sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade && sudo apt-get autoremove

2.2. INSTALL THE DEPENDENCIES

Now install the dependencies:

Build tools:

sudo apt-get install build-essential cmake

GUI:

sudo apt-get install qt5-default libvtk6-dev

Media I/O:

sudo apt-get install zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libjasper-dev libopenexr-dev libgdal-dev

Video I/O:

sudo apt-get install libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev

Parallelism and linear algebra libraries:

sudo apt-get install libtbb-dev libeigen3-dev

Python:

sudo apt-get install python-dev python-tk python-numpy python3-dev python3-tk python3-numpy

Java:

sudo apt-get install ant default-jdk

Documentation:

sudo apt-get install doxygen

2.3. DOWNLOAD AND DECOMPRESS OPENCV

Enter the OpenCV official website and download the latest version for Linux. Then decompress the downloaded file.

2.4. COMPILE AND INSTALL OPENCV

In your terminal, make sure you are within the OpenCV directory and run the following commands:

mkdir build
cd build
cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=ON ..
make -j4
sudo make install

Now, configure OpenCV:

sudo ldconfig

Now, you have installed OpenCV. You can execute some examples builded and/or compile one demonstration.

2.5. EXECUTE SOME OPENCV EXAMPLES

Go to your OpenCV directory and execute a C++ example:

cd build/bin
./cpp-example-edge ../../samples/data/fruits.jpg

Now, go to your OpenCV directory and execute a Python example:

cd samples/python2
python video.py

Finally, go to your OpenCV directory and execute a Java example:

cd samples/java/ant
ant -DocvJarDir=../../../build/bin -DocvLibDir=../../../build/lib

2.6. COMPILE A DEMONSTRATION

Enabling Face Recognition when compiling OpenCV

To enable Face Recognition, you need to:

  • download the 3.1 version of the opencv_contrib: https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
  • extract the archive
  • open file opencv_contrib-3.1.0/modules/face/CMakeLists.txt
  • append "java" to the WRAP section, so that it reads: ocv_define_module(face opencv_core opencv_imgproc opencv_objdetect WRAP python java)
  • add the switch -D OPENCV_EXTRA_MODULES_PATH=$YOUR_OPENCV_CONTRIB_PATH/opencv_contrib-3.1.0/modules  to the compile command showed in the installation tutorial linked above


PS: 需要特别注意的是,为了生成Jar包,cmake的参数需要做一些变化:

添加参数D BUILD_opencv_java=ON BUILD_opencv_test_java=OFF, 并且需要安装ant才会生产jar包.

还要修改一下face模块的CmakeList文件(face opencv_core opencv_imgproc opencv_objdetect WRAP python java)


cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=ON -DBUILD_opencv_java=ON BUILD_opencv_test_java=OFF -D OPENCV_EXTRA_MODULES_PATH=/home/lwc/Documents/opencv-3.1.0/opencv_contrib-3.1.0/modules  ..

若没有用到contrib模块,OPENCV_EXTRA_MODULES_PATH参数不用添加

成功以后会在bin目录下生产Jar包,Native函数在lib文件夹下

2.Idea中配置OpenCV,使用Java开发

重要的一点是将bin目录的Jar包添加到工程,并在Jar包上添加lib文件夹为Native方法.

Ubuntu 下编译Opencv3.1.0, 使用IDEA下Java开发_第1张图片

用Java开发Opencv相关的教程确实太少了, 各种各样的坑,希望大家多多分享自己走过的坑.



你可能感兴趣的:(OpenCV)