官方下载opencv安装文件: http://opencv.org/releases.html,以windows版本为例,下载opencv-3.1.0.exe
安装后,在build目录下 D:\opencv\opencv\build\java,获取opencv-310.jar,copy至项目opncv目录(需要新建)
同时需要dll文件 与 各识别xml文件,进行不同特征的识别(人脸,侧脸,眼睛等)
dll目录: D:\opencv\opencv\build\java\x64\opencv_java2413.dll (dll库)
xml目录:D:\opencv\opencv\sources\data\haarcascades\haarcascade_frontalface_alt.xml(目录中有各类识别文件)
下面给出图片的各种操作的源码和运行结果,源码中有各个操作的解释,看代码就可以理解,java代码如下
package com.zmx.opencvtest;import org.opencv.core.*;import org.opencv.core.Point;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;import org.opencv.objdetect.CascadeClassifier;import javax.imageio.ImageIO;import javax.swing.*;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;/** * Created by Administrator on 2017/8/17. */public class DetectFaceTest { static{ // 载入opencv的库 String opencvpath = System.getProperty("user.dir") + "\\opencv\\x64\\"; String opencvDllName = opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll"; System.load(opencvDllName); } /** * opencv实现人脸识别 * @param imagePath * @param outFile * @throws Exception */ public static void detectFace(String imagePath, String outFile) throws Exception { System.out.println("Running DetectFace ... "); // 从配置文件lbpcascade_frontalface.xml中创建一个人脸识别器,该文件位于opencv安装目录中 CascadeClassifier faceDetector = new CascadeClassifier( "D:\\opencv\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml"); Mat image = Imgcodecs.imread(imagePath); // 在图片中检测人脸 MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); Rect[] rects = faceDetections.toArray(); if(rects != null && rects.length > 1){ throw new RuntimeException("超过一个脸"); } // 在每一个识别出来的人脸周围画出一个方框 Rect rect = rects[0]; Imgproc.rectangle(image, new Point(rect.x-2, rect.y-2), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); Imgcodecs.imwrite(outFile, image); System.out.println(String.format("人脸识别成功,人脸图片文件为: %s", outFile)); } /** * opencv实现人眼识别 * @param imagePath * @param outFile * @throws Exception */ public static void detectEye(String imagePath, String outFile) throws Exception { CascadeClassifier eyeDetector = new CascadeClassifier( "D:\\opencv\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml"); Mat image = Imgcodecs.imread(imagePath); //读取图片 // 在图片中检测人脸 MatOfRect faceDetections = new MatOfRect(); eyeDetector.detectMultiScale(image, faceDetections, 2.0,1,1,new Size(20,20),new Size(20,20)); System.out.println(String.format("Detected %s eyes", faceDetections.toArray().length)); Rect[] rects = faceDetections.toArray(); if(rects != null && rects.length <2){
throw new RuntimeException("不是一双眼睛"); } Rect eyea = rects[0]; Rect eyeb = rects[1]; System.out.println("a-中心坐标 " + eyea.x + " and " + eyea.y); System.out.println("b-中心坐标 " + eyeb.x + " and " + eyeb.y); //获取两个人眼的角度 double dy=(eyeb.y-eyea.y); double dx=(eyeb.x-eyea.x); double len=Math.sqrt(dx*dx+dy*dy); System.out.println("dx is "+dx); System.out.println("