/
PreProcess.cpp
132 lines (104 loc) · 3.21 KB
/
PreProcess.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "PreProcess.h"
#include "FaceDetector.h"
/*
Function: DetectAndPreProcess
Purpose: Given a file and a name, try to find the face in the image (largest face)
Save this file as the name to disk
Notes:
Throws std::string if somthing goes wrong
Returns: true if face found and data saved, false if no face found
*/
bool DetectAndPreProcess(const char *image, const char* name)
{
IplImage* faceImage = NULL;
bool bRes = false;
// try to open the given file containing the face
// the one indicates that we assume the image is color
faceImage = cvLoadImage(image,1);
if ( image )
{
try
{
FaceDetector* fd = new FaceDetector(faceImage, true);
// find the largest face in the image
fd->Detect(true);
IplImage *tempFace;
// did we find the face?
if ( !fd->GetFaceVec().empty() )
{
// get the face from the face detector
IplImage* face = fd->GetFaceVec()[0];
// now perform the rest of the preprocessing on the face
tempFace = cvCreateImage(cvSize(face->width, face->height), face->depth, 1);
ConvertToGreyScale(face, tempFace);
Resize(face, tempFace);
// do histogram equalization on the found face
cvEqualizeHist(tempFace, tempFace);
// try to save it to disk
if ( !cvSaveImage( name, tempFace ) )
{
std::string err;
err = "Error: DetectAndPreProcess could not save ";
err += image;
err += " as ";
err += name;
throw err;
}
bRes = true;
}
delete fd;
}
catch (...)
{
throw;
}
}
else
{
// could not open image
std::string err;
err = "Error: DetectAndPreProcess could not open ";
err += image;
throw err;
}
return bRes;
}
/*
Function: PreProcess
Purpose: Resize, make into grey scale, and do histogram equalization on given image
Notes:
Throws std::string if somthing goes wrong
*/
void PreProcess( IplImage* src, IplImage** dest )
{
if ( *dest )
cvReleaseImage(&*dest);
try
{
FaceDetector* fd = new FaceDetector(src, false);
fd->Detect(true);
if ( !fd->GetFaceVec().empty() )
{
// get the face from the face detector
IplImage* face = fd->GetFaceVec()[0];
int width = 100;
int height = 100;
*dest = cvCreateImage(cvSize(width, height), src->depth, 1);
if ( !*dest )
throw std::string("PreProcess could not create dest image");
if ( src->nChannels != 1 )
ConvertToGreyScale(face, face);
Resize(face, *dest);
// do histogram equalization on the found face
cvEqualizeHist(*dest, *dest);
}
else
{
throw std::string("FaceDetector could not find face");
}
}
catch ( ... )
{
throw;
}
}