-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontouring_binary_img.cpp
118 lines (105 loc) · 3.45 KB
/
contouring_binary_img.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
//Function will trace and display the contours in the picture.
#include <iostream>
#include <fstream>
#include <math.h>
#include "cv.h"
#include "highgui.h"
#include "colourslidebar.hpp"
#define CVX_RED CV_RGB(0xff,0x00,0x00)
#define CVX_GREEN CV_RGB(0x00,0xff,0x00)
#define CVX_BLUE CV_RGB(0x00,0x00,0xff)
using namespace std;
int main(int argc, char** argv){
IplImage* img = cvLoadImage( argv[1],CV_LOAD_IMAGE_GRAYSCALE );
IplImage* grey = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage* eroded = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage* color = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
CvMemStorage* storage = cvCreateMemStorage();
CvMemStorage* cHStorage = cvCreateMemStorage();
CvMemStorage* cHDStorage = cvCreateMemStorage();
CvMemStorage* faceStorage = cvCreateMemStorage();
CvSeq* contours = NULL;
CvSeq* convexHull = NULL;
CvSeq* convexHull2 = NULL;
CvSeq* convexityDefects = NULL;
CvSeq* faces;
CvSize trainer_size = cvSize(20,20);
CvHaarClassifierCascade* cascade = cvLoadHaarClassifierCascade("haarcascade_frontalface_alt2.xml",trainer_size);
CvMoments moments;
CvMoments* mooment;
cvNamedWindow("Raw Binary Image", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Eroded Image", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Contours", CV_WINDOW_AUTOSIZE);
cvCopy(img,grey,NULL);
cvErode(img, eroded,NULL,1);
int Nc = cvFindContours(eroded, storage, &contours,
sizeof(CvContour),
CV_RETR_EXTERNAL
);
cvCvtColor( grey, color, CV_GRAY2BGR);
faces = cvHaarDetectObjects(grey,cascade,faceStorage,1.2,2,CV_HAAR_DO_CANNY_PRUNING);
if(faces){
cout << "Found a face." << endl;
for(int i = 0; i< faces->total; i++){
CvRect* face_rect = (CvRect*) cvGetSeqElem(faces,i);
//add some output here. THIS IS WHERE I WAS.
CvPoint center_of_face = cvPoint((int)(face_rect->x + face_rect->width / 2),
(int)(face_rect->y + face_rect->height /2));
cvRectangle(color,
cvPoint(face_rect->x, face_rect->y),
cvPoint(face_rect->x + face_rect->width, face_rect->y + face_rect->height),
CV_RGB(255,0,0),
3
);
cvCircle(color, center_of_face, 20, CVX_GREEN, 2);
}
}
int d = -1;
int n = 0;
cvShowImage("Raw Binary Image", grey);
cvShowImage("Eroded Image", eroded);
cvShowImage("Contours", color);
cvWaitKey(0);
for(CvSeq* c = contours; c!=NULL; c= c->h_next){
if(c->total > 500){
convexHull = cvConvexHull2(c, cHStorage, CV_CLOCKWISE, 2);
convexHull2 = cvConvexHull2(c, cHDStorage, CV_CLOCKWISE, 0);
convexityDefects = cvConvexityDefects(c, convexHull2);
cvDrawContours(color,
convexHull,
CVX_GREEN,
CVX_BLUE,
0,
2,
8
);
cvDrawContours(color,
c,
CVX_RED,
CVX_BLUE,
0,
2,
8
);
cvMoments(convexHull,&moments);
int k = 0;
CvPoint center_of_mass = cvPoint((int)(moments.m10/moments.m00), (int)(moments.m01/moments.m00));
cvCircle(color, center_of_mass, 20, CVX_GREEN, 3);
for(int j = 0; j < convexityDefects -> total; ++j){
CvConvexityDefect* cDef = (CvConvexityDefect*)cvGetSeqElem( convexityDefects,j);
if(cDef->depth > 30){
k++;
cvCircle(color, *(cDef->depth_point), 5, CVX_BLUE, 2);
}
}
cvShowImage("Contours", color);
d = cvWaitKey(0);
}
if( d == 27){
cout << "Escape key received, terminating." << endl;
break;
}
n++;
}
return 0;
}