这道题是利用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的更多相关文章

  1. 《学习OpenCV》练习题第四章第三题b

    #include <highgui.h> #include <cv.h> #include "opencv_libs.h" /* *<学习OpenCV ...

  2. 《学习OpenCV》练习题第四章第三题a

    #include <highgui.h> #include <cv.h> #include "opencv_libs.h" #pragma comment ...

  3. 《学习OpenCV》练习题第四章第七题abc

    题外话:一直是打算把这本书的全部课后编程题写完的,中间断了几个月,一直忙于其他事.现在开始补上. 这道题我不清楚我理解的题意是不是正确的,这道题可以练习用OpenCV实现透视变换(可以用于矫正在3维环 ...

  4. OpenCV实现的高斯滤波探究_1(《学习OpenCV》练习题第五章第三题ab部分)

    首先看下OpenCV 官方文档对于cvSmooth各个参数的解释: Smooths the image in one of several ways. C: void cvSmooth(const C ...

  5. 《学习OpenCV》 第四章 习题六

    实现的是一个图像标签编辑器,其间遇到了些问题还未解决或者可能解决方法上不是最优,若你有更好的思路可以提供给我,大恩不言谢啦!!☆⌒(*^-゜)v. #include "stdafx.h&qu ...

  6. 《学习OpenCV》练习题第四章第二题

    #include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...

  7. 《学习OpenCV》练习题第四章第一题b&c

    #include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...

  8. 《学习OpenCV》练习题第四章第一题a

    #include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...

  9. 学习opencv中文版教程——第二章

    学习opencv中文版教程——第二章 所有案例,跑起来~~~然而并没有都跑起来...我只把我能跑的都尽量跑了,毕竟看书还是很生硬,能运行能出结果,才比较好. 越着急,心越慌,越是着急,越要慢,越是陌生 ...

随机推荐

  1. eclipse中tomcat使用add and remove无法发布web项目

    继上次启动eclipse中的tomcat报classNotFound的问题后,这次又遇到新问题.就是右键点击tomcat使用add and remove发布web项目至tomcat后,启动tomcat ...

  2. cdev、udev

    udev :应用层的守护进程,由启动脚本加载,负责建立热拨插的接点 cdev :建立字符设备接口 platform device :相关平台直接总线建立的设备,主要出现需要自己直接挂到平台的时候,因为 ...

  3. SQLServer2008 行转列

    with a as( select *,row_number() over(partition by hyid order by jp desc) rowid from rtc) select a.h ...

  4. 10.10 dos试验

    一. 实验目的 (1)认识DOS: (2)掌握命令解释程序的原理: (3)掌握简单的DOS调用方法: (4)掌握C语言编程初步. 二. 实验内容和要求 编写类似于DOS,UNIX的命令行解释程序 (1 ...

  5. 浅析JavaScript函数的参数

    ECAMScript函数不介意传递进来多少个参数,也不介意传递的参数的类型,即使定义的函数只接受两个参数,当调用该函数时没有传递参数,甚至传递了三个参数等等都无所谓,这是因为在ECAMScript中参 ...

  6. Cookie操作类 实现记住用户名和密码的功能

    import java.util.Hashtable;import java.util.Iterator;import java.util.Set;import javax.servlet.http. ...

  7. content management system

    Defination of CMS: The definition of a CMS is an application (more likely web-based), that provides ...

  8. JAVA字符串格式化-String.format()的使用 (转载)

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...

  9. android studio sqlite操作代码片段

    SQLiteDatabase db;Cursor cur; SimpleCursorAdapter adapter;   // 打开或创建数据库db = openOrCreateDatabase(DB ...

  10. UVa 1609 (博弈) Foul Play

    姑且把它归类为一道博弈吧,毕竟这也是在找必胜方案. 十分有意思的一道题目,设计一种方案让你支持的1队获胜. 题目给出了两个很重要的条件: 1队能打败至少一半的队伍 对于1队不能打败的黑队,一定存在一个 ...