-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindBallse.c
366 lines (292 loc) · 10 KB
/
findBallse.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* findBallse.c
* ------------
* Copyright : (c) 2008, Xavier Lagorce <[email protected]>
* (c) 2011, Jeremie Dimino <[email protected]>
* Licence : BSD3
*
* This file is a part of [kro]bot.
*/
#include "cv.h"
#include "highgui.h"
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#include <err.h>
/* +-----------------------------------------------------------------+
| Parameters |
+-----------------------------------------------------------------+ */
/* The parameters of the color to find in images. */
struct color_params {
int centH, radH, minS, maxS, minV, maxV;
int threshold;
int minCont, maxCont;
};
/* Global parameters. */
struct color_params params;
/* Load the source image. HighGUI use. */
IplImage *image01 = 0, *image02 = 0, *image03 = 0, *imCont = 0, *imFill = 0, *imHSV = 0;
/* Whether to display the result on the screen or not using HighGUI. */
int display = 0;
CvMat* homography;
/* +-----------------------------------------------------------------+
| Config file parsing |
+-----------------------------------------------------------------+ */
/* Parse the configuration file and store the result into params. */
void parse_config(char *file_name)
{
FILE *fp = fopen(file_name, "r");
char *line = NULL;
size_t line_len = 0;
char key[1024];
int value;
while (getline(&line, &line_len, fp) != -1) {
char *p;
/* Skip blanks at the beginning of the line. */
for (p = line; *p && isblank(*p); p++);
/* Skip empty lines and comments. */
if (*p == 0 || *p == '#') continue;
/* Check that key won't overflow. */
if (strlen(line) >= 1024) errx(2, "line too long in configuration file");
/* Parse the line. */
if (sscanf(p, "%s = %d \n", &key, &value) != 2) errx(2, "invalid configuration file");
/* Assign the value to the right parameter. */
if (strcmp(key, "centH") == 0)
params.centH = value;
else if (strcmp(key, "radH") == 0)
params.radH = value;
else if (strcmp(key, "minS") == 0)
params.minS = value;
else if (strcmp(key, "maxS") == 0)
params.maxS = value;
else if (strcmp(key, "minV") == 0)
params.minV = value;
else if (strcmp(key, "maxV") == 0)
params.maxV = value;
else if (strcmp(key, "threshold") == 0)
params.threshold = value;
else if (strcmp(key, "minCont") == 0)
params.minCont = value;
else if (strcmp(key, "maxCont") == 0)
params.maxCont = value;
else
errx(2, "invalid key in configuration file: '%s'", key);
}
if (line) free(line);
fclose(fp);
}
/* Parse the homography configuration file and store the result into
homography. */
void parse_homography(char *file_name)
{
FILE *fp = fopen(file_name, "r");
int i;
for (i = 0; i < 8; i++) {
float x;
fscanf(fp, "%f\n", &x);
homography->data.fl[i] = x;
}
fclose(fp);
}
/* +-----------------------------------------------------------------+
| Image processing |
+-----------------------------------------------------------------+ */
int in_radius(int val, int center, int radius, int max)
{
int d;
d = abs(val - center);
return (d <= radius) || (max - d <= radius);
}
// This function the balls,
void process_image()
{
CvMemStorage* stor;
CvSeq* cont;
CvBox2D32f* box;
CvPoint* PointArray;
CvPoint2D32f* PointArray2D32f;
int i, j, meanRad;
CvMat* src;
CvMat* dst;
// Changement d'espace de couleur
cvCvtColor(image01, imHSV, CV_BGR2HSV);
// Génération de l'image avec les résultats
if (display != 0)
{
imCont = cvCloneImage(image01);
cvZero(imCont);
// On fixe la ROI de l'image pour ne pas dessiner à l'extérieur
imCont->roi = (_IplROI*)malloc(sizeof(IplROI));
imCont->roi->coi = 0;
imCont->roi->xOffset = 0;
imCont->roi->yOffset = 0;
imCont->roi->width = imCont->width;
imCont->roi->height = imCont->height;
}
// On fait le traitement pour chaque couleur de balle
imFill = cvCloneImage(image01);
for (i=0 ; i < imFill->width*imFill->height; i++)
{
if (in_radius(imHSV->imageData[3*i], params.centH, params.radH, 180) &&
(uchar)imHSV->imageData[3*i+1] >= params.minS && (uchar)imHSV->imageData[3*i+1] <= params.maxS &&
(uchar)imHSV->imageData[3*i+2] >= params.minV && (uchar)imHSV->imageData[3*i+2] <= params.maxV )
{
// On laisse la couleur du pixel
}
else
{
// sinon on l'efface
imFill->imageData[3*i ] = 0;
imFill->imageData[3*i+1] = 0;
imFill->imageData[3*i+2] = 0;
}
}
// Conversion en niveaux de gris de l'image filtrée
cvCvtColor(imFill, image03, CV_BGR2GRAY);
// Create the destination images
image02 = cvCloneImage( image03 );
// Create dynamic memory storage and sequence.
stor = cvCreateMemStorage(0);
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
/* Print a separator. */
printf("=====\n");
if (cont) {
// Threshold the source image. This needful for cvFindContours().
cvThreshold( image03, image02, params.threshold, 255, CV_THRESH_BINARY );
// Find all contours.
cvFindContours( image02, stor, &cont, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
// Clear image. IPL use.
cvZero(image02);
// This cycle draw all contours and approximate it by ellipses.
for(;cont;cont = cont->h_next) {
int i; // Indicator of cycle.
int count = cont->total; // This is number point in contour
CvPoint center;
CvSize size;
// Number point must be more than or equal to 6 (for cvFitEllipse_32f).
if( count < 6 )
continue;
// Alloc memory for contour point set.
PointArray = (CvPoint*)malloc( count*sizeof(CvPoint) );
PointArray2D32f= (CvPoint2D32f*)malloc( count*sizeof(CvPoint2D32f) );
// Alloc memory for ellipse data.
box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
// Get contour point set.
cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
// Convert CvPoint set to CvBox2D32f set.
for(i=0; i<count; i++) {
PointArray2D32f[i].x = (float)PointArray[i].x;
PointArray2D32f[i].y = (float)PointArray[i].y;
}
// Fits ellipse to current contour.
cvFitEllipse(PointArray2D32f, count, box);
// Convert ellipse data from float to integer representation.
center.x = cvRound(box->center.x);
center.y = cvRound(box->center.y);
size.width = cvRound(box->size.width*0.5);
size.height = cvRound(box->size.height*0.5);
box->angle = -box->angle;
// On ne dessine le contour et l'ellipse que si son rayon est dans les bornes
meanRad = (size.width + size.height) / 2;
if (meanRad >= params.minCont && meanRad <= params.maxCont) {
// tansform matched elipsis to table coordinates
src = cvCreateMat(1, 1, CV_32FC2);
dst = cvCreateMat(1, 1, CV_32FC2);
src->data.fl[0] = (float) center.x;
dst->data.fl[1] = (float) center.y;
cvPerspectiveTransform(src, dst, homography);
/* Print ellipsis parameters on stdout. */
printf("%f %f\n", dst->data.fl[0], dst->data.fl[1]);
if (display) {
// Draw current contour.
cvDrawContours(imCont,cont,CV_RGB(255,255,255),CV_RGB(255,255,255),0,1,8,cvPoint(0,0));
// Draw ellipse.
cvEllipse(imCont, center, size,
box->angle, 0, 360,
CV_RGB(255,0,0), 1, CV_AA, 0);
}
}
// Free memory.
free(PointArray);
free(PointArray2D32f);
free(box);
}
}
fflush(stdout);
// On libère la mémoire
if (display) {
free(imCont->roi);
imCont->roi = NULL;
}
cvReleaseMemStorage(&stor);
cvReleaseImage(&image02);
cvReleaseImage(&imFill);
}
/* +-----------------------------------------------------------------+
| Entry point |
+-----------------------------------------------------------------+ */
int main( int argc, char** argv )
{
int source, c, quit=0;
CvCapture *capture = NULL;
FILE *fichier;
// Traitement des paramètres de ligne de commande
if (argc < 3)
{
printf("argv[1] : chemin du fichier de paramètres\n");
printf("argv[2] : chemin du fichier de paramètres pour l'homography\n");
printf("argv[3] : (optionnel) si différent de 0, affiche une fenêtre\n");
printf("argv[4] : (optionnel) numéro de la webcam à utiliser\n");
return 1;
}
/* Create and initialise homograpy matrix. */
homography = cvCreateMat(3, 3, CV_32F);
/* Parse configuration. */
parse_config(argv[1]);
/* Parse the homography matrix. */
parse_homography(argv[2]);
display = argc >= 4 ? atoi(argv[3]) : 0;
source = argc >= 5 ? atoi(argv[4]) : 0;
// Ouvre la webcam
capture = cvCaptureFromCAM(source);
// Create window
if (display != 0)
cvNamedWindow("Result", 1);
// Récupère une image de la webcam
image01 = cvQueryFrame(capture);
// Création de l'image en niveaux de gris à la taile de l'image prise par la webcam
image03 = cvCreateImage(cvSize(image01->width,image01->height), IPL_DEPTH_8U, 1);
// Même chose avec l'image pour conversion
imHSV = cvCreateImage(cvSize(image01->width,image01->height), IPL_DEPTH_8U, 3);
while (!quit)
{
// Récupère une image de la webcam
image01 = cvQueryFrame(capture);
process_image();
// Show the image
if (display != 0) {
cvShowImage("Source", image01);
cvShowImage("Result", imCont);
}
// Wait for a key stroke; the same function arranges events processing
c = (char)cvWaitKey(10);
switch (c)
{
case 'q':
quit = 1;
break;
}
cvReleaseImage(&imCont);
}
// On release la mémoire
cvReleaseCapture(&capture);
cvReleaseImage(&image02);
cvReleaseImage(&image03);
cvReleaseImage(&imHSV);
cvReleaseImage(&imFill);
cvReleaseImage(&imCont);
if (display != 0)
cvDestroyWindow("Result");
return 0;
}