本文共 1371 字,大约阅读时间需要 4 分钟。
#include当然有了图片的识别,视频就逐帧处理就好了。直接捕获电脑自带的摄像头就OKusing namespace std;using namespace cv;string Path = "C:\\Users\\hl\\Desktop\\haarcascade_frontalface_default.xml";int main(){ Mat img = imread("C:\\Users\\hl\\Desktop\\功能照片\\本科入学照片hl.jpg"); imshow("input image", img); CascadeClassifier Classifier;//定义分类器 Classifier.load(Path); if (!Classifier.load(Path)) //加载训练文件 { cout << "xml文件加载失败" << endl; return -1; } VideoCapture capture(0); vector faces; Classifier.detectMultiScale(img, faces, 1.1, 3, 0, Size(30, 30)); for (size_t t = 0; t < faces.size(); t++) { rectangle(img, faces[t], Scalar(0, 255, 0), 2, 8);//画个绿框 } imshow("Result", img); waitKey(); return 0;}
#includeusing namespace std;using namespace cv;string Path = "C:\\Users\\hl\\Desktop\\haarcascade_frontalface_default.xml";int main(){ CascadeClassifier Classifier;//定义分类器 Classifier.load(Path); if (!Classifier.load(Path)) //加载xml文件 { cout << "xml文件加载失败" << endl; return -1; } VideoCapture capture(0);//捕获摄像头 vector faces; while (true) { Mat frame; capture >> frame;//逐帧处理 Classifier.detectMultiScale(frame, faces, 1.1, 3, 0, Size(30, 30)); for (size_t t = 0; t < faces.size(); t++) { rectangle(frame, faces[t], Scalar(0, 255, 0), 2, 8);//画个绿框 } imshow("Result", frame); waitKey(10);//延时10ms } return 0;}
视频截图。
转载地址:http://mamen.baihongyu.com/