使用opneCV人脸识别C/C++代码

一、案例

1.下列是完整的案例代码(含注解)。

#include<iostream>
#include<opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>

using namespace cv;
using namespace std;

//人脸识别函数
void datectFace(Mat& frame, CascadeClassifier cascade, double scale)
{
    
    Mat gray;
    cvtColor(frame, gray, CV_RGB2GRAY);  //颜色通道转换

    Mat RecognitionFace(cvRound(frame.rows / scale), cvRound(frame.cols / scale), CV_8UC1);   //灰度图行列压缩
    
    resize(gray, RecognitionFace, RecognitionFace.size(), 0, 0, INTER_LINEAR); //采用线性方式压缩

    equalizeHist(RecognitionFace, RecognitionFace);   灰度图转直方图函数处理
    imshow("RecognitionFace", RecognitionFace);     //灰度图显示

    
      //使用CV_HAAR_SCALE_IMAGE算法 图像甄别

    vector<Rect>faces;
  
    cascade.detectMultiScale(RecognitionFace, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));  //人脸检测函数:detectMultiScale

    //绘制矩形
    vector<Rect>::const_iterator iter;
   
    for (iter = faces.begin(); iter != faces.end(); iter++)
    {
            rectangle(frame,
            cvPoint(cvRound(iter->x * scale), cvRound(iter->y * scale)),//左上
            cvPoint(cvRound((iter->x + iter->width) * scale), cvRound((iter->y + iter->height) * scale)),//右下
            Scalar(0, 255, 0), 2, 8//颜色 像素位
        );
    }
    imshow("frame", frame);
}

int main(int argc, char* argv[])
{
   
    CascadeClassifier cascade;
    cascade.load("haarcascade_frontalface_default.xml"); //添加人脸模式

    Mat frame;
   
    VideoCapture capture(0);   //打开摄像头

    if (!capture.isOpened()) 
    {
        cout << "无法打开摄像头" << endl;
        return -1;
    }

    while (capture.read(frame))
    {
       
        imshow("frame", frame);  //显示
       
        datectFace(frame, cascade, 2); 
 
        if (waitKey(30) >= 0) break; // 等待按键输入,按下任意键退出循环

    }
    return 0;
}

二、效果

1.这里注意一下,opneCV自带有模型,直接复制到创建的文件下就好了。

2.模型用途

3.操作看下图。

4.识别效果

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV是一个开源的计算机视觉库,可以用于实现人脸识别功能。以下是一个示例的人脸识别代码(用C语言实现): ```c #include <opencv2/opencv.hpp> #include <opencv2/face.hpp> using namespace cv; using namespace cv::face; int main() { // 创建人脸识别器 Ptr<LBPHFaceRecognizer> model = LBPHFaceRecognizer::create(); // 准备训练数据 std::vector<Mat> images; std::vector<int> labels; // 输入样本图片和对应的标签 images.push_back(imread("person1.jpg", 0)); labels.push_back(1); images.push_back(imread("person2.jpg", 0)); labels.push_back(2); // 训练人脸识别模型 model->train(images, labels); // 加载待识别的测试图片 Mat testImage = imread("test.jpg", 0); // 进行人脸识别 int predictedLabel = -1; double predictedConfidence = 0.0; model->predict(testImage, predictedLabel, predictedConfidence); // 输出识别结果 if (predictedLabel != -1) { std::cout << "Predicted label: " << predictedLabel << std::endl; std::cout << "Confidence: " << predictedConfidence << std::endl; } else { std::cout << "No face detected in the test image." << std::endl; } return 0; } ``` 上述代码首先创建了一个LBPHFaceRecognizer的对象,接着加载训练样本图片和对应的标签,并通过train函数对模型进行训练。然后,代码加载待识别的测试图片,调用predict函数进行人脸识别,并输出识别结果。如果成功识别出人脸,代码将打印出预测的标签和置信度。如果无法在测试图片中检测到人脸,则输出未检测到人脸的提示信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值