source: foam/trunk/simple-faceident/src/Face.cpp @ 76

Revision 76, 2.3 KB checked in by dave, 12 years ago (diff)

changed file format to xml, experimental multiple image/face matching, better makefile

Line 
1// Copyright (C) 2009 foam
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17#include <vector>
18#include "highgui.h"
19
20#include "ImageUtils.h"
21#include "Face.h"
22
23#ifdef WIN32
24#include <string>
25#define snprintf _snprintf
26#endif
27
28using namespace std;
29
30/////////////////////////////////////////////////////////////////////////////////
31
32Face::Face(IplImage *image)
33{
34        AddImage(image);
35}
36
37Face::~Face()
38{
39        for(vector<IplImage *>::iterator i=m_ImageVec.begin(); i!=m_ImageVec.end(); i++)
40        {
41                cvReleaseImage(&(*i));
42        }
43}
44
45void Face::Learn(const IplImage *image, float blend, int imagenum)
46{
47        CvSize sizea = cvGetSize(image);
48        CvSize sizeb = cvGetSize(m_ImageVec[0]);
49
50        assert(sizea.width == sizeb.width);
51        assert(sizea.height == sizeb.height);
52        assert(imagenum < (int)m_ImageVec.size());
53       
54        float ret=0;
55
56    for(int y=0; y<sizea.height; y++)
57        {
58        for(int x=0; x<sizea.width; x++)
59                {
60                        cvSet2D(m_ImageVec[imagenum],y,x,cvScalar(
61                                                cvGet2D(m_ImageVec[imagenum],y,x).val[0]*(1 - blend) +
62                                                        cvGet2D(image,y,x).val[0]*blend,
63                                                cvGet2D(m_ImageVec[imagenum],y,x).val[1]*(1 - blend) +
64                                                        cvGet2D(image,y,x).val[1]*blend,
65                                                cvGet2D(m_ImageVec[imagenum],y,x).val[2]*(1 - blend) +
66                                                        cvGet2D(image,y,x).val[2]*blend));
67                }
68        }
69}
70
71void Face::AddImage(IplImage *image)
72{
73        m_ImageVec.push_back(image);
74}
75
76float Face::FindSimilar(const IplImage *image, int &imagenum)
77{
78        float error=FLT_MAX;
79        imagenum=-1;
80        unsigned int count=0;
81       
82        for(vector<IplImage *>::iterator i=m_ImageVec.begin(); i!=m_ImageVec.end(); i++)
83        {
84                float tmp = Diff(image,*i);
85                if (tmp<error)
86                {
87                        error=tmp;
88                        imagenum=count;
89                }
90                count++;
91        }
92       
93        return error;
94}
Note: See TracBrowser for help on using the repository browser.