-
Notifications
You must be signed in to change notification settings - Fork 1
/
face_detection.cpp
216 lines (188 loc) · 6.55 KB
/
face_detection.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <string>
#include <iostream>
#include <image_raii.hpp>
#include <histogram_raii.hpp>
#include <window_raii.hpp>
#include <cv.h>
#include <highgui.h>
/**
* Mouse event. Handles picking the ROI.
*/
void on_mouse( int event, int x, int y, int flags, void *param )
{
switch( event )
{
case CV_EVENT_LBUTTONDOWN:
{
CvPoint point = cvPoint( x, y );
( *(std::pair<CvPoint, CvPoint> *)param ).first = point;
break;
}
case CV_EVENT_LBUTTONUP:
{
CvPoint point = cvPoint( x, y );
( *(std::pair<CvPoint, CvPoint> *)param ).second = point;
break;
}
}
}
/**
* User defines a Region of Interest.
* @param image image of interest.
*/
CvRect specifyROI( IplImage * image )
{
const char * WINDOW_NAME = "Pick Region of Interest";
const int WAIT_TIME = 100; // in milliseconds
// first point corresponds to top left, second point corresponds to bottom right
std::pair<CvPoint, CvPoint> rectangle_corners;
// so we can tell if the rectangle_corners have been set
rectangle_corners.first.x = -1;
rectangle_corners.first.y = -1;
rectangle_corners.second.x = -1;
rectangle_corners.second.y = -1;
WindowRAII window( WINDOW_NAME );
cvShowImage( window.name, image);
cvSetMouseCallback( window.name, on_mouse, &rectangle_corners );
// wait until the rectangular bounding box is set
while( rectangle_corners.first.x < 0 || rectangle_corners.first.y < 0 || rectangle_corners.second.x < 0 || rectangle_corners.second.y < 0 )
{
cvWaitKey( WAIT_TIME );
}
CvPoint top_left_corner = rectangle_corners.first;
CvPoint bottom_right_corner = rectangle_corners.second;
int x_distance = bottom_right_corner.x - top_left_corner.x;
int y_distance = bottom_right_corner.y - top_left_corner.y;
CvRect image_roi = cvRect( top_left_corner.x, top_left_corner.y, x_distance, y_distance );
// display rectangle
CvScalar red;
red.val[2] = 255;
// copy image so not to ruin original image
ImageRAII image_copy( cvCreateImage( cvGetSize( image ), image->depth, image->nChannels ) );
cvRectangle( image_copy.image, top_left_corner, bottom_right_corner, red, 1 );
cvShowImage( window.name, image_copy.image );
cvWaitKey( WAIT_TIME );
return image_roi;
}
/**
* Makes a hue histogram given the image for a specified number of bins.
* @param image image to generate hue histogram off of.
* @param bins number of bins to use in the histogram
*/
HistogramRAII makeHueHistogram( IplImage * image, int bins )
{
const int HIST_DIMENSIONS = 1;
const float NORMALIZATION_FACTOR = 1.0;
ImageRAII image_hsv( cvCreateImage( cvGetSize( image ), image->depth, 3 ) );
ImageRAII hue_plane( cvCreateImage( cvGetSize( image ), image->depth, 1 ) );
ImageRAII saturation_plane( cvCreateImage( cvGetSize( image ), image->depth, 1 ) );
IplImage * planes[] = { hue_plane.image };
ImageRAII value_plane( cvCreateImage( cvGetSize( image ), image->depth, 1 ) );
int hist_size[] = { bins };
float hist_ranges[] = { 0, 360 };
float *ranges[] = { hist_ranges };
// Convert RGB to HSV
cvCvtColor( image, image_hsv.image, CV_BGR2HSV );
// Split HSV Channels
cvCvtPixToPlane( image_hsv.image, hue_plane.image, saturation_plane.image, value_plane.image, 0 );
WindowRAII window( "ROI" );
cvShowImage( window.name, hue_plane.image );
cvWaitKey( 0 );
HistogramRAII hue_histogram( cvCreateHist( HIST_DIMENSIONS, hist_size, CV_HIST_ARRAY, ranges, 1 ) );
cvCalcHist( planes, hue_histogram.histogram, 0, 0 );
// normalize histogram b/t 0 and 1
cvNormalizeHist( hue_histogram.histogram, NORMALIZATION_FACTOR );
return hue_histogram;
}
/**
* makes a probability map given the hue histogram and image.
* @param image image to compare against hue histogram
* @param histogram hue histogram
* @param bins number of bins
*/
float * makeProbabilityMap( IplImage * image, CvHistogram * histogram, int bins )
{
CvSize image_size = cvGetSize( image );
float sum = 0;
float bin_values[bins];
ImageRAII image_hsv( cvCreateImage( cvGetSize( image ), image->depth, 3 ) );
ImageRAII map( cvCreateImage( image_size, IPL_DEPTH_8U, 1 ) );
float * probability_map = new float[image_size.width * image_size.height];
// convert to HSV
cvCvtColor( image, image_hsv.image, CV_BGR2HSV );
for( int i = 0; i < bins; i++ )
{
int bin_value = cvQueryHistValue_1D( histogram, i );
sum += bin_value;
bin_values[i] = bin_value;
}
for( int i = 0; i < image_size.width; i++ )
{
for( int j = 0; j < image_size.height; j++ )
{
float hue = cvGet2D( image_hsv.image, j, i ).val[0] / 255.0;
for( int k = 0; k < bins; k++ )
{
float max_bin_value = ( 1.0 / bins ) * k + 1;
float min_bin_value = ( 1.0 / bins ) * k;
if( hue < max_bin_value && hue > min_bin_value )
{
float hist_value = cvQueryHistValue_1D( histogram, k );
CvScalar value;
value.val[0] = hist_value * 255;
cvSet2D( map.image, j, i, value );
}
}
/*
float total_bin_contribution = 0;
CvRect roi = cvRect( i, j, 1, 1 );
CvSize one_pixel = cvSize( 1, 1 );
ImageRAII pixel_image( cvCreateImage( one_pixel, image->depth, 3 ) );
cvSetImageROI( image, roi );
cvCopy( image, pixel_image.image );
HistogramRAII pixel_histogram = makeHueHistogram( pixel_image.image, 10 );
for( int l = 0; l < bins; l++ )
{
int bin_value = cvQueryHistValue_1D( pixel_histogram.histogram, l );
if( bin_value > 0 )
{
total_bin_contribution = bin_value * bin_values[l];
}
}
*( probability_map + i * image_size.width + j ) = total_bin_contribution / sum;
*/
}
}
WindowRAII window( "Probability Map" );
cvShowImage( window.name, map.image );
cvWaitKey( 0 );
return probability_map;
}
int main( int argc, char * agv[] )
{
const int BINS = 32;
float * probability_map;
ImageRAII friends_image( "Friends.jpg" );
CvRect image_roi = specifyROI( friends_image.image );
CvSize roi_size = cvSize( image_roi.width, image_roi.height );
ImageRAII roi_image( cvCreateImage( roi_size, friends_image.image->depth, 3 ) );
cvSetImageROI( friends_image.image, image_roi );
cvCopy( friends_image.image, roi_image.image );
cvResetImageROI( friends_image.image );
HistogramRAII histogram = makeHueHistogram( roi_image.image, BINS );
probability_map = makeProbabilityMap( friends_image.image , histogram.histogram, BINS );
cvResetImageROI( friends_image.image );
// display probability map
CvSize image_size = cvGetSize( friends_image.image );
printf( "%d %d", image_size.width, image_size.height );
for( int i = 0; i < image_size.width; i++ )
{
for( int j = 0; j < image_size.height; j++ )
{
//printf( "%f.2 ", *( probability_map + i * image_size.width + j ) );
}
//printf( "\n" );
}
delete[] probability_map;
return 0;
}