opencv人脸识别

opencv人脸识别

1.官网下载opencv:https://opencv.org/releases/
在这里插入图片描述
2.双击打开生成文件夹opencv,
在这里插入图片描述
3.创建项目
4.将opencv-454.jar和opencv_java454.dll复制到项目中
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
5.项目中引入jar
在这里插入图片描述
6.将人脸识别的类库映入项目
在这里插入图片描述
在这里插入图片描述
7.测试代码

package liu.com.dome;


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

import java.util.Arrays;

public class FaceAi {

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

    static int i = 0;

    static {
        String path=System.getProperty("user.dir")+"/opencv/opencv_java454.dll";
        System.load(path);
        faceDetector = new CascadeClassifier(System.getProperty("user.dir")+"/opencv/haarcascade_frontalface_alt.xml");
    }

    public static void main(String[] args) {
    //人脸识别后抠图
    //   getFace("F:/123.png","123_1.jpeg");
    //人脸识别后抠图
    //    getFace("F:/1234.jpg","1234_1.jpeg");
    
    //相似度对比
        double v = compare_image("F:/0123_1.jpeg", "F:/01234_1.jpeg");
    }


    /**
     * 人脸识别
     * @param imagePath  图片路径
     * @param outFile    处理后的图片路径
     */
    public static void getFace(String imagePath,String outFile) {
        // 原始图像
        Mat image = Imgcodecs.imread(imagePath);
        System.out.println("图片长-宽:"+image.rows()+"-"+image.cols());
        //创建一个矩形框住人脸
        MatOfRect matOfRect = new MatOfRect();
        //执行人脸检测
        faceDetector.detectMultiScale(image,matOfRect);
        //识别出人脸
        Rect[] rects = matOfRect.toArray();
        System.out.println("识别出人脸的数量"+rects.length);

        for (int i=0;i<rects.length;i++){
            Imgproc.rectangle(image,
                    new Point(rects[i].x,rects[i].y),//框的起点
                    new Point(rects[i].x+rects[i].width,rects[i].y+rects[i].height),//框的宽,框的高
                    new Scalar(255,255,255)//框的颜色
            );
            //保存识别后的图片
            Imgcodecs.imwrite("F:/"+outFile, image);
            //把检测到的人脸抠出保存
            imageCut(imagePath,"F:/"+i+outFile,rects[i].x,rects[i].y,rects[i].width,rects[i].height);
        }

        System.out.println("成功检测到人脸在"+"F:/"+outFile+"路径上");
    }

    /**
     * 裁剪人脸
     *
     * @param imagePath   图片路径
     * @param outFile   抠图后的图片路径
     * @param posX    抠图坐标
     * @param posY
     * @param width   抠图的宽
     * @param height  抠图的高
     */
    public static void imageCut(String imagePath, String outFile, int posX, int posY, int width, int height) {
        // 原始图像
        Mat image = Imgcodecs.imread(imagePath);

        // 截取的区域:参数,坐标X,坐标Y,截图宽度,截图长度
        Rect rect = new Rect(posX, posY, width, height);
        Mat sub = image.submat(rect);
        Mat mat = new Mat();
        Size size = new Size(300, 300); //指定抠出来的图标尺寸
        Imgproc.resize(sub, mat, size); // 将人脸进行截图并保存
        Imgcodecs.imwrite(outFile, mat);
        System.out.println(String.format("图片裁切成功,裁切后图片文件为", outFile));
    }

    /**
     * 人脸比对
     *
     * @param img_1
     * @param img_2
     * @return
     */
    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(1000);

        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;
    }

    /**
     * 灰度化人脸
     *
     * @param img
     * @return
     */
    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;
    }
}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桀骜浮沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值