/*#include "cv.h"
#include "cvcam.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
//#include <ctype.h>
//#include <stdio.h>
#pragma comment(lib, "cv.lib")
#pragma comment(lib, "highgui.lib")
#pragma comment(lib, "cxcore.lib")
// various tracking parameters (in seconds)
const double MHI_DURATION = 0.5;
const double MAX_TIME_DELTA = 0.5;
const double MIN_TIME_DELTA = 0.05;
// 用于运动检测的循环帧数,与机器速度以及FPS设置有关
const int N = 2;
CvSize imgSize;
// ring image buffer
IplImage *buf = 0;
int last = 0;
// temporary images
IplImage *mhi = 0; // MHI: motion history image
IplImage *orient = 0; // orientation
IplImage *mask = 0; // valid orientation mask
IplImage *segmask = 0; // motion segmentation map
IplImage *out = 0;
CvMemStorage* storage = 0; // temporary storage
CvRect track_rect;
CvCapture* capture = 0;
CvPoint trackpoint ;
//IplImage *tmpImg2 = NULL;
int count = 0;
int count1 = 0;
IplImage* facecheck(IplImage *image)
{
cvFlip(image, image, 0);
CvHaarClassifierCascade* cascade;
CvMemStorage* storage;
CvSeq* faces;
const char* file_name;
int i;
IplImage *image_scale;
image_scale = cvCreateImage( cvSize(480,270), IPL_DEPTH_8U, 3);//160,120,cvSize(192,108)384,216,768,432
cvResize(image,image_scale,1);
//initializations
storage=cvCreateMemStorage(0);
cvFirstType();
file_name="haarcascade_frontalface_alt.xml";
cascade = (CvHaarClassifierCascade*)cvLoad(file_name,NULL, NULL, NULL);
faces = cvHaarDetectObjects( image_scale, cascade, storage, 1.2, 2,CV_HAAR_DO_CANNY_PRUNING,cvSize(0, 0));
//draw rectangles
for(i=0;i<(faces ? faces->total:0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
CvPoint pt1 = { r->x-10, r->y-10 };
CvPoint pt2 = { r->x + r->width+20, r->y + r->height + 140};
cvRectangle( image_scale, pt1, pt2, CV_RGB(0,0,0),CV_FILLED, 8, 0 );//CV_FILLED
// cvCircle(image, pt1, 1, CV_RGB(255,0,0), 4);
// cvCircle(image, pt2, 1, CV_RGB(255,255,0), 4);
}
cvResize(image_scale,image,1);
cvFlip(image, image, 0);
// cvNamedWindow( "skinout1", 1 );
// cvShowImage( "skinout1", image );
// create window and show the image with outlined faces
if (faces!=NULL)
{
cvClearSeq(faces);
}
cvReleaseHaarClassifierCascade( &cascade );
cvReleaseMemStorage( &storage );
cvReleaseImage(&image_scale);
return image;
}
IplImage *skinDetect( IplImage *img)
{
IplImage *tmpImg = NULL;
IplImage *tmpImg2 = NULL;
IplImage *conv = NULL;
IplImage *H= NULL;
IplImage *S= NULL;
IplImage *V= NULL;
IplImage *tmpH1= NULL;
IplImage *tmpS1= NULL;
IplImage *tmpH2= NULL;
IplImage *tmpH3= NULL;
IplImage *tmpS2= NULL;
IplImage *tmpS3= NULL;
CvMemStorage* storage;
storage = cvCreateMemStorage(0);
CvSeq* contour;
contour = 0;
IplConvKernel *erosionElement, *dilationElement;
double contArea, imgArea = 0.0;
imgArea = imgSize.width * imgSize.height;
conv = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);
tmpImg2 = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);
tmpH1 = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
tmpS1 = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
tmpH2 = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
tmpS3 = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
tmpH3 = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
tmpS2 = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
H = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
S = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
V = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
tmpImg =cvCloneImage(img);
// Convert to HSV
cvCvtColor( tmpImg, conv, CV_BGR2HSV );
// Split to HSV planes
//cvCvtPixToPlane(conv,H,S,V,0);
cvSplit(conv,H,S,V,0);
/* cvInRangeS(H,cvScalar(5.0,0.0,0,0),cvScalar(30.0,0.0,0,0),tmpH1);//25 5 18
cvInRangeS(S,cvScalar(10.0,0.0,0,0),cvScalar(150.,0.0,0,0),tmpS1);//75 90
cvAnd(tmpH1,tmpS1,tmpH1,0);
cvInRangeS(H,cvScalar(170.0,0.0,0,0),cvScalar(180.0,0.0,0,0),tmpH2);//25 5 18
cvInRangeS(S,cvScalar(15.0,0.0,0,0),cvScalar(90.,0.0,0,0),tmpS2);//75
cvAnd(tmpH2,tmpS2,tmpH2,0);//*/
/* cvInRangeS(H,cvScalar(2.0,0.0,0,0),cvScalar(30.0,0.0,0,0),tmpH1);//25 5 18
cvInRangeS(S,cvScalar(5.0,0.0,0,0),cvScalar(150.,0.0,0,0),tmpS1);//75 90
cvAnd(tmpH1,tmpS1,tmpH1,0);
cvInRangeS(H,cvScalar(160.0,0.0,0,0),cvScalar(180.0,0.0,0,0),tmpH2);//25 5 18
cvInRangeS(S,cvScalar(15.0,0.0,0,0),cvScalar(90.0,0.0,0,0),tmpS2);//75
cvAnd(tmpH2,tmpS2,tmpH2,0);
cvOr(tmpH1,tmpH2,tmpH1);
cvSmooth(tmpH1, tmpH3, CV_MEDIAN);
// Dilation and Erosion
// Structuring Element Generation
dilationElement = cvCreateStructuringElementEx( 5,5,3,3, CV_SHAPE_RECT , 0 );
erosionElement = cvCreateStructuringElementEx( 5,5,3,3, CV_SHAPE_RECT , 0 );
// Erosion peels a layer of pixels off, and makes small regions disappear
cvErode(tmpH3,tmpH3,erosionElement,1);
// Dilation adds a layer on, and returns things to the correct size.
cvDilate(tmpH3,tmpH3,dilationElement,1);
// Find the contours of all remaining objects
cvFindContours( tmpH3, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
cvZero(tmpH1);
// contour now contains a CvSequence of all of the contours
for( ; contour != 0; contour = contour->h_next )
{
contArea = fabs(cvContourArea( contour, CV_WHOLE_SEQ ));
double bl=fabs(cvContourPerimeter(contour));
//double a=4*3.14*contArea/(bl*bl);
// Ignore very small contours
for(int i = 0;i < contour->total;i++)
{
CvPoint* p = (CvPoint* )cvGetSeqElem(contour, i);
if ((p->y == 0) || (p->x == 0)||(p->x == imgSize.width) ||(p->y == imgSize.height))
{
// cvSeqRemove(contour,i);
cvClearSeq(contour);
}
}
if ( (contArea/imgArea <= 0.015) )//|| (contArea/imgArea >= 0.3)
{
cvSeqRemove(contour,0);
}
else //if((a >= 0.09)&&(a<= 0.33) )
{
//CvRect comp_rect1 = ((CvConnectedComp*)cvGetSeqElem( seq, i ))->rect;
/*CvRect bndRect1 = cvBoundingRect( contour, 0 );
CvPoint pt1, pt1;
if ((bndRect1.height/bndRect1.width) > 1.1 )// (bndRect1.height/bndRect1.width) < 0.9
{
for(int i = 0;i < contour->total;i++)
{
CvPoint p = (CvPoint* )cvGetSeqElem(contour_mid, i);
if (p->y > 1.1 * bndRect1.width )
{
top.x = p->x;
top.y = p->y;
}
}
/* pt1.x = bndRect1.x;
pt1.y = bndRect1.y;
pt2.x = bndRect1.x + bndRect1.width;
pt2.y = bndRect1.y + bndRect1.height;//*/
/* if ((bndRect1.height/bndRect1.width) > 1.1 )// (bndRect1.height/bndRect1.width) < 0.9
{
}//*/
/* cvDrawContours( tmpH1, contour, CV_RGB( 255,255,255 ), CV_RGB( 255,255,255 ), 0, -1, 8 );
//cvDrawContours( img, contour, CV_RGB( 255,0,255 ), CV_RGB( 0,255,255 ), -1, -1, 8 );
}
}
cvCvtColor(tmpH1,tmpImg2,CV_GRAY2BGR);
cvAnd(tmpImg2,img,img);
// If we found any contours then free the memory they use.
if (contour!=NULL)
{
cvClearSeq(contour);
}
cvReleaseImage(&conv);
cvReleaseImage(&H);
cvReleaseImage(&S);
cvReleaseImage(&V);
cvReleaseImage(&tmpH1);
cvReleaseImage(&tmpH2);
cvReleaseImage(&tmpH3);
cvReleaseImage(&tmpS1);
cvReleaseImage(&tmpS2);
cvReleaseImage(&tmpS3);
cvReleaseImage(&tmpImg);
cvReleaseImage(&tmpImg2);
cvReleaseMemStorage(&storage);
return img;
}
// parameters:
// img - input video frame
// dst - resultant motion picture
// args - optional parameters
void update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
{
//cvNamedWindow( "Motion1", 1 );
double timestamp = clock()/1000.; // get current time in seconds
CvSize size = cvSize(img->width,img->height); // get current frame size
// int i, idx1 = last, idx2;
IplImage* silh;
IplImage* draw = NULL;
CvSeq* seq;
CvRect comp_rect;
double count;
double angle;
CvPoin