《学习OpenCV》练习题第四章第八题ab

这道题是利用OpenCV例子程序里自带的人脸检测程序,做点图像的复制操作以及alpha融合。

说明:人脸检测的程序我参照了网上现有的例子程序,没有用我用的OpenCV版本(2.4.5)的facedetect.c,人脸检测部分是一个月前写的程序,这次做了点改动,以前的部分注释了起来。

代码:

 #include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv_libs.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h> /*
*《学习OpenCV》第四章第八题
* 完成时间:3:43 10/5 星期六 2013
* 作者:qdsclove@163.com
*/ /* Result window title */
#define WND_RESULT "result" static CvMemStorage* storage = ;
static CvHaarClassifierCascade* cascade = ; void detect_and_draw( IplImage* image ); const char* cascade_name =
"haarcascade_frontalface_alt.xml"; /* Skull image */
IplImage* g_skullImage; /* Trackbar initial value */
int g_trackbar_value = ;
/* Tackbar total values */
int g_trackbar_total = ; /* Alpha blend's alpha */
double g_alpha = ; /* Trackbar callback function */
void switch_callback(int pos)
{
printf("Trackbar event. pos: %d\n", pos);
g_alpha = (double)pos / g_trackbar_total;
} int main( )
{
CvCapture* capture = cvCreateCameraCapture();
if(!capture)
{
printf("Camera error.\n");
return -;
}
cascade_name = "haarcascade_frontalface_alt2.xml";
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, , , ); if( !cascade )
{
printf( "ERROR: Could not load classifier cascade\n" );
return -;
} storage = cvCreateMemStorage();
cvNamedWindow( WND_RESULT, ); IplImage* frame = cvQueryFrame(capture);
if(!frame)
{
return -;
} // Load skull image
IplImage* skullImage = cvLoadImage("skull.jpg", CV_LOAD_IMAGE_UNCHANGED);
if(!skullImage)
{
printf("ERROR: Can't load skull image.\n");
return -;
}
g_skullImage = cvCreateImage(cvGetSize(skullImage), skullImage->depth, );
if(skullImage->nChannels != )
{
cvCvtColor(skullImage, g_skullImage, CV_GRAY2BGR);
}
else
{
cvCopy(skullImage, g_skullImage);
} // Create trackbar
cvCreateTrackbar("Switch", WND_RESULT, &g_trackbar_value,
g_trackbar_total, switch_callback );
// Initialize Alpha-Blend's alpha
g_alpha = (double)g_trackbar_value / g_trackbar_total; char c;
while( )
{
frame = cvQueryFrame(capture);
if(!frame)
{
return -;
} detect_and_draw( frame ); c = cvWaitKey();
if(c == )
{
break;
}
} cvDestroyWindow(WND_RESULT);
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
cvReleaseImage(&skullImage);
cvReleaseImage(&g_skullImage); return ;
} void detect_and_draw(IplImage* img )
{
double scale=1.2;
static CvScalar colors[] = {
{{,,}},{{,,}},{{,,}},{{,,}},
{{,,}},{{,,}},{{,,}},{{,,}}
};//Just some pretty colors to draw with //Image Preparation
//
IplImage* gray = cvCreateImage(cvSize(img->width,img->height),,);
IplImage* small_img=cvCreateImage(cvSize(cvRound(img->width/scale),cvRound(img->height/scale)),,);
cvCvtColor(img,gray, CV_BGR2GRAY);
cvResize(gray, small_img, CV_INTER_LINEAR); cvEqualizeHist(small_img,small_img); //直方图均衡 //Detect objects if any
//
cvClearMemStorage(storage);
double t = (double)cvGetTickCount();
CvSeq* objects = cvHaarDetectObjects(small_img,
cascade,
storage,
1.1,
,
/*CV_HAAR_DO_CANNY_PRUNING*/,
cvSize(,)); t = (double)cvGetTickCount() - t;
printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*.) ); //Loop through found objects and draw boxes around them
for(int i=;i<(objects? objects->total:);++i)
{
CvRect* r=(CvRect*)cvGetSeqElem(objects,i); CvRect r_scale = cvRect( r->x * scale, r->y * scale, r->width * scale, r->height * scale );
// 改变骷髅头图像大小
IplImage * skullResize = cvCreateImage( cvSize(r_scale.width, r_scale.height),
g_skullImage->depth, g_skullImage->nChannels );
cvResize(g_skullImage, skullResize, CV_INTER_LINEAR); // 将人脸检测区域用骷髅头图像代替
cvSetImageROI(img, r_scale);
// b -- Alpha blend
printf("Alpha: %f\t", g_alpha);
cvAddWeighted( img, g_alpha, skullResize, 1.0 - g_alpha, 0.0, img);
/* This is a part
cvCopy(skullResize, img); */
cvResetImageROI(img);
cvReleaseImage(&skullResize); // cvRectangle(img, cvPoint(r->x*scale,r->y*scale), cvPoint((r->x+r->width)*scale,(r->y+r->height)*scale), colors[i%8]);
}
/* for( int i = 0; i < (objects? objects->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( objects, 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 );
} */ cvShowImage( WND_RESULT, img );
cvReleaseImage(&gray);
cvReleaseImage(&small_img);
}

运行结果(此时的alpha为0.2):

《学习OpenCV》练习题第四章第八题ab

上一篇:怎么查看mysql的数据库编码格式


下一篇:FLASH AS 不显示中文