Jade Dungeon

OpenCV 安装

linux

安装依赖

卸载任何以前安装的ffmpeg和x264软件

sudo apt-get -qq remove ffmpeg x264 libx264-dev

安装一些依赖关系

来自kumaranbus的一个安装反馈,在非英伟达显卡的机器上安装「libopencv-dev」依赖的时候会破坏机器的驱动。要是你使用的是非英伟达的显卡像AMD/ATI, Radeon系列显卡,请把上面的依赖名称替换成「ocl-icd-libopencl1」它是一个通用的驱动。请注意:本人没用亲自去试验这个方法的可行性,因为我没有ATI/Radeon显卡的机器。

sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils 

安装ffmpeg

sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get install ffmpeg gstreamer0.10-ffmpeg

编译安装OpenCV

wget -O OpenCV-2.4.9.zip http://fossies.org/linux/misc/opencv-2.4.9.zip

unzip OpenCV-2.4.9.zip

cd opencv-2.4.9

mkdir build

cd build

cmake .. -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D CMAKE_BUILD_TYPE=RELEASE               \
  -D WITH_TBB=ON                            \
  -D WITH_V4L=ON                            \
  -D WITH_QT=ON                             \
  -D WITH_OPENGL=ON                         \
  -D BUILD_NEW_PYTHON_SUPPORT=ON            \
  -D BUILD_EXAMPLES=ON                      \
  -D INSTALL_C_EXAMPLES=ON                  \
  -D INSTALL_PYTHON_EXAMPLES=ON

make -j2

sudo make install

由于网络问题,部分编译过程中需要下载的文件会下载失败。 我已经下载好了和源代码放一起。

配置库的地址

首先共享库文件一般在/usr/local/lib下可以找到,如果找到该共享库的话, 那么在/etc/ld.so.conf.d/xxxx.conf注意(xxxx.conf)是你自己命名的。 比如我缺少opencv库 那么我就写 opencv.conf。

接下来,就在xxxx.conf文件中写path路径, 比如说共享库在/usr/local/opencv/libopencv_core.so.2.4中的话, 你就写/usr/local/opencv/

sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig -v

检查是否安装成功

手动安装的测试方法:

mkdir test-example
cp /usr/local/share/OpenCV/samples/ .
cd test-example/samples/c/
bash ./build_all.sh  
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" lena.jpg

ubuntu软件源中安装后的测试方法:

Let's use a simple program such as DisplayImage.cpp shown below.

#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

Now you have to create your CMakeLists.txt file. It should look like this:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
Generate the executable

This part is easy, just proceed as with any other project using CMake:

cd <DisplayImage_directory>
cmake .
make
Result

By now you should have an executable (called DisplayImage in this case). You just have to run it giving an image location as an argument, i.e.:

./DisplayImage lena.jpg

Java调用OpenCV

  • /usr/local/share/OpenCV/java/opencv-2411.jar
  • /usr/local/share/OpenCV/java/opencv_java2411.so

eclipse

Eclipse中「add user library」把opencv-2411.jar添加进来。 然后在这个jar下面指定「Native library location」把opencv_java2411.so添加进来。

package testOpenCV;

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

public class HelloCV {
	public static void main(String[] args) {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
		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());
	}
}

输出结果:

Welcome to OpenCV 2.4.11.0
OpenCV Mat: Mat [ 5*10*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x7f7a8818ad70, dataAddr=0x7f7a8818ae30 ]
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]

Java Sample with Maven

把编译出来的jar包和dll文件放到本地的maven库中:

mvn install:install-file -Dfile=opencv-320.jar                            -Dpackaging=jar -DgroupId=net.jade-dungeon -DartifactId=opencv         -Dversion=3.2.0
mvn install:install-file -Dfile=opencv-runtime-natives-windows-x86_64.jar -Dpackaging=jar -DgroupId=net.jade-dungeon -DartifactId=opencv-runtime -Dversion=3.2.0 -Dclassifier=natives-windows-x86_64

在pom.xml中添加opencv的依赖如下:

<dependency>
    <groupId>org.opencv</groupId>
    <artifactId>opencv</artifactId>
    <version>2.4.11</version>
    <systemPath>/usr/local/share/OpenCV/java/opencv-2411.jar</systemPath>
    <scope>system</scope>
</dependency>

然后在加载动态链接库时,不使用System.loadLibrary(xxx);。 而是使用绝对路径加载:System.load(xxx);

将Demo中的System.loadLibrary(xxx);用如下的两句话替换,其他无需改变:

System.load("/usr/local/share/OpenCV/java/libopencv_java2411.so");