OpenCV 脸部识别 c++

本文介绍了如何使用OpenCV库中的Haar级联分类器进行人脸检测,包括创建分类器对象、加载预训练模型、捕获视频帧并进行灰度转换、等化直方图、检测人脸并绘制矩形框。

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


#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
 
#include <iostream>
#include <stdio.h>
 
using namespace std;
using namespace cv;
 
int main(int argc, const char** argv)
{
    //create the cascade classifier object used for the face detection
    CascadeClassifier face_cascade;
    //use the haarcascade_frontalface_alt.xml library
    face_cascade.load("haarcascade_frontalface_alt2.xml");
 
    //setup video capture device and link it to the first capture device
    VideoCapture captureDevice;
    captureDevice.open(0);
 
    //setup image files used in the capture process
    Mat captureFrame;
    Mat grayscaleFrame;
 
    //create a window to present the results
    namedWindow("outputCapture", 1);
 
    //create a loop to capture and find faces
    while(true)
    {
        //capture a new image frame
        captureDevice>>captureFrame;
 
        //convert captured image to gray scale and equalize
        cvtColor(captureFrame, grayscaleFrame, CV_BGR2GRAY);
        equalizeHist(grayscaleFrame, grayscaleFrame);
 
        //create a vector array to store the face found
        std::vector<Rect> faces;
 
        //find faces and store them in the vector array
        face_cascade.detectMultiScale(grayscaleFrame, faces, 1.1, 3, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30));
 
        //draw a rectangle for all found faces in the vector array on the original image
        for(int i = 0; i < faces.size(); i++)
        {
            Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
            Point pt2(faces[i].x, faces[i].y);
 
            rectangle(captureFrame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
        }
 
        //print the output
        imshow("outputCapture", captureFrame);
 
        //pause for 33ms
        waitKey(33);
    }
 
    return 0;
}




Opencv是一个开源的计算机视觉库,提供了许多视觉处理的功能,其中包括人脸识别。下面是一个使用Opencv进行人脸识别的C语言示例代码: ```c #include <stdlib.h> #include <stdio.h> #include <opencv2/opencv.hpp> using namespace cv; int main(int argc, char** argv) { CascadeClassifier face_cascade; Mat frame, gray; // 加载人脸级联分类器 if (!face_cascade.load("haarcascade_frontalface_alt.xml")) { printf("Error loading face cascade.\n"); return -1; } // 打开摄像头 VideoCapture cap(0); if (!cap.isOpened()) { printf("Error opening video capture.\n"); return -1; } namedWindow("Face Detection", WINDOW_NORMAL); while (true) { // 读取帧 if (!cap.read(frame)) { printf("Error reading frame.\n"); break; } // 转换为灰度图像 cvtColor(frame, gray, COLOR_BGR2GRAY); // 人脸检测 std::vector<Rect> faces; face_cascade.detectMultiScale(gray, faces, 1.3, 5); // 绘制检测框 for (size_t i = 0; i < faces.size(); i++) { rectangle(frame, faces[i], Scalar(0, 255, 0), 2); } // 显示结果 imshow("Face Detection", frame); // 按ESC键退出 if (waitKey(30) == 27) { break; } } // 释放内存 cap.release(); destroyAllWindows(); return 0; } ``` 上面的代码中使用了OpenCV自带的级联分类器来检测人脸,通过调用CascadeClassifier类并加载haarcascade_frontalface_alt.xml文件来实现。同时,还需要使用VideoCapture类来读取摄像头捕获的帧并进行灰度转换,最后在图像上绘制检测到的人脸,并在窗口中显示结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值