opencv在视频中捕捉人脸--opencv3.0

使用opencv在视频中检测人脸

#include <iostream>
#include "cxcore.h"
#include "cv.h"
//#include "highgui.h"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp" 
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h> 


using namespace std;
using namespace cv;


void detectAndDisplay(Mat face);


string face_cascade_name = "haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;


int main(int _argc, char **_argv){
char *videoName;


if (_argc != 2){
videoName = "ny.avi";
}
else
{
videoName = _argv[1];
}
//cvNamedWindow("video", 0);
//打开视频文件---
VideoCapture capture(videoName);
if (!capture.isOpened())cout << "fail to open!" << endl;
//获取帧的总数
long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
cout << "整个视频共" << totalFrameNumber << "帧" << endl;



//从第30帧开始捕捉
long frameToStart = 300;
long currentFrame = frameToStart;//记录当前读取的是第几帧
capture.set(CV_CAP_PROP_POS_FRAMES, frameToStart);


//获取帧率
double rate = capture.get(CV_CAP_PROP_FPS);
cout << "帧率为:" << rate << endl;


namedWindow("Extracted Frame"); //建立一个用以显示图像的窗口
Mat frame;  //存放每一帧的图像


//利用while循环读取帧
while (true){
if (!capture.read(frame)){
cout << "读取视频失败" << endl;
return -1;
}




//在当前帧图片中检测人脸/
if (!face_cascade.load(face_cascade_name)){
printf("级联分类器错误,可能未找到文件,拷贝该文件到工程目录下!\n");
return -1;
}
detectAndDisplay(frame); //调用人脸检测函数  
/






imshow("Extracted Frame",frame);
cout << "正在读取第" << currentFrame << "帧" << endl;


int c = waitKey(33);  //等待33毫秒   //当时间结束前没有按键按下时,返回值为-1;否则返回按键
if (c == 27 || currentFrame >= totalFrameNumber)

break;
}
if (c >= 0){
waitKey(0);
}


currentFrame++;
}


capture.release();
waitKey(0);
return 0;

}




void detectAndDisplay(Mat face){
std::vector<Rect> faces;
Mat face_gray;

cvtColor(face, face_gray, CV_BGR2GRAY);  //rgb类型转换为灰度类型
equalizeHist(face_gray, face_gray);   //直方图均衡化
/*
//iplImage to Mat
cv::Mat pmatImage = cv::cvarrToMat(face_gray); //第二个参数表示不进行像素数据copy;
*/
face_cascade.detectMultiScale(face_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(1, 1));


for (int i = 0; i < faces.size(); i++){
Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
ellipse(face, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 0), 2, 7, 0);
}
imshow("Extracted Frame", face);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值