保存图像代码
unsigned char * pBuffer =video_dev.buffer;
fp=fopen("./photo.jpg","w");
fwrite(pBuffer,12000,1,fp);
fclose(fp);
在利用IplImage把图片读到内存中。接着就是对他进行人脸检测处理。
void photo::detect_and_draw(IplImage *img)
{
static CvScalar colors[] =
{
{{0,0,255}},
{{0,128,255}},
{{0,255,255}},
{{0,255,0}},
{{255,128,0}},
{{255,255,0}},
{{0,255,0}},
{{255,0,255}}
};
double scale = 2;
//建立一个空的灰度图
IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
//建立一个空圆形的灰度图
IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
cvRound (img->height/scale)),
8, 1 );
int i;
//图像转换RGB模式转为灰度图
cvCvtColor( img, gray, CV_BGR2GRAY );
cvResize( gray, small_img, CV_INTER_LINEAR );
cvEqualizeHist( small_img, small_img );
cvClearMemStorage( storage );
if( cascade )
{
double t = (double)cvGetTickCount();
CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
cvSize(30, 30) );
t = (double)cvGetTickCount() - t;
printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, 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( img, center, radius, colors[i%8], 3, 8, 0 );
cvRectangle(img,cvPoint(center.x-radius,center.y-radius),cvPoint(center.x+radius,center.y+radius),colors[i%8],3,8,0);
//cvRect(center.x-radius,center.y-radius,center.x+radius,center.y+radius);
}
}
}