首页 > 编程语言 >C#+OpenCV进阶(一)_人体识别

C#+OpenCV进阶(一)_人体识别

时间:2024-07-11 11:43:12浏览次数:20  
标签:gray 进阶 C# outMat OpenCV faces var new Mat

  效果排名:Lbp < Haar < CNN

1、Lbp

/// <summary>
/// Lbp人脸识别
/// </summary>
public static Mat FaceDetection_Lbp(Mat mat)
{
    var lbpCascade = new CascadeClassifier("model/lbpcascade_frontalface.xml");
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    using (var gray = new Mat())
    {
        Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);

        // 人脸识别
        Rect[] faces = lbpCascade.DetectMultiScale(
            gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));

        // Render all detected faces
        foreach (Rect face in faces)
        {
            // 画矩形
            outMat.Rectangle(face, Scalar.Red, 2);
        }
    }
    return outMat;
}

2、Haar

/// <summary>
/// Haar人脸识别1
/// haarcascade_frontalface_default.xml
/// </summary>
public static Mat FaceDetection_Haar(Mat mat)
{
    var haarCascade = new CascadeClassifier("model/haarcascade_frontalface_default.xml");
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    using (var gray = new Mat())
    {
        Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);

        // 人脸识别
        Rect[] faces = haarCascade.DetectMultiScale(
            gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));

        // Render all detected faces
        foreach (Rect face in faces)
        {
            // 画矩形
            outMat.Rectangle(face, Scalar.Red, 2);
        }
    }
    return outMat;
}

/// <summary>
/// Haar人脸识别2
/// haarcascade_profileface.xml
/// </summary>
public static Mat FaceDetection_HaarProfileFace(Mat mat)
{
    var haarCascade = new CascadeClassifier("model/haarcascade_profileface.xml");
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    using (var gray = new Mat())
    {
        Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);

        // 人脸识别
        Rect[] faces = haarCascade.DetectMultiScale(
            gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));

        // Render all detected faces
        foreach (Rect face in faces)
        {
            // 画矩形
            outMat.Rectangle(face, Scalar.Red, 2);
        }
    }
    return outMat;
}

/// <summary>
/// Haar眼睛识别
/// </summary>
public static Mat EyeDetection_Haar(Mat mat)
{
    var haarCascade = new CascadeClassifier("model/haarcascade_eye.xml");
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    using (var gray = new Mat())
    {
        Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);

        // 眼睛识别
        Rect[] faces = haarCascade.DetectMultiScale(
            gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));

        // Render all detected faces
        foreach (Rect face in faces)
        {
            // 画矩形
            outMat.Rectangle(face, Scalar.Red, 2);
        }
    }
    return outMat;
}
/// <summary>
/// Haar嘴巴识别
/// </summary>
public static Mat MouthDetection_Haar(Mat mat)
{
    var haarCascade = new CascadeClassifier("model/haarcascade_mcs_mouth.xml");
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    using (var gray = new Mat())
    {
        Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);

        // 眼睛识别
        Rect[] faces = haarCascade.DetectMultiScale(
            gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));

        // Render all detected faces
        foreach (Rect face in faces)
        {
            // 画矩形
            outMat.Rectangle(face, Scalar.Red, 2);
        }
    }
    return outMat;
}

/// <summary>
/// Haar微笑识别
/// </summary>
public static Mat SmileDetection_Haar(Mat mat)
{
    var haarCascade = new CascadeClassifier("model/haarcascade_smile.xml");
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    using (var gray = new Mat())
    {
        Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);

        // 眼睛识别
        Rect[] faces = haarCascade.DetectMultiScale(
            gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));

        // Render all detected faces
        foreach (Rect face in faces)
        {
            // 画矩形
            outMat.Rectangle(face, Scalar.Red, 2);
        }
    }
    return outMat;
}

/// <summary>
/// Haar全身识别
/// </summary>
public static Mat FullbodyDetection_Haar(Mat mat)
{
    var haarCascade = new CascadeClassifier("model/haarcascade_fullbody.xml");
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    using (var gray = new Mat())
    {
        Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);

        // 眼睛识别
        Rect[] faces = haarCascade.DetectMultiScale(
            gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));

        // Render all detected faces
        foreach (Rect face in faces)
        {
            // 画矩形
            outMat.Rectangle(face, Scalar.Red, 2);
        }
    }
    return outMat;
}
/// <summary>
/// Haar上身识别
/// </summary>
public static Mat UpperbodyDetection_Haar(Mat mat)
{
    var haarCascade = new CascadeClassifier("model/haarcascade_upperbody.xml");
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    using (var gray = new Mat())
    {
        Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);

        // 眼睛识别
        Rect[] faces = haarCascade.DetectMultiScale(
            gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));

        // Render all detected faces
        foreach (Rect face in faces)
        {
            // 画矩形
            outMat.Rectangle(face, Scalar.Red, 2);
        }
    }
    return outMat;
}
/// <summary>
/// Haar下身识别
/// </summary>
public static Mat LowerbodyDetection_Haar(Mat mat)
{
    var haarCascade = new CascadeClassifier("model/haarcascade_lowerbody.xml");
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    using (var gray = new Mat())
    {
        Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);

        // 眼睛识别
        Rect[] faces = haarCascade.DetectMultiScale(
            gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));

        // Render all detected faces
        foreach (Rect face in faces)
        {
            // 画矩形
            outMat.Rectangle(face, Scalar.Red, 2);
        }
    }
    return outMat;
}

3、CNN

/// <summary>
/// CNN人脸识别
/// </summary>
/// <returns></returns>
public static Mat FaceDetection_CNN(Mat mat)
{
    const string configFile = "model/deploy.prototxt";                               // Dnn参数
    const string faceModel = "model/res10_300x300_ssd_iter_140000_fp16.caffemodel";  // Dnn人脸模型
    Mat outMat = new Mat();
    mat.CopyTo(outMat);

    // 读图片
    int matHeight = outMat.Rows;
    int matWidth = outMat.Cols;
    using (var faceNet = CvDnn.ReadNetFromCaffe(configFile, faceModel))  // Dnn初始化
    {
        using (var blob = CvDnn.BlobFromImage(outMat, 1.0,
            new Size(300, 300), new Scalar(104, 117, 123), false, false))  // Dnn网络
        {
            faceNet.SetInput(blob, "data");  // 识别入参

            using (var detection = faceNet.Forward("detection_out"))
            {
                using (var detectionMat = new Mat(detection.Size(2), detection.Size(3), MatType.CV_32F, detection.Ptr(0)))
                {
                    for (int i = 0; i < detectionMat.Rows; i++)
                    {
                        float confidence = detectionMat.At<float>(i, 2);

                        if (confidence > 0.7)  // 识别概率>0.7时
                        {
                            // 框选人脸
                            int x1 = (int)(detectionMat.At<float>(i, 3) * matWidth);
                            int y1 = (int)(detectionMat.At<float>(i, 4) * matHeight);
                            int x2 = (int)(detectionMat.At<float>(i, 5) * matWidth);
                            int y2 = (int)(detectionMat.At<float>(i, 6) * matHeight);

                            Cv2.Rectangle(outMat,
                                new OpenCvSharp.Point(x1, y1), new OpenCvSharp.Point(x2, y2),
                                new Scalar(0, 255, 0), 2, LineTypes.Link4);
                        }
                    }
                    return outMat;
                }
            }
        }
    }
}

 

标签:gray,进阶,C#,outMat,OpenCV,faces,var,new,Mat
From: https://www.cnblogs.com/qq2806933146xiaobai/p/18295770

相关文章

  • golang sync.Pool 的基本原理
    sync.Pool包寥寥不过300行代码,却可以为我们提供类似对象池的功能,减少了对象的重复创建于销毁,减少了性能损耗,增加内存复用,同时自带mutex锁可以保证Put/Get操作的并发安全特性,在一些对象需要重复创建销毁的场景中很是实用,今天来看看sync.Pool的基本原理。sync.Pool就在标准......
  • QtQuick.Dialogs中的FileDialog设置默认目录的问题
    在QML中,假如想要使用文件浏览器选择文件或者文件夹时。可以使用FileDialog。FileDialog有个属性folder,设置好路径之后,当你打开fileDialog时,fileDialog当前定位到的路径就是你设置的路径。但是这个folder的设置有点问题,和路径的层级有关系假如你的目标路径是大于等于三级的(比如......
  • C#+OpenCV进阶(三)_操作摄像头与视频文件
    1、VideoCapture的常用属性#region常用属性//_=videoCapture.CaptureType;//获取视频的捕获类型(文件或摄像头)//_=videoCapture.PosAviRatio;//获取或设置视频的相对位置(0开头1结尾)//_=videoCapture.FourCC;//获取或设置视频的编解码的4字符代码//_=......
  • 使用夜莺和 Categraf 快速建设 MySQL 监控
    之前翻译过一篇文章,介绍 MySQL监控的一些原理,本文侧重实操,使用夜莺v7.beta12.1版本为大家做一个演示,采集器使用Categraf,先看一下最终仪表盘效果:下面开工。1.安装夜莺和Categraf夜莺的安装可以参考 夜莺官方文档,Categraf的安装可以参考 Categraf官方文档。这里就不赘......
  • 界面组件Kendo UI for React 2024 Q2亮点 - 生成式AI集成、设计系统增强
    随着最新的2024年第二季度发布,KendoUIforReact为应用程序开发设定了标准,包括生成式AI集成、增强的设计系统功能和可访问的数据可视化。新的2024年第二季度版本为应用程序界面提供了人工智能(AI)提示,从设计到代码的生产力增强、可访问性改进、一系列新的UI组件等。KendoUI致力......
  • Mock server自启动
    使用Flask-testing客户端,实现mock server自启动1.创建mock_server#mock_server.pyfromflaskimportFlask,jsonifyapp=Flask(__name__)@app.route('/api/user',methods=['GET'])defmock_user():returnjsonify({'id':1,'name&#......
  • HumanoidBench——模拟仿人机器人算法有未来
    概述论文地址:https://arxiv.org/pdf/2403.10506仿人机器人具有类似人类的外形,有望在各种环境和任务中为人类提供支持。然而,昂贵且易碎的硬件是这项研究面临的挑战。因此,本研究开发了使用先进模拟技术的HumanoidBench。该基准利用仿人机器人评估不同算法的性能,其中包括各......
  • SuperSocket 客户端
    通过NuGet来安装SuperSocket客户端,SuperSocket.ClientEngine实现代码如下:publicpartialclassMainPage:Page{SuperSocket.ClientEngine.AsyncTcpSessionclient=null;publicMainPage(){InitializeComponent();DataContext=ViewMode......
  • C语言中的数组:掌握数据的有序集合【一维数组,二维数组,字符串数组,直方图打印,计算全排列,
    目录C语言中的数组:掌握数据的有序集合【一维数组,二维数组,字符串数组】一维数组一维数组的创建数组的七种初始化完全初始化:部分初始化:字符数组的初始化:自动初始化为0:使用`memset`函数初始化:循环初始化:指定初始化器(`c99`,`gcc`)支持:一维数组的使用案例1:统计随机数的分布......
  • Memcached介绍和详解
    Memcached介绍和详解Memcached是一个高性能的分布式内存对象缓存系统,通过在内存中缓存数据来减少数据库的读取次数,从而提高动态Web应用程序的速度和效率。下面将详细介绍Memcached的安装、配置和使用方法。Memcached简介Memcached是一个基于内存的缓存系统,它通常用于缓......