码迷,mamicode.com
首页 > 其他好文 > 详细

基于Opencv图像处理的时时头像採集试验

时间:2014-08-27 14:37:37      阅读:424      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   for   文件   ar   

2014 4.20 

      近期想做一个关于图像处理的软件玩玩,可惜也没有什么特别的想法,就当玩玩好了,准备用Opencv开源库实现下简单的功能吧。

    Opencv是一个专业的图像处理库,里面有非常多基础函数能够实现非常多非常多功能,明天開始动工吧,真是兴致来了挡也档不住,思考一晚上!

2014 4.21

    本来想用Vs2010做下,发现配置有点麻烦,还是选择Vc6干吧,选用的库为 Opencv1.0.

配置Visual C++ 6.0

全局设置

菜单Tools->Options->Directories:先设置lib路径,选择Library files,在下方填入路径:

E:\OpenCV\lib

然后选择include files,在下方填入路径:

E:\OpenCV\cxcore\include

E:\OpenCV\cv\include

E:\OpenCV\cvaux\include

E:\ml\include

E:\OpenCV\otherlibs\highgui

E:\otherlibs\cvcam\include

然后选择source files,在下方填入路径:

E:\OpenCV\cv\src

E:\OpenCV\cxcore\src

E:\OpenCV\cvaux\src

E:\OpenCV\otherlibs\highgui

E:\OpenCV\otherlibs\cvcam\src\windows

还有就是配置系统环境变量 E:\OpenCV\bin

然后选择右边的link标签,在Object/library modules附加上

cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib cvcam.lib

关于配置的基本就到这里了,该用的基本都用上了。

 

以下就開始做小demo測试了。

2014 4.28

近期事情有点多,偷懒不想写,还是坚持下吧。

以下做一个人脸检測的demo,基于Win32 application,功能大致实现了,代码网上也有非常多,我来分析下代码吧。

#include "cv.h"    //核心头文件
#include "highgui.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>

#ifdef _EiC
#define WIN32
#endif

static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;

void detect_and_draw( IplImage* image );

const char* cascade_name =
"D:\\OpenCV 1.0\\data\\haarcascades\\haarcascade_frontalface_alt.xml";//人脸检測库文件
/*    "haarcascade_profileface.xml";*/

int main( int argc, char** argv )
{
    //cascade_name = "haarcascade_frontalface_alt.xml";
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
 
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);
    cvNamedWindow( "result", 1 ); //创建一个窗体 名称为result
    
    const char* filename = "D:\\lena.jpg"; //目标图片
    IplImage* image = cvLoadImage( filename, 1 );

    if( image )
    {
        detect_and_draw( image );
        cvWaitKey(0);
        cvReleaseImage( &image );  
    }

    cvDestroyWindow("result");
 
    return 0;
}


void detect_and_draw(IplImage* img ) //把脸部范围圈出来
{
    double scale=1.2;
    static CvScalar colors[] = {
        {{0,0,255}},{{0,128,255}},{{0,255,255}},{{0,255,0}},
        {{255,128,0}},{{255,255,0}},{{255,0,0}},{{255,0,255}}
    };//Just some pretty colors to draw with

    //Image Preparation
    //
    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);
    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,
                                                                        2,
                                                                        0/*CV_HAAR_DO_CANNY_PRUNING*/,
                                                                        cvSize(30,30));

    t = (double)cvGetTickCount() - t;
    printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );

    //Loop through found objects and draw boxes around them
   
 for(int i=0;i<(objects? objects->total:0);++i)
    {
        CvRect* r=(CvRect*)cvGetSeqElem(objects,i);
        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 j= 0; j < (objects? objects->total : 0); j++ )
    {
        CvRect* r = (CvRect*)cvGetSeqElem( objects, j );
        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[j%8], 3, 8, 0 );
    }*/

 
    cvShowImage( "result", img );
    cvReleaseImage(&gray);
    cvReleaseImage(&small_img); //记得创建的指针得释放
}

bubuko.com,布布扣

效果大致是这种,把脸部圈出来了。

 

基于Opencv图像处理的时时头像採集试验

标签:des   style   blog   http   color   io   for   文件   ar   

原文地址:http://www.cnblogs.com/mfrbuaa/p/3939316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!