OpenCv haar+SVM训练的xml检测人头位置

注意:opencv-2.4.10

#include "stdio.h"
#include "string.h"
#include "iostream" #include "opencv/cv.h"
#include "opencv/cxcore.h"
#include "opencv/highgui.h"
#include "opencv/ml.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/videostab/videostab.hpp" #include "opencv2/contrib/contrib.hpp"
#include "opencv2/objdetect/objdetect.hpp" #pragma comment(lib,"opencv_calib3d2410d.lib")
#pragma comment(lib,"opencv_contrib2410d.lib")
#pragma comment(lib,"opencv_core2410d.lib")
#pragma comment(lib,"opencv_features2d2410d.lib")
#pragma comment(lib,"opencv_highgui2410d.lib")
#pragma comment(lib,"opencv_imgproc2410d.lib")
#pragma comment(lib,"opencv_objdetect2410d.lib")
#pragma comment(lib,"opencv_video2410d.lib")
#pragma comment(lib,"opencv_videostab2410d.lib") int main()
{ IplImage *pFrame = NULL;
IplImage *pGrayImg = NULL; //加载Haar特征检测分类器
const char *pCascadeFileName = "cascades.xml";
//CvHaarClassifierCascade *pHaarCascade = (CvHaarClassifierCascade*)cvLoad(pCascadeFileName);
CvHaarClassifierCascade *pHaarCascade = cvLoadHaarClassifierCascade(pCascadeFileName, cvSize(, )); //CvCapture *pCapture = cvCreateCameraCapture(0); //获取摄像头
cv::VideoCapture video();
int nFrmNum = ; if (!video.isOpened())
{
std::cout << "加载失败..." << std::endl;
return -;
}
//create window
cvNamedWindow("Video", );
CvMemStorage *pcvMStorage;
CvSeq *pCvSeq;
cv::Mat img,grayimg,dstimg;
double scalar = 2.2;
video >> img;
std::vector<cv::Rect>rect,vector1,vector2,vector3;
int nFrame = ;
while () //逐帧读取
{
video >> img;
cv::cvtColor(img, grayimg, CV_BGR2GRAY);
cv::imshow("grayimg1", grayimg);
cv::add(grayimg, grayimg, grayimg);
//cv::add(grayimg, grayimg, grayimg);
//cv::imshow("grayimg2", grayimg);
pFrame = &(IplImage(grayimg)); //---------------人脸识别-------------------------
rect.clear();
if (pHaarCascade != NULL)
{ pcvMStorage = cvCreateMemStorage();
cvClearMemStorage(pcvMStorage); // 识别
pCvSeq = cvHaarDetectObjects(pFrame, pHaarCascade, pcvMStorage, 1.1, , , cvSize(, ), cvSize(, )); // 标记
for (int i = ; i <pCvSeq->total; i++)
{
CvRect* r = (CvRect*)cvGetSeqElem(pCvSeq, i);
rect.push_back(cv::Rect(r->x,r->y,r->width,r->height));
} cvReleaseMemStorage(&pcvMStorage);
} for (size_t i = ; i < rect.size(); i++)
{
cv::rectangle(img,rect[i],cv::Scalar(,,),);
}
cv::imshow("video", img);
cv::resize(img, dstimg, cv::Size(img.cols*scalar, img.rows*scalar), , , ); cv::imshow("dstimg", dstimg);
//---------------人脸识别-------------------------
char c = cvWaitKey(); //按esc退出
if (c == )
{
break;
}
nFrame++;
}
cv::destroyAllWindows();
return ;
}
上一篇:安卓开发_浅谈TimePicker(时间选择器)


下一篇:Cron表达式详解【转】