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(200, 200)); //CvCapture *pCapture = cvCreateCameraCapture(0); //获取摄像头 cv::VideoCapture video(0); int nFrmNum = 0; if (!video.isOpened()) { std::cout << "加载失败..." << std::endl; return -1; } //create window cvNamedWindow("Video", 1); 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 = 0; while (1) //逐帧读取 { 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(0); cvClearMemStorage(pcvMStorage); // 识别 pCvSeq = cvHaarDetectObjects(pFrame, pHaarCascade, pcvMStorage, 1.1, 2, 0, cvSize(120, 120), cvSize(420, 420)); // 标记 for (int i = 0; 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 = 0; i < rect.size(); i++) { cv::rectangle(img,rect[i],cv::Scalar(0,255,0),2); } cv::imshow("video", img); cv::resize(img, dstimg, cv::Size(img.cols*scalar, img.rows*scalar), 1, 1, 3); cv::imshow("dstimg", dstimg); //---------------人脸识别------------------------- char c = cvWaitKey(33); //按esc退出 if (c == 27) { break; } nFrame++; } cv::destroyAllWindows(); return 0; }

 

posted @ 2015-12-23 15:54  一样菜  阅读(6263)  评论(0编辑  收藏  举报