毕业设计——基于JAVA+OpenCV的人脸识别项目

完整项目,私信博主获取

功能

  1. 人脸检测、识别(图片、视频)
  2. 更换背景图(例如给任务换一张海景背景图)
  3. 图片合成(多张照片合成)
  4. 证件照更换背景色
  5. 年龄识别
  6. 性别识别
  7. 图片修复(可用于水印去除)
  8. 图片分类
  9. 老照片
  10. 素描
  11. 图像色温调整
  12. 图像对比度调整
  13. 图像高光调整
  14. 图像明度调整
  15. 图像饱和度调整
  16. 图像阴影调整
  17. 天空滤镜
  18. 人脸检测、识别(Dlib版本)
  19. 暗通道去雾算法
  20. 中值滤波去雾算法
  21. 人脸美颜磨皮算法

开发环境

  • Windows 10(x64)

  • Java 1.8

  • OpenCV 3.4.3

  • JavaCV 1.4.3

  • Dlib 仅仅支持 Linux Or MacOS

例子

图片人脸检测

package com.biubiu.example;


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

import java.math.BigDecimal;

import static org.bytedeco.javacpp.opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING;

public class FaceDetect {
   
   

    static {
   
   
        // 加载 动态链接库
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args) {
   
   

        String filepath = "/home/yinyue/opencv/test.JPG";
        Mat srcImg = Imgcodecs.imread(filepath);
        // 目标灰色图像
        Mat dstGrayImg = new Mat();
        // 转换灰色
        Imgproc.cvtColor(srcImg, dstGrayImg, Imgproc.COLOR_BGR2GRAY);
        // OpenCv人脸识别分类器
        CascadeClassifier classifier = new CascadeClassifier("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml");
        // 用来存放人脸矩形
        MatOfRect faceRect = new MatOfRect();
        // 特征检测点的最小尺寸
        Size minSize = new Size(32, 32);
        // 图像缩放比例,可以理解为相机的X倍镜
        double scaleFactor = 1.2;
        // 对特征检测点周边多少有效检测点同时检测,这样可以避免选取的特征检测点大小而导致遗漏
        int minNeighbors = 3;
        // 执行人脸检测
        classifier.detectMultiScale(dstGrayImg, faceRect, scaleFactor, minNeighbors, CV_HAAR_DO_CANNY_PRUNING, minSize);
        //遍历矩形,画到原图上面
        // 定义绘制颜色
        Scalar color = new Scalar(0, 0, 255);
        for(Rect rect: faceRect.toArray()) {
   
   
            int x = rect.x;
            int y = rect.y;
            int w = rect.width;
            int h = rect.height;
            // 单独框出每一张人脸
            Imgproc.rectangle(srcImg, new Point(x, y), new Point(x + h, y + w), color, 2);
            // 左眼
            Imgproc.circle(srcImg, new Point(x + Math.floor(getDivideDouble(w, 4)), y + Math.floor(getDivideDouble(h, 4)) + 15), Math.min(getDivideInt(h, 8), getDivideInt(w, 8)), color);
            // 右眼
            Imgproc.circle(srcImg, new Point(x + 3 * Math.floor(getDivideDouble(w, 4)), y + Math.floor(getDivideDouble(h, 4)) + 15), Math.min(getDivideInt(h, 8), getDivideInt(w, 8)), color);
            // 嘴巴
            Imgproc.rectangle(srcImg, new Point(x + 3 * Math.floor(getDivideDouble(w, 8)), y + 3 * Math.floor(getDivideDouble(h, 4)) - 5), new Point(x + 5 * Math.floor(getDivideDouble(w, 8)) + 10, y + 7 * Math.floor(getDivideDouble(h, 8))), color, 2);
        }
        HighGui.imshow("预览", srcImg);
        // 显示图像
        HighGui.waitKey(0) ;
        // 释放所有的窗体资源
        HighGui.destroyAllWindows();

    }


    /**
     * 图片转换成灰色(降低为一维的灰度,减低计算强度)
     * @param path
     * @return
     */
    private static Mat transferToGray(String path) {
   
   
        // 读取图片
        Mat srcImg = Imgcodecs.imread(path);
        // 目标灰色图像
        Mat dstGrayImg = new Mat();
        // 转换灰色
        Imgproc.cvtColor(srcImg, dstGrayImg, Imgproc.COLOR_BGR2GRAY);

        return dstGrayImg;
    }

    /**
     * 在图片上画矩形
     * @param path
     */
    private static void drawRect(String path) {
   
   
        // 读取图片
        Mat srcImg = Imgcodecs.imread(path);
        // 目标灰色图像
        Mat dstGrayImg = new Mat();
        // 转换灰色
        Imgproc.cvtColor(srcImg, dstGrayImg, Imgproc.COLOR_BGR2GRAY);
        // 坐标
        double x = 10, y = 10;
        //  矩形大小(宽、高)
        double w = 100;
        // 定义绘制颜色
        Scalar color = new Scalar(0, 0, 255);
        Imgproc.rectangle(srcImg, new Point(x, y), new Point(x + w, y + w), color, 1);
        HighGui.imshow("预览", srcImg);
        // 显示图像
        HighGui.waitKey(0);
        // 释放所有的窗体资源
        HighGui.destroyAllWindows();

    }


    /**
     * 计算除法
     * @param a
     * @param b
     * @return
     */
    private static double getDivideDouble(int a, int b) {
   
   
        return new BigDecimal(a).divide(new BigDecimal(b), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    /**
     * 计算除法
     * @param a
     * @param b
     * @return
     */
    private static int getDivideInt(int a, int b) {
   
   
        return new BigDecimal(a).divide(new BigDecimal(b), 2, BigDecimal.ROUND_HALF_UP).intValue();
    }



}

视频人脸检测

package com.biubiu.example;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;

import static org.bytedeco.javacpp.opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING;

/**
 * 视频人脸检测
 */
public class VideoDetect {
   
   

    static {
   
   
        // 加载 动态链接库
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args) {
   
   
        VideoCapture camera = new VideoCapture();
        // 参数0表示,获取第一个摄像头。
        camera.open(0);
        // 图像帧
        Mat frame = new Mat();
        for(;;) {
   
   
            camera.read(frame);
            draw(frame)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

毕业小助手

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

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

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

打赏作者

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

抵扣说明:

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

余额充值