java实现OpenCV 4.1.0人脸相似度对比

需要opencv的jar,从opencv的安装路径中可以获取。 

 

package com.ahies.dit.management.util;


import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

import java.util.Arrays;

/**
 * 1.  灰度化(减小图片大小)
 * 2. 人脸识别
 * 3. 人脸切割
 * 4. 规一化(人脸直方图)
 * 5. 直方图相似度匹配
 *
 */
public class FaceCompare {

    // 初始化人脸探测器
    static CascadeClassifier faceDetector;

    static {
        //路径不能包含中文  linux使用libopencv_java410.so
        String opencvDllName = "D:\\opencv\\opencv\\build\\java\\x64\\opencv_java410.dll";
        System.load(opencvDllName);
        // xml路径不能包含中文
        faceDetector = new CascadeClassifier("d:\\haarcascade_frontalface_alt.xml");
    }


    // 灰度化人脸
    public static Mat conv_Mat(String img) {
        Mat image0 = Imgcodecs.imread(img);
        Mat image1 = new Mat();
        // 灰度化
        Imgproc.cvtColor(image0, image1, Imgproc.COLOR_BGR2GRAY);
        // 探测人脸
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image1, faceDetections);
        // rect中人脸图片的范围
        for (Rect rect : faceDetections.toArray()) {
            Mat face = new Mat(image1, rect);
            return face;
        }
        return null;
    }

    public static double compare_image(String img_1, String img_2) {
        Mat mat_1 = conv_Mat(img_1);
        Mat mat_2 = conv_Mat(img_2);
        Mat hist_1 = new Mat();
        Mat hist_2 = new Mat();

        //颜色范围
        MatOfFloat ranges = new MatOfFloat(0f, 256f);
        //直方图大小, 越大匹配越精确 (越慢)
        MatOfInt histSize = new MatOfInt(10000000);

        Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
        Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);

        // CORREL 相关系数
        double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
        return res;
    }

    public static void main(String[] args) {
        //图片路径不能包含中文
        String basePicPath = "d:\\face\\";
        double compareHist = compare_image(basePicPath + "10.jpg", basePicPath + "11.jpg");
        System.out.println(compareHist);
        if (compareHist > 0.72) {
            System.out.println("人脸匹配");
        } else {
            System.out.println("人脸不匹配");
        }
    }
}


 

你可能感兴趣的:(Java和Jvm)