forked from willdollman/Condensation-Person-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainTracker.c
635 lines (445 loc) · 20.5 KB
/
mainTracker.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
mainTracker.c - the brains of the operation
Takes input in the form of background model and then tracks people within a webcam feed or recorded video
Does 3D tracking when depth data is supplied
Required for 2D tracking:
webcam OR a video in uncompressed avi format
backMean.xml - statistical model of background, generated by Matlab program (mean)
backCov.xml - statistical model of background, generated by Matlab program (covariance)
Optional for 3D tracking:
stereo displacement video (uncompressed avi) generated by stereoProcess.c
Contained functions:
main() - executes the main get frame/classify/update condensation/display loop
drawPositionTrail() - draws the x,z positions in a window
drawTrackingBoxes() - draws tracking boxes around subjects, based on particle filtering results
drawHeatmap() - draws map of likelihoods; whiter values are more likely to belong to foreground
drawSpecial() - experiment, draws foreground pixels as blue
depthRectangle() - calculates the depth of the object contained within the input rectangle
2010-2011 Will Dollman <willdollman@gmail.com>
*/
#include <stdio.h>
#include <highgui.h>
#include <cv.h>
#include <cxcore.h>
#include <math.h>
#include <cvaux.h>
#include "mainTracker.h"
#include "particleFilter.c"
#include "imageLikelihood.c"
#include <time.h>//
unsigned char getPixel( IplImage* stereoFrame, int x, int y );
void drawHeatmap( int maxBox[10], CvMat *imgLikelihood, IplImage* heatFrame );
void drawTrackingBoxes( int personNum, int maxBox[10],
IplImage* frame, IplImage* stereoFrame );
void drawPositionTrail( int maxBox[10], int trailLength,
int depth[2], int planX1Pos[20],
int planX2Pos[20], int planZ1Pos[20], int planZ2Pos[20],
IplImage* positionFrame );
void drawSpecial( CvMat *imgLikelihood, IplImage* specialFrame );
void initCaptureFiles( int argc, char *argv[], CvCapture **capture, CvCapture **stereoCapture, int *webcamRun, int *enable3D );
// need to return positive or negative
void initCaptureFiles( int argc, char *argv[], CvCapture **capture, CvCapture **stereoCapture, int *webcamRun, int *enable3D ) {
if( argc < 3 ) {;
printf("Incorrect arguments:\n");
printf("Webcam: mainTracker mean.xml cov.xml\n");
printf("2D video: mainTracker mean.xml cov.xml video.avi");
printf("3D video: mainTracker mean.xml cov.xml video.avi stereo.avi\n");
exit(1);
}
char locationVideoActual[300];
char locationVideoStereo[300];
if( argc == 3 ) {
printf("Running on webcam\n");
*webcamRun = 1;
} else if( argc == 4 ) {
strncpy( locationVideoActual, argv[3], 300 );
} else if( argc == 5 ) {
strncpy( locationVideoActual, argv[3], 300 );
strncpy( locationVideoStereo, argv[4], 300 );
} else {
printf("Incorrect arguments:\n");
printf("Webcam: mainTracker mean.xml cov.xml\n");
printf("2D video: mainTracker mean.xml cov.xml video.avi");
printf("3D video: mainTracker mean.xml cov.xml video.avi stereo.avi\n");
exit(1);
}
if( *webcamRun ) {
*capture = cvCaptureFromCAM(0);
*stereoCapture = cvCreateFileCapture( "nonexistantfile.avi" );
} else {
*capture = cvCreateFileCapture( locationVideoActual );
*stereoCapture = cvCreateFileCapture( locationVideoStereo );
}
if ( !*capture ) {
fprintf( stderr, "Cannot open regular video file\n" );
exit(1);
} else {
fprintf( stderr, "\nRegular video loaded ok\n" );
}
if ( *stereoCapture ) {
printf("Depth file found, enabling 3D tracking\n");
*enable3D = 1;
} else {
fprintf( stderr, "Could not open depth file, using 2D tracking" );
*enable3D = 0;
}
}
int main( int argc, char *argv[] ) {
int webcamRun = 0;
CvCapture *capture = 0;
CvCapture *stereoCapture = 0;
CvCapture **cptPtr = &capture;
CvCapture **scptPtr = &stereoCapture;
initCaptureFiles( argc, argv, cptPtr, scptPtr, &webcamRun, &enable3D );
char locationMean[300];
char locationCov[300];
strncpy( locationMean, argv[1], 300 );
strncpy( locationCov, argv[2], 300 );
/* check for stereo file */
if( !enable3D ) {
displayStereoFrame = 0;
displayPositionFrame = 0;
depthScaling = 0;
}
/* Fast-forwards through the video to the action */
int kl;
for( kl = 0; kl < 100 && webcamRun == 0; kl++ ) {
cvQueryFrame( capture );
if( enable3D ) cvQueryFrame( stereoCapture );
}
/*
Housekeeping
*/
int maxBox[10];
// this should be in the relevant file
//! Create matrix files to hold intermediate calculations for likelihood
/* this should have its own matrix setup function and not be hard-coded */
//! Create N-dimensional matrices to hold mean and cov data
int backMeanSizes[] = {_sizeY,_sizeX,4,_numResponses};
int backCovSizes[] = {_sizeY,_sizeX,4,_numResponses};
CvMatND *backMean = cvCreateMatND( 4, backMeanSizes, CV_32F );
CvMatND *backCov = cvCreateMatND( 4, backCovSizes, CV_32F );
/* Fix this */
if( webcamRun ) {
backMean = (CvMatND*) cvLoad( "D:/Will/Dropbox/My Dropbox/Project/Matlab/backMean.xml", NULL, NULL, NULL );
backCov = (CvMatND*) cvLoad( "D:/Will/Dropbox/My Dropbox/Project/Matlab/backCov.xml", NULL, NULL, NULL );
} else {
backMean = (CvMatND*) cvLoad( locationMean, NULL, NULL, NULL );
backCov = (CvMatND*) cvLoad( locationCov, NULL, NULL, NULL );
}
/* end here */
CvMat *imgLikelihood = cvCreateMat( 48, 64, CV_32F );
img = cvLoadImage( "foreground8/image (1).jpg", CV_LOAD_IMAGE_COLOR );
yImg = cvLoadImage( "foreground8/image (1).jpg", CV_LOAD_IMAGE_COLOR );
cvNamedWindow( "Tracker", CV_WINDOW_AUTOSIZE );
/* Condensation stuff */
ConDens = cvCreateConDensation( DP, MP, nSamples );
bx = 320; by = 240;
//hm = 0; //vm = 0;
/* Initialize the random number generator */
rng_state = cvRNG(0xffffffff);
initializeCondensation();
/* This allows us to change the probabiity with which Condensation alters each variable */
cvRandInit( &(ConDens->RandS[0]), -75, 75, 0, CV_RAND_UNI);
cvRandInit( &(ConDens->RandS[1]), -5, 5, 1, CV_RAND_UNI);
cvRandInit( &(ConDens->RandS[2]), -5, 5, 2, CV_RAND_UNI);
cvRandInit( &(ConDens->RandS[3]), -2, 2, 3, CV_RAND_UNI);
cvRandInit( &(ConDens->RandS[4]), -75, 75, 4, CV_RAND_UNI);
cvRandInit( &(ConDens->RandS[5]), -5, 5, 5, CV_RAND_UNI);
cvRandInit( &(ConDens->RandS[6]), -5, 5, 6, CV_RAND_UNI);
cvRandInit( &(ConDens->RandS[7]), -2, 2, 7, CV_RAND_UNI);
/*
If we have depth scaling, the depth controls the width & height of the box
So we don't want any randomness
*/
if( depthScaling ) {
cvRandInit( &(ConDens->RandS[3]), 0, 0, 3, CV_RAND_UNI);
cvRandInit( &(ConDens->RandS[7]), 0, 0, 7, CV_RAND_UNI);
}
IplImage* heatFrame = NULL;
heatFrame = cvQueryFrame( capture );
if(enable3D) cvQueryFrame( stereoCapture ); // making sure they stay in sync (and init positionFrame)
int frameNumb = 0;
// int mjk;
// for( mjk = 0; mjk < 320; mjk++ ) {
// cvQueryFrame( capture );
// cvQueryFrame( stereoCapture );
// frameNumb++;
// }
positionFrame = cvCreateImage(cvSize(640, 510), 8, 3);
int trailLength = 20;
int planX1Pos[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int planX2Pos[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int planZ1Pos[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int planZ2Pos[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int key = 0;
double totalTime = 0;
int totalFrames = 0;
while( key != 'q' ) {
// stopCount++;
// strcpy(frameNameTemp, frameName);
// itoa(stopCount, frameCount, 10);
// strcat( frameNameTemp, frameCount);
// strcat(frameNameTemp, ".jpg");
// cvSaveImage(frameNameTemp, positionFrame, 0);
/* Start timing */
clock_t start = clock();
frameNumb++;
printf("Frame %d\n", frameNumb);
if( frameNumb == 210 ) {
for( ; frameNumb < 290; frameNumb++ ) {
cvQueryFrame( capture );
cvQueryFrame( stereoCapture );
}
}
if( frameNumb == 350 ) {
return(0);
}
/* Get the first video frame, and maybe stereo frame */
frame = cvQueryFrame( capture );
if(enable3D) stereoFrame = cvQueryFrame( stereoCapture );
/* Double-check that we haven't reached the end */
if( !frame ) break;
if( enable3D ) if( !stereoFrame ) break;
/* Compute likelihoods for new frame, using mean and cov */
likelihood( frame, backMean, backCov, imgLikelihood );
/* Update the Condensation model using new likelihoods */
updateCondensation( 0, 0, imgLikelihood, maxBox );
/* Maybe display the Condensation particles */
if( displayParticles )
drawParticles();
/* Draw tracking boxes onto the video and stereo video frame */
drawTrackingBoxes( 0, maxBox, frame, stereoFrame );
drawTrackingBoxes( 1, maxBox, frame, stereoFrame );
/* Show the latest video frame (with boxes and particles) */
cvShowImage( "Tracker", frame );
/* Maybe show latest stereo depth frame */
if( displayStereoFrame )
cvShowImage( "Stereo", stereoFrame );
/* Maybe show latest position map */
if( displayPositionFrame )
drawPositionTrail( maxBox, trailLength,
depth,
planX1Pos, planX2Pos, planZ1Pos,
planZ2Pos, positionFrame );
/* Maybe show the heat map of the input image (the likelihood for each patch) */
if( displayHeatFrame ) {
drawHeatmap( maxBox, imgLikelihood, heatFrame );
cvShowImage( "Heat", heatFrame );
}
/* Update previous x, y positions */
prevX[0] = maxBox[0]; prevY[0] = maxBox[1];
prevX[1] = maxBox[4]; prevY[1] = maxBox[5];
/* Calculate fps and average fps */
printf("fps: %3.1f, ", (double)1/(((double)clock() - start) / CLOCKS_PER_SEC));
totalTime = totalTime + (double)1/(((double)clock() - start) / CLOCKS_PER_SEC);
totalFrames++;
printf(" average fps %3.1f", totalTime/totalFrames);
/* Check for key presses */
key = cvWaitKey( 50 );
/* Enable debugging mode */
if( key == 'd' ) {
debug = 1;
printf("\n*********\n\nDebug: ON\n\n*********\n");
}
/* Enable debugging mode */
if( key == 'p' ) {
if( stateDraw )
stateDraw = 0;
else
stateDraw = 1;
printf("\n*********\n\nParticle Drawing Toggled\n\n*********\n");
}
}
printf("\n\nquitting");
//cvReleaseVideoWriter( &writerTracker );
cvReleaseImage( &img );
cvReleaseImage( &frame );
cvDestroyWindow( "Tracker" );
cvDestroyWindow( "Heat" );
cvDestroyWindow( "Stereo" );
cvDestroyWindow( "Position" );
return(0);
}
//! Draw the position window - displays the x,z position of tracked objects
void drawPositionTrail( int maxBox[10], int trailLength,
int depth[2], int planX1Pos[20],
int planX2Pos[20], int planZ1Pos[20], int planZ2Pos[20],
IplImage* positionFrame )
{
int xzPos;
cvCircle( positionFrame, cvPoint( planX1Pos[0], planZ1Pos[0] ), 5, CV_RGB ( 0, 0, 0 ) , 1,8,0);
cvCircle( positionFrame, cvPoint( planX2Pos[0], planZ2Pos[0] ), 5, CV_RGB ( 0, 0, 0 ) , 1,8,0);
/* Move all objects up the "queue" */
for( xzPos = 0; xzPos < trailLength; xzPos++ ) {
planX1Pos[xzPos] = planX1Pos[xzPos+1];
planX2Pos[xzPos] = planX2Pos[xzPos+1];
planZ1Pos[xzPos] = planZ1Pos[xzPos+1];
planZ2Pos[xzPos] = planZ2Pos[xzPos+1];
}
/* Depending on whether an object was found, add their position or don't */
if( person[0] ) {
planX1Pos[trailLength-1] = maxBox[0];
planZ1Pos[trailLength-1] = depth[0] * 2;
} else {
planX1Pos[trailLength-1] = 1000;
planZ1Pos[trailLength-1] = 1000;
}
if( person[1] ) {
planX2Pos[trailLength-1] = maxBox[4];
planZ2Pos[trailLength-1] = depth[1] * 2;
} else {
planX2Pos[trailLength-1] = 1000;
planZ2Pos[trailLength-1] = 1000;
}
/* Blank out the previous image */
/* Build new image frame using "queue", with fancy fade transitions */
for( xzPos = 0; xzPos < trailLength; xzPos++ ) {
if( xzPos == trailLength - 1 ) {
cvCircle( positionFrame, cvPoint( planX1Pos[xzPos], planZ1Pos[xzPos] ), 5, CV_RGB ( 255, 255, 255 ) , 1,8,0);
cvCircle( positionFrame, cvPoint( planX2Pos[xzPos], planZ2Pos[xzPos] ), 5, CV_RGB ( 255, 0, 0 ) , 1,8,0);
} else {
cvCircle( positionFrame, cvPoint( planX1Pos[xzPos], planZ1Pos[xzPos] ), 5, CV_RGB ( (xzPos*7)+94, (xzPos*7)+94, (xzPos*7)+94 ) , 1,8,0);
cvCircle( positionFrame, cvPoint( planX2Pos[xzPos], planZ2Pos[xzPos] ), 5, CV_RGB ( (xzPos*7)+94, 0, 0 ) , 1,8,0);
}
}
// Display our hard work
if( displayPositionFrame ) cvShowImage( "Position", positionFrame );
}
//! Draw boxes around the subjects we've found
void drawTrackingBoxes( int personNum, int maxBox[10], IplImage* frame, IplImage* stereoFrame ) {
int process = 0;
int colour[3];
int base = personNum * 4;
// hack while we still refer to them as persons p and q
if( personNum == 0 ) {
if( person[0] == 1 ) {
process = 1;
}
colour[0] = 255; colour[1] = 255; colour[2] = 255;
} else if (personNum == 1 ) {
if( person[1] == 1) {
process = 1;
}
colour[0] = 255; colour[1] = 0; colour[2] = 0;
}
/* if 3D is enabled, get the depth within this rectangle */
if( enable3D ) depth[personNum] = depthRectangle( cvPoint(maxBox[base+0], maxBox[base+1] ), cvPoint( maxBox[base+0] + maxBox[base+2], maxBox[base+1] + maxBox[base+3] ) );
/* Cap minimum depth to prevent box scaling problems */
if( depth[personNum] < minDepth )
depth[personNum] = minDepth;
if(personNum==0) printf("\nDepth: %d | ",depth[0]);
if(personNum==1) printf("%d\n",depth[1]);
// 'process' refers to whether the object was found or not
if( process ) {
/* draw the body box in the stereo window */
if( displayStereoFrame ) cvRectangle(stereoFrame, cvPoint(maxBox[base+0], maxBox[base+1] ), cvPoint(maxBox[base+0] + maxBox[base+2], maxBox[base+1] + maxBox[base+3] ), CV_RGB ( colour[0], colour[1], colour[2] ),1, 8, 0);
/* draw the body box in the video window */
cvRectangle(frame, cvPoint(maxBox[base+0], maxBox[base+1] ), cvPoint(maxBox[base+0] + maxBox[base+2], maxBox[base+1] + maxBox[base+3] ), CV_RGB ( colour[0], colour[1], colour[2] ),4, 8, 0);
/* draw the head box in the video window */
cvRectangle(frame, cvPoint( maxBox[base+0] + ( maxBox[base+2]/3 ), maxBox[base+1] - ( maxBox[base+3]/4 ) ), cvPoint( maxBox[base+0] + ( 2*maxBox[base+2]/3 ), maxBox[base+1] ), CV_RGB ( colour[0], colour[1], colour[2] ),4, 8, 0);
/* draw the speed box in the video window */
cvRectangle(frame, cvPoint( maxBox[base+0] , maxBox[base+1] ), cvPoint( maxBox[base+0] + maxBox[personNum+8], maxBox[base+1] + 10 ), CV_RGB ( colour[0], colour[1], colour[2] ),1, 8, 0);
}
}
//! Displays the likelihood of each patch. Ligher = more likely to be foreground object
void drawHeatmap( int maxBox[10], CvMat* imgLikelihood, IplImage* heatFrame ) {
int xHeat, yHeat, tempHeat, heatColour;
// For every patch, threshold the values and assign a shade of white/grey
for( yHeat = 0; yHeat < 48; yHeat++ ) {
for( xHeat = 0; xHeat < 64; xHeat++ ) {
tempHeat = cvGetReal2D( imgLikelihood, yHeat, xHeat );
if( shadowRemove ) {
if( tempHeat < 15)
heatColour = 0;
else if( tempHeat < 30)
heatColour = 70;
else if( tempHeat < 70)
heatColour = 120;
else if( tempHeat < 100)
heatColour = 175;
else if( tempHeat >= 200)
heatColour = 255;
} else {
if( tempHeat < 200)
heatColour = 0;
else if( tempHeat < 600)
heatColour = 70;
else if( tempHeat < 1000)
heatColour = 120;
else if( tempHeat < 1400)
heatColour = 175;
else if( tempHeat >= 1400)
heatColour = 255;
}
// display a rectange of that colour
cvRectangle(heatFrame, cvPoint(xHeat*10, yHeat*10 ), cvPoint(xHeat*10+9, yHeat*10+9 ), CV_RGB ( heatColour, heatColour, heatColour ),CV_FILLED, 8, 0);
//if( special && heatColour < 175 ) cvRectangle(specialFrame, cvPoint(xHeat*10, yHeat*10 ), cvPoint(xHeat*10+9, yHeat*10+9 ), CV_RGB ( 0, 0, 0 ),CV_FILLED, 8, 0);
}
}
// draw tracking rectanges - different to the others in that they will always be there
// even if we haven't found an object to track
/* Don't need to be shown in the final version */
/*
cvRectangle(heatFrame, cvPoint(maxBox[0], maxBox[1] ), cvPoint(maxBox[0] + maxBox[2], maxBox[1] + maxBox[3] ), CV_RGB ( 255, 255, 255 ),1, 8, 0);
cvRectangle(heatFrame, cvPoint( maxBox[0] + ( maxBox[2]/3 ), maxBox[1] - ( maxBox[3]/4 ) ), cvPoint( maxBox[0] + ( 2*maxBox[2]/3 ), maxBox[1] ), CV_RGB ( 255, 255, 255 ),1, 8, 0);
cvRectangle(heatFrame, cvPoint(maxBox[4], maxBox[5] ), cvPoint(maxBox[4] + maxBox[6], maxBox[5] + maxBox[7] ), CV_RGB ( 255, 0, 0 ),1, 8, 0);
cvRectangle(heatFrame, cvPoint( maxBox[4] + ( maxBox[6]/3 ), maxBox[5] - ( maxBox[7]/4 ) ), cvPoint( maxBox[4] + ( 2*maxBox[6]/3 ), maxBox[5] ), CV_RGB ( 255, 0, 0 ),1, 8, 0);
*/
}
//! Draw foreground patches as blue
void drawSpecial( CvMat* imgLikelihood, IplImage* theFrame ) {
int xHeat, yHeat, tempHeat, heatColour;
IplImage *specialFrame = 0;
specialFrame = cvCloneImage( theFrame );
// For every patch, threshold the values and assign a shade of white/grey
for( yHeat = 0; yHeat < 48; yHeat++ ) {
for( xHeat = 0; xHeat < 64; xHeat++ ) {
tempHeat = cvGetReal2D( imgLikelihood, yHeat, xHeat );
// display a rectange of that colour
if( tempHeat > 200 ) {
cvRectangle(specialFrame, cvPoint(xHeat*10, yHeat*10 ), cvPoint(xHeat*10+9, yHeat*10+9 ), CV_RGB ( 0,0,255 ),CV_FILLED, 8, 0);
}
}
}
cvShowImage( "Special Frame", specialFrame );
}
//! Returns a metric for the depth of a given rectangle
//! Sort of the median, but ignoring zero values
int depthRectangle( CvPoint topleft, CvPoint bottomright ) {
int i, j, median;
int totalPixels = 0;
int hist[256];
/* Zero the histogram */
for( i = 0; i < 256; i++ )
hist[i] = 0;
/* Bound checking */
if( topleft.x > 640) topleft.x = 640;
if( topleft.y > 480) topleft.y = 480;
if( bottomright.x > 640) bottomright.x = 640;
if( bottomright.y > 480) bottomright.y = 480;
// for every pixel in the rectangle, store their value in histogram form
for( j = topleft.y; j < bottomright.y; j++ ) {
for( i = topleft.x; i < bottomright.x; i++ ) {
hist[(int) getPixel( stereoFrame, i, j )]++;
totalPixels++;
}
}
/* we want to ignore 0-valued pixels, so subtract them */
totalPixels = totalPixels - hist[0];
/* find the median */
i = 0; median = 0;
while( i < totalPixels / 2 ) {
median++;
i = hist[median] + i;
}
return median;
}
// gets pixel
unsigned char getPixel(IplImage* image, // image
int x,
int y
)
{
return ((unsigned char*)(image->imageData + image->widthStep*y))[x*image->nChannels];
}