Linux中OpenCV安装及在Intellij Idea中Java调用

安装OpenCV

1.安装依赖包

OpenCV依赖如下包:

GCC 4.4.x or later
CMake 2.6 or higher
Git
GTK+2.x or higher, including headers (libgtk2.0-dev)
pkg-config
Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
[optional] libtbb2 libtbb-dev
[optional] libdc1394 2.x
[optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev

在终端中执行如下命令安装依赖包:

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

2.获取OpenCV源码

github地址:https://github.com/opencv/opencv
官网地址:http://opencv.org/downloads.html

除Windows以外,其他的系统都需要手动源码编译。

3.编译OpenCV源码

  • 解压压缩包到~/opencv-2.4.13文件夹
  • 进入安装目录执行如下命令
    cd ~/opencv-1.2.4
    mkdir release
    cd release
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
    Linux中OpenCV安装及在Intellij Idea中Java调用_第1张图片
    依赖包安装完成

    Linux中OpenCV安装及在Intellij Idea中Java调用_第2张图片
    release文件夹中产生很多文件

    Linux中OpenCV安装及在Intellij Idea中Java调用_第3张图片

  • 进入release目录执行如下命令
    make
    sudo make install
    执行make命令

    Linux中OpenCV安装及在Intellij Idea中Java调用_第4张图片

    make命令执行完成(可能花半个多小时,中途还可能出现错误)

    Linux中OpenCV安装及在Intellij Idea中Java调用_第5张图片

    执行sudo make install

    Linux中OpenCV安装及在Intellij Idea中Java调用_第6张图片

  • 等待编译完成,添加库路径及更新

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

    安装完成后release/bin目录如下:

    Linux中OpenCV安装及在Intellij Idea中Java调用_第7张图片

在IntellijIdea中使用OpenCV

  1. 建立Java项目
  2. 在Project Structure->Libraries中导入依赖包opencv-2413.jar(在~/release/bin下)
    Linux中OpenCV安装及在Intellij Idea中Java调用_第8张图片
  3. 编写Java测试代码

    package org.fang.test;
    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.core.MatOfRect;
    import org.opencv.core.Point;
    import org.opencv.core.Rect;
    import org.opencv.core.Scalar;
    import org.opencv.highgui.Highgui;
    import org.opencv.objdetect.CascadeClassifier;
    
    /*
     * Detects faces in an image, draws boxes around them, and writes the results
     * to "faceDetection.png".
     */
    public class DetectFaceDemo {
        public void run() {
            System.out.println("\nRunning DetectFaceDemo");
    
            // Create a face detector from the cascade file in the resources
            // directory.
            CascadeClassifier faceDetector = new CascadeClassifier(getClass()
                    .getResource("/lbpcascade_frontalface.xml").getPath());
            Mat image = Highgui.imread(getClass().getResource(
                    "/AverageMaleFace.jpg").getPath());
    
            // Detect faces in the image.
            // MatOfRect is a special container class for Rect.
            MatOfRect faceDetections = new MatOfRect();
            faceDetector.detectMultiScale(image, faceDetections);
    
            System.out.println(String.format("Detected %s faces",
                    faceDetections.toArray().length));
    
            // Draw a bounding box around each face.
            for (Rect rect : faceDetections.toArray()) {
                Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
                        + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
            }
    
            // Save the visualized detection.
            String filename = "faceDetection.png";
            System.out.println(String.format("Writing %s", filename));
            Highgui.imwrite(filename, image);
        }
    }
    
  4. 提交运行需要在Run/Debug Donfigurations中VM Options添加
    -Djava.library.path=/home/fang/SoftWare/opencv-2.4.13/release/lib
    Linux中OpenCV安装及在Intellij Idea中Java调用_第9张图片
  5. 运行结果

    Linux中OpenCV安装及在Intellij Idea中Java调用_第10张图片


The End

你可能感兴趣的:(java,OpenCV)