#include "peoplecounting.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "cvaux.h"
#include "cxcore.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void peoplecounting::SelectTheCamera()
{
//初始化内存及分类器
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
//加载分类器,名称为"xml.xml"
const char* cascade_name="C:/Users/kjyszylkj/Desktop/460518377Count/Count/Count/xml.xml";
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
if( !cascade )
{
QMessageBox::information(
0,
"错误",
"不能加载分类器",
QMessageBox::Ok);
}
//定义某一帧的图片、灰度图及haar图
IplImage* pFrame = NULL;
IplImage* pHaarImg = NULL;
IplImage* pGrayImg = NULL;
int nFrmNum = 0;
cvNamedWindow("camera",1);
cvMoveWindow("camera",30,0);
//打开自带摄像头
CvCapture* pCapture = cvCreateCameraCapture(-1);
if(!(pCapture))
{
QMessageBox::information(
0,
"错误",
"不能打开摄像头",
QMessageBox::Ok);
}
else
{
while(pFrame = cvQueryFrame(pCapture)) //如果能够捕获到视频
{
nFrmNum++;
//如果这是第一帧视频,需要申请内存并初始化
if(nFrmNum == 1)
{
pGrayImg = cvCreateImage(cvSize(pFrame->width,pFrame->height),8,1);
pHaarImg = cvCreateImage(cvSize(cvRound(pFrame->width/1.1),cvRound(pFrame->height/1.1)),8,1);
//转化为灰度图像再处理
cvCvtColor(pFrame, pGrayImg,CV_BGR2GRAY);
//缩放灰度图像
cvResize(pGrayImg,pHaarImg,CV_INTER_LINEAR);
}
else
{
cvGrabFrame(pCapture); //抓取一帧
pFrame = cvRetrieveFrame(pCapture); //将图像从缓存中转换为IplImage存在pFrame中
//转化为灰度图像
cvCvtColor(pFrame,pGrayImg,CV_BGR2GRAY);
//缩放灰度图像
cvResize(pGrayImg, pHaarImg,CV_INTER_LINEAR);
//直方图均衡化
cvEqualizeHist(pHaarImg,pHaarImg);
//分配内存空间
storage = cvCreateMemStorage(0);
//人数统计
if(cascade)
{
//检测窗口每次放大1.1倍
double scale = 1.1;
int i;
//使用分类器进行人脸检测
CvSeq* PeopleCounting = cvHaarDetectObjects( pHaarImg, cascade, storage,
scale, 2, 0/*"CV_HAAR_DO_CANNY_PRUNING"*/,
cvSize(30,30) );
//在检测到的人脸上画出红色圆圈标记
for(i=0;i<(PeopleCounting?PeopleCounting->total:0);i++)
{
CvRect* r = (CvRect*)cvGetSeqElem(PeopleCounting,i); //返回矩形的信息
CvPoint center;
int radius;
center.x = cvRound((r->x + r->width*0.5)*scale); //圆心横坐标
center.y = cvRound((r->y + r->height*0.5)*scale); //圆心纵坐标
radius = cvRound((r->width + r->height)*0.25*scale); //半径
cvCircle(pFrame, center, radius, CV_RGB(255,0,0), 3, 8, 0 ); //(图像,圆心,半径,颜色,粗细,线条类型,小数点位数)
}
//将人数的个数显示到主窗口
int number;
number = PeopleCounting->total;
QString tmp;
tmp = QString("副窗口画面中一共有%1个人").arg(number); //文字信息
ui->textEdit->setText(QString(tmp)); //设置文字信息
ui->textEdit->setAlignment( Qt::AlignCenter ); //设置文字居中
}
//显示每一帧的图像
cvShowImage("camera",pFrame);
char key = cvWaitKey(1000);
}
}
//释放图像
cvReleaseImage(&pFrame);
cvReleaseImage(&pHaarImg);
cvReleaseImage(&pGrayImg);
cvWaitKey(0);
//销毁窗口
cvDestroyWindow("camera");
//释放摄像头
cvReleaseCapture(&pCapture);
}
}
各位大侠求救!!!