-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaskMaker.c
More file actions
178 lines (138 loc) · 4.34 KB
/
maskMaker.c
File metadata and controls
178 lines (138 loc) · 4.34 KB
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
#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>
// User's image mask (not an actual CvArr* mask)
IplImage* mask;
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
void detect_and_draw( IplImage* image );
const char* cascade_name = "haarcascade_frontalface_default.xml";
// Function to detect and draw any faces that is present in an image
void detect_and_draw( IplImage* img )
{
int scale = 1;
// Create a new image based on frame image (img)
IplImage* temp =
cvCreateImage(cvSize(img->width/scale,img->height/scale),8,3);
// Two points to represent the face locations
CvPoint pt1,pt2;
int i;
// Clear the memory storage used before
cvClearMemStorage( storage );
if( cascade )
{
// Detect the objects and store them in the sequence
CvSeq* faces = cvHaarDetectObjects( img, cascade,storage,1.1,
2,CV_HAAR_DO_CANNY_PRUNING,cvSize(20,20),cvSize(40, 40) );
// Loop the number of faces found.
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
// Create a new rectangle for drawing face
CvRect* r = (CvRect*)cvGetSeqElem(faces, i );
// Find the dimensions of the face and scale if needed
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
int width = pt2.x-pt1.x;
int height = pt2.y-pt1.y;
width = width + 1;
height = height + 1;
// Resize mask to fit face detection
IplImage* t = cvCreateImage(cvSize(width,height),8,3);
cvResize(mask,t,CV_INTER_LINEAR);
CvSize sz = cvGetSize(t);
cvSetImageROI(img,cvRect(pt1.x,pt1.y,width,height));
cvCopy(t,img,NULL);
cvResetImageROI(img);
}
}
cvShowImage( "Make-A-Mask", img );
cvReleaseImage( &temp );
}
int main( int argc, char** argv ){
// 1st argument is video, 2nd argument is mask pic
IplImage *src;
src = cvLoadImage(argv[2],CV_LOAD_IMAGE_COLOR);
// Crop mask pic according to user's dimensions
int usrW;
int usrH;
printf ("Enter width to crop mask or enter 0 to load mask as is: ");
scanf ("%d", &usrW);
if (usrW != 0){
printf ("Enter height to crop mask: ");
scanf ("%d", &usrH);
cvSetImageROI(src, cvRect((src->width - usrW)/2, (src->height -
usrH)/2,usrW,usrH));
mask = cvCreateImage(cvGetSize(src),src->depth, src->nChannels);
cvCopy(src,mask,NULL);
cvResetImageROI(src);
}
else{
mask = cvLoadImage(argv[2],CV_LOAD_IMAGE_COLOR);
}
// For getting video from camera or avi
CvCapture* capture = 0;
// Images to capture the frame from video or camera or from file
IplImage *frame, *frame_copy = 0;
// Used for calculations
int optlen = strlen("--cascade=");
// Load the HaarClassifierCascade for face detection
cascade = (CvHaarClassifierCascade*)cvLoad(cascade_name,0,0,0 );
if( !cascade )
{
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
return -1;
}
storage = cvCreateMemStorage(0);
const char* input_name;
input_name = argv[1];
// avi video file
// Change AVI to CAM in cvCaptureFrom if camera capture desired
capture = cvCaptureFromAVI(input_name);
cvNamedWindow( "Make-A-Mask", 1 );
if( capture )
{
for(;;)
{
// Capture the frame and load it in IplImage
if( !cvGrabFrame( capture )){
break;
}
frame = cvRetrieveFrame( capture,0);
if( !frame ){
printf("no frame exists");
break;
}
// Allocate framecopy as the same size of the frame
if( !frame_copy )
frame_copy = cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U, frame->nChannels );
// Check the origin of image.
//If top left, copy the image frame to frame_copy.
if( frame->origin == IPL_ORIGIN_TL )
cvCopy( frame, frame_copy,0 );
// Else flip and copy the image
else
cvFlip( frame, frame_copy,0 );
// Detect and draw mask on top of face
detect_and_draw( frame_copy );
// Wait time between frames
if( cvWaitKey( 1 ) >= 0 )
break;
}
// Release the images and capture memory
cvReleaseImage( &frame_copy );
cvReleaseCapture( &capture );
}
cvDestroyWindow("Make-A-Mask");
cvReleaseHaarClassifierCascade(&cascade);
return 0;
}