Java使用OpenCV实现人脸识别/人眼检测/图片截取/合成/添加水印

本文介绍了如何在Java中利用OpenCV进行人脸识别、人眼检测、图片截取、合成以及添加水印的操作。详细阐述了安装OpenCV、添加相关依赖的过程,并提供了源码示例,展示了从原图到最终处理结果的步骤。实验结果显示,OpenCV的人脸检测效果良好,但人眼检测存在一些不足。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

               

官方下载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("
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值