一、前言
使用OpenCV实现从摄像头实时检测人脸,并用方框框出来。
这里主要通过OpenCV里自带的一些函数和Haar特征分类器【脸部】的来实现人脸检测。仅适合初学者。
能力有限,理论不做解释,仅含简单代码逻辑。
二、主要代码
开启摄像头代码:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
VideoCapture cap(0);
if (!cap.isOpened()) {
cout << "打开摄像头失败!" <<endl;
return -1;
}
Mat frame,grayFrame;
Mat edges;
bool stop = false;
while (!stop) {
cap >> frame;
//将图像变灰、模糊,保留轮廓
//cvtColor(frame, edges, COLOR_BGR2GRAY);
//GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
//Canny(edges, edges, 0, 30, 3);
//imshow("Camera", edges);//显示处理后的轮廓图
imshow("C