-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualDrumset.cpp
1476 lines (1211 loc) · 43.2 KB
/
VisualDrumset.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
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// VisualDrumset.cpp : Defines the entry point for the console application.
//
/*
Senior Design Project - Visual Drumset with Stereovision using OpenCV
Kalev Roomann-Kurrik
Spring 2011
Last Updated: Wednesday 4/26/2011
- System uses two Playstation Eye cameras to obtain two live feeds of the user.
- The two cameras undergo stereo calibration.
- After calibration marked drumsticks are tracked in the left camera feed.
- The x and y image coordinates of the marked drumsticks along with their corresponding
disparities from the stereo calibrated system are used to find the real world 3D
position of the drumsticks with respect to the camera setup.
- The 3D position of the drumsticks is then used for gesture recognition which generates
events with sound feedback to emulate the action of an acoustic drumset.
*/
#include "stdafx.h"
#include "cxmisc.h"
#include "cvaux.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include <windows.h>
using namespace std;
// Sample camera capture class
class CLEyeCameraCapture
{
CHAR _windowName[256];
GUID _cameraGUID;
CLEyeCameraInstance _cam;
CLEyeCameraColorMode _mode;
CLEyeCameraResolution _resolution;
float _fps;
HANDLE _hThread;
bool _running;
IplImage *pCapImage;
public:
CLEyeCameraCapture(LPSTR windowName, GUID cameraGUID, CLEyeCameraColorMode mode, CLEyeCameraResolution resolution, float fps) :
_cameraGUID(cameraGUID), _cam(NULL), _mode(mode), _resolution(resolution), _fps(fps), _running(false)
{
strcpy(_windowName, windowName);
}
bool StartCapture()
{
_running = true;
cvNamedWindow(_windowName, CV_WINDOW_AUTOSIZE);
// Start CLEye image capture thread
_hThread = CreateThread(NULL, 0, &CLEyeCameraCapture::CaptureThread, this, 0, 0);
if(_hThread == NULL)
{
//MessageBox(NULL,"Could not create capture thread","CLEyeMulticamTest", MB_ICONEXCLAMATION);
return false;
}
return true;
}
void StopCapture()
{
if(!_running) return;
_running = false;
WaitForSingleObject(_hThread, 1000);
cvDestroyWindow(_windowName);
}
// New function written by Kalev to return IplImage captured by camera from thread to main function
IplImage * getImage()
{
return pCapImage;
}
bool isRunning()
{
if(_running)
{
return true;
}
else
{
return false;
}
}
void Run()
{
int w, h;
//IplImage *pCapImage;
PBYTE pCapBuffer = NULL;
// Create camera instance
_cam = CLEyeCreateCamera(_cameraGUID, _mode, _resolution, _fps);
if(_cam == NULL) return;
// Get camera frame dimensions
CLEyeCameraGetFrameDimensions(_cam, w, h);
// Depending on color mode chosen, create the appropriate OpenCV image
if(_mode == CLEYE_COLOR_PROCESSED || _mode == CLEYE_COLOR_RAW)
pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 4);
else
pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1);
// Set some camera parameters
CLEyeSetCameraParameter(_cam, CLEYE_GAIN, 0);
CLEyeSetCameraParameter(_cam, CLEYE_EXPOSURE, 511);
CLEyeSetCameraParameter(_cam, CLEYE_ZOOM, (int)(1.0));
CLEyeSetCameraParameter(_cam, CLEYE_ROTATION, (int)(0.0));
// Start capturing
CLEyeCameraStart(_cam);
cvGetImageRawData(pCapImage, &pCapBuffer);
// image capturing loop
while(_running)
{
CLEyeCameraGetFrame(_cam, pCapBuffer);
//cvShowImage(_windowName, pCapImage);
}
// Stop camera capture
CLEyeCameraStop(_cam);
// Destroy camera object
CLEyeDestroyCamera(_cam);
// Destroy the allocated OpenCV image
cvReleaseImage(&pCapImage);
_cam = NULL;
}
static DWORD WINAPI CaptureThread(LPVOID instance)
{
// seed the rng with current tick count and thread id
srand(GetTickCount() + GetCurrentThreadId());
// forward thread to Capture function
CLEyeCameraCapture *pThis = (CLEyeCameraCapture *)instance;
pThis->Run();
return 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
// Ask user whether they want normal mode or debug mode
// Normal mode is continuous frame capture after calibration
// Debug mode is waiting after each frame for user input in order to get
// the next frame so that intermediate values can be seen
int DEBUG;
printf("Select operating mode (0 = Normal 1 = Debug): ");
cin >> DEBUG;
printf("\n");
if(DEBUG == 0)
{
printf("You have selected Normal mode\n");
}
else
{
printf("You have selected Debug mode\n");
}
CLEyeCameraCapture *cam[2] = { NULL };
srand(GetTickCount());
// Query for number of connected cameras
int numCams = CLEyeGetCameraCount();
if(numCams == 0)
{
printf("No PS3Eye cameras detected\n");
return -1;
}
printf("Found %d cameras\n", numCams);
for(int i = 0; i < numCams; i++)
{
char windowName[64];
// Query unique camera uuid
GUID guid = CLEyeGetCameraUUID(i);
printf("Camera %d GUID: [%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x]\n",
i+1, guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2],
guid.Data4[3], guid.Data4[4], guid.Data4[5],
guid.Data4[6], guid.Data4[7]);
sprintf(windowName, "Camera Window %d", i+1);
// Create camera capture object
// Randomize resolution and color mode
cam[i] = new CLEyeCameraCapture(windowName, guid, CLEYE_COLOR_PROCESSED, CLEYE_QVGA, 30);
printf("Starting capture on camera %d\n", i+1);
cam[i]->StartCapture();
}
printf("outside of camera setup for loop\n");
// Images to hold camera feeds
IplImage *feed0;
IplImage *feed1;
// The <ESC> key will exit the program
CLEyeCameraCapture *pCam = NULL;
int param = -1, key;
printf("Press Esc to start capturing images\n");
while((key = cvWaitKey(0)) != 0x1b)
{
}
int frameCounter = 0;
// Arrays of captured images for calibration
//IplImage *leftCamCaptures[2] = { NULL };
//IplImage *rightCamCaptures[2] = { NULL };
printf("Capture 15 images of chessboard pattern\n");
// Allocate matrices and variables for points and parameters
int displayCorners = 1;
int showUndistorted = 1;
bool isVerticalStereo = false;
const int maxScale = 1;
const float squareSize = 1.f; // Currently 1 square = 1 unit (also equal to 1 inch)
int nx = 8;
int ny = 6;
int i,j, nframes, n=nx*ny, N = 0;
int result_left = 0;
int result_right = 0;
int count = 0;
int s = 1;
vector<CvPoint3D32f> objectPoints;
vector<CvPoint2D32f> points[2];
vector<CvPoint2D32f> temp(n);
vector<int> npoints;
vector<uchar> active[2];
// Arrays and vectors
double M1[3][3], M2[3][3], D1[5], D2[5];
double R[3][3], T[3], E[3][3], F[3][3];
CvMat _M1 = cvMat(3, 3, CV_64F, M1);
CvMat _M2 = cvMat(3, 3, CV_64F, M2);
CvMat _D1 = cvMat(1, 5, CV_64F, D1);
CvMat _D2 = cvMat(1, 5, CV_64F, D2);
CvMat _R = cvMat(3, 3, CV_64F, R);
CvMat _T = cvMat(3, 1, CV_64F, T);
CvMat _E = cvMat(3, 3, CV_64F, E);
CvMat _F = cvMat(3, 3, CV_64F, F);
CvSize imageSize = {0,0};
cvNamedWindow("corners", 1);
// Image holders for all 20 calibration images
IplImage* left1;
IplImage* left2;
IplImage* left3;
IplImage* left4;
IplImage* left5;
IplImage* left6;
IplImage* left7;
IplImage* left8;
IplImage* left9;
IplImage* left10;
IplImage* left11;
IplImage* left12;
IplImage* left13;
IplImage* left14;
IplImage* left15;
IplImage* right1;
IplImage* right2;
IplImage* right3;
IplImage* right4;
IplImage* right5;
IplImage* right6;
IplImage* right7;
IplImage* right8;
IplImage* right9;
IplImage* right10;
IplImage* right11;
IplImage* right12;
IplImage* right13;
IplImage* right14;
IplImage* right15;
feed0 = cam[0]->getImage();
// Setup of variables for object tracking and gesture recognition
IplImage* prev_frame = cvCreateImage(cvGetSize(feed0), 8, 3);
IplImage* grayFrame = cvCreateImage(cvGetSize(feed0), IPL_DEPTH_8U, 1);
IplImage* grayPrevFrame = cvCreateImage(cvGetSize(feed0), IPL_DEPTH_8U, 1);
// Component images
IplImage* red = cvCreateImage(cvGetSize(feed0), 8, 1);
IplImage* green = cvCreateImage(cvGetSize(feed0), 8, 1);
IplImage* blue = cvCreateImage(cvGetSize(feed0), 8, 1);
// HSV image
IplImage* hsvFrame = cvCreateImage(cvGetSize(feed0), 8, 3);
// Threshold image
IplImage* threshFrame = cvCreateImage(cvGetSize(feed0), 8, 1);
// First capture 15 sets of images for calibration
while(frameCounter < 15)
{
// Take pictures of chessboard pattern for calibration
// Space bar takes a new image
if(cam[0]->isRunning() == true && cam[1]->isRunning() == true)
{
feed0 = cam[0]->getImage();
feed1 = cam[1]->getImage();
cvShowImage("Camera Window 1", feed0);
cvShowImage("Camera Window 2", feed1);
if((key = cvWaitKey(5)) == 0x20)
{
// Get Chessboard points from two images
printf("Performing stereo calibration\n");
printf("Working on left cam image %d\n",frameCounter+1);
IplImage* timg_left = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,timg_left);
IplImage* timg_right = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,timg_right);
switch(frameCounter)
{
case 0:
left1 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left1);
right1 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right1);
break;
case 1:
left2 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left2);
right2 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right2);
break;
case 2:
left3 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left3);
right3 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right3);
break;
case 3:
left4 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left4);
right4 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right4);
break;
case 4:
left5 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left5);
right5 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right5);
break;
case 5:
left6 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left6);
right6 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right6);
break;
case 6:
left7 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left7);
right7 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right7);
break;
case 7:
left8 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left8);
right8 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right8);
break;
case 8:
left9 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left9);
right9 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right9);
break;
case 9:
left10 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left10);
right10 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right10);
break;
case 10:
left11 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left11);
right11 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right11);
break;
case 11:
left12 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left12);
right12 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right12);
break;
case 12:
left13 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left13);
right13 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right13);
break;
case 13:
left14 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left14);
right14 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right14);
break;
case 14:
left15 = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,left15);
right15 = cvCreateImage(cvGetSize(feed1),8,4);
cvCopyImage(feed1,right15);
break;
default:
break;
}
printf("temp image created\n");
imageSize = cvGetSize(feed0);
vector<CvPoint2D32f>& pts = points[0];
printf("Finding chessboard corners\n");
result_right = cvFindChessboardCorners(timg_right,cvSize(nx,ny),&temp[0],&count,CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);
result_left = cvFindChessboardCorners(timg_left,cvSize(nx,ny),&temp[0],&count,CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);
printf("result_left = %d\n", result_left);
printf("result_right = %d\n", result_right);
// if not all of the corners were notify the user to try again
if((result_right == 0) || (result_left == 0))
{
printf("Not all of the corners were found for those two views\n");
printf("Please try again\n");
}
// if all the corners were found in both images then proceed
if((result_right != 0) && (result_left != 0))
{
printf("Found chessboard corners\n");
if(timg_left != feed0)
cvReleaseImage(&timg_left);
if(result_left || s == maxScale)
{
for(j=0; j < count; j++)
{
temp[j].x /= s;
temp[j].y /= s;
}
}
printf("Displaying corners\n");
if(displayCorners)
{
IplImage* cimg = cvCreateImage(cvGetSize(feed0),8,4);
cvCopyImage(feed0,cimg);
cvDrawChessboardCorners(cimg, cvSize(nx,ny), &temp[0], count, result_left);
cvShowImage("corners",cimg);
cvReleaseImage(&cimg);
printf("Press space bar to continue\n");
if(cvWaitKey(0) == 32) // Space bar to continue
{
}
}
N = pts.size();
pts.resize(N + n, cvPoint2D32f(0,0));
active[0].push_back((uchar)result_left);
// Subpixel accuracy
if(result_left)
{
IplImage *gray_image = cvCreateImage(imageSize,8,1);
cvCvtColor(feed0, gray_image, CV_BGR2GRAY);
cvFindCornerSubPix(gray_image, &temp[0], count, cvSize(11,11), cvSize(-1,-1),cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,0.1));
copy(temp.begin(),temp.end(), pts.begin() + N);
}
printf("Done with left cam image %d\n",frameCounter);
printf("Working on right cam image %d\n",frameCounter);
cvCopyImage(feed1,timg_right);
printf("Copied feed1 to timg_right\n");
imageSize = cvGetSize(feed1);
printf("set imageSize\n");
vector <CvPoint2D32f>& pts2 = points[1];
printf("set pts\n");
result_right = cvFindChessboardCorners(timg_right,cvSize(nx,ny),&temp[0],&count,CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);
printf("Called cvFindChessboardCorners\n");
if(timg_right != feed1)
cvReleaseImage(&timg_right);
if(result_right || s == maxScale)
{
for(j=0; j < count; j++)
{
temp[j].x /= s;
temp[j].y /= s;
}
}
if(displayCorners)
{
IplImage *cimg = cvCreateImage(imageSize,8,4);
cvCopyImage(feed1,cimg);
cvDrawChessboardCorners(cimg, cvSize(nx,ny), &temp[0], count, result_right);
cvShowImage("corners",cimg);
cvReleaseImage(&cimg);
printf("Press space bar to continue\n");
if(cvWaitKey(0) == 32) // Space bar to continue
{
}
}
N = pts2.size();
pts2.resize(N + n, cvPoint2D32f(0,0));
active[1].push_back((uchar)result_right);
// Subpixel accuracy
if(result_right)
{
IplImage *gray_image = cvCreateImage(imageSize,8,1);
cvCvtColor(feed1,gray_image,CV_BGR2GRAY);
printf("Performing subpixel accuracy\n");
cvFindCornerSubPix(gray_image, &temp[0], count, cvSize(11,11), cvSize(-1,-1),cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,0.1));
copy(temp.begin(),temp.end(), pts2.begin() + N);
}
printf("Done with right cam image %d\n",frameCounter);
// Increment frame counter
frameCounter++;
}
}
}
}
// Get number of good chessboard patterns found
nframes = active[0].size();
printf("Number of good left patterns found = %d\n",nframes);
nframes = active[1].size();
printf("Number of good right patterns found = %d\n",nframes);
// Get Chessboard 3D Object point list
objectPoints.resize(nframes*n);
for(i=0; i<ny; i++)
{
for(j=0; j<nx; j++)
{
objectPoints[i*nx + j] = cvPoint3D32f(i*squareSize, j*squareSize, 0);
}
}
printf("Setup object points\n");
for(i=1; i <nframes; i++)
{
copy(objectPoints.begin(), objectPoints.begin() + n, objectPoints.begin() + i*n);
}
printf("Copied object points\n");
npoints.resize(nframes,n);
N = nframes * n;
CvMat _objectPoints = cvMat(1, N, CV_32FC3, &objectPoints[0]);
CvMat _imagePoints1 = cvMat(1, N, CV_32FC2, &points[0][0]);
CvMat _imagePoints2 = cvMat(1, N, CV_32FC2, &points[1][0]);
CvMat _npoints = cvMat(1, npoints.size(), CV_32S, &npoints[0]);
cvSetIdentity(&_M1);
cvSetIdentity(&_M2);
cvZero(&_D1);
cvZero(&_D2);
// Perform Stereo Calibration
printf("Performing actual calibration\n");
fflush(stdout);
cvStereoCalibrate(&_objectPoints, &_imagePoints1, &_imagePoints2, &_npoints, &_M1, &_D1, &_M2, &_D2, imageSize, &_R, &_T, &_E, &_F, cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 100, 1e-5),CV_CALIB_FIX_ASPECT_RATIO + CV_CALIB_ZERO_TANGENT_DIST + CV_CALIB_SAME_FOCAL_LENGTH);
printf("done\n");
// Calibration check
vector<CvPoint3D32f> lines[2];
points[0].resize(N);
points[1].resize(N);
_imagePoints1 = cvMat(1, N, CV_32FC2, &points[0][0]);
_imagePoints2 = cvMat(1, N, CV_32FC2, &points[1][0]);
lines[0].resize(N);
lines[1].resize(N);
CvMat _L1 = cvMat(1, N, CV_32FC3, &lines[0][0]);
CvMat _L2 = cvMat(1, N, CV_32FC3, &lines[1][0]);
cvUndistortPoints(&_imagePoints1, &_imagePoints1, &_M1, &_D1, 0, &_M1);
cvUndistortPoints(&_imagePoints2, &_imagePoints2, &_M2, &_D2, 0, &_M2);
cvComputeCorrespondEpilines(&_imagePoints1, 1, &_F, &_L1);
cvComputeCorrespondEpilines(&_imagePoints2, 2, &_F, &_L2);
double avgError = 0;
for(i=0; i<N; i++)
{
double error = fabs(points[0][i].x*lines[1][i].x + points[0][i].y*lines[1][i].y + lines[1][i].z) + fabs(points[1][i].x*lines[0][i].x + points[1][i].y*lines[0][i].y + lines[0][i].z);
avgError += error;
}
printf("The average error = %g\n", avgError/(nframes*n));
// Rectification
CvMat* mx1 = cvCreateMat(imageSize.height, imageSize.width, CV_32F);
CvMat* my1 = cvCreateMat(imageSize.height, imageSize.width, CV_32F);
CvMat* mx2 = cvCreateMat(imageSize.height, imageSize.width, CV_32F);
CvMat* my2 = cvCreateMat(imageSize.height, imageSize.width, CV_32F);
CvMat* img1r = cvCreateMat(imageSize.height, imageSize.width, CV_8U);
CvMat* img2r = cvCreateMat(imageSize.height, imageSize.width, CV_8U);
CvMat* disp = cvCreateMat(imageSize.height, imageSize.width, CV_16S);
CvMat* vdisp = cvCreateMat(imageSize.height, imageSize.width, CV_8U);
CvMat* pair;
double R1[3][3], R2[3][3], P1[3][4], P2[3][4], Q[4][4];
CvMat _R1 = cvMat(3, 3, CV_64F, R1);
CvMat _R2 = cvMat(3, 3, CV_64F, R2);
CvMat _Q = cvMat(4,4,CV_64F, Q);
printf("Created matrices for rectification\n");
// Bouguet's Method (using calibrated stereo system)
CvMat _P1 = cvMat(3, 4, CV_64F, P1);
CvMat _P2 = cvMat(3, 4, CV_64F, P2);
cvStereoRectify(&_M1, &_M2, &_D1, &_D2, imageSize, &_R, &_T, &_R1, &_R2, &_P1, &_P2, &_Q, 0 /*CV_CALIB_ZERO_DISPARITY*/);
isVerticalStereo = fabs(P2[1][3]) > fabs(P2[0][3]);
printf("Called cvStereoRectify\n");
// maps for cvRemap()
cvInitUndistortRectifyMap(&_M1,&_D1,&_R1,&_P1,mx1,my1);
cvInitUndistortRectifyMap(&_M2,&_D2,&_R2,&_P2,mx2,my2);
printf("Called cvInitUndistortRectifyMap\n");
cvNamedWindow("rectified", 1);
// Rectify images and find disparity map
pair = cvCreateMat(imageSize.height,imageSize.width*2,CV_8UC3);
// Setup for finding stereo correspondences
CvStereoBMState *BMState = cvCreateStereoBMState();
assert(BMState != 0);
BMState->preFilterSize=41;
BMState->preFilterCap=31;
BMState->SADWindowSize=41;
BMState->minDisparity=-64;
BMState->numberOfDisparities=128;
BMState->textureThreshold=10;
BMState->uniquenessRatio=15;
IplImage* img1 = cvCreateImage(cvGetSize(left1),8,1);
IplImage* img2 = cvCreateImage(cvGetSize(right1),8,1);
printf("Performed setup for correspondences\n");
for(i=0; i < nframes; i++)
{
printf("Going through disparity map loop iteration %d\n",i);
switch(i)
{
case 0:
cvCvtColor(left1, img1, CV_BGR2GRAY);
cvCvtColor(right1, img2, CV_BGR2GRAY);
break;
case 1:
cvCvtColor(left2, img1, CV_BGR2GRAY);
cvCvtColor(right2, img2, CV_BGR2GRAY);
break;
case 2:
cvCvtColor(left3, img1, CV_BGR2GRAY);
cvCvtColor(right3, img2, CV_BGR2GRAY);
break;
case 3:
cvCvtColor(left4, img1, CV_BGR2GRAY);
cvCvtColor(right4, img2, CV_BGR2GRAY);
break;
case 4:
cvCvtColor(left5, img1, CV_BGR2GRAY);
cvCvtColor(right5, img2, CV_BGR2GRAY);
break;
case 5:
cvCvtColor(left6, img1, CV_BGR2GRAY);
cvCvtColor(right6, img2, CV_BGR2GRAY);
break;
case 6:
cvCvtColor(left7, img1, CV_BGR2GRAY);
cvCvtColor(right7, img2, CV_BGR2GRAY);
break;
case 7:
cvCvtColor(left8, img1, CV_BGR2GRAY);
cvCvtColor(right8, img2, CV_BGR2GRAY);
break;
case 8:
cvCvtColor(left9, img1, CV_BGR2GRAY);
cvCvtColor(right9, img2, CV_BGR2GRAY);
break;
case 9:
cvCvtColor(left10, img1, CV_BGR2GRAY);
cvCvtColor(right10, img2, CV_BGR2GRAY);
break;
case 10:
cvCvtColor(left11, img1, CV_BGR2GRAY);
cvCvtColor(right11, img2, CV_BGR2GRAY);
break;
case 11:
cvCvtColor(left12, img1, CV_BGR2GRAY);
cvCvtColor(right12, img2, CV_BGR2GRAY);
break;
case 12:
cvCvtColor(left13, img1, CV_BGR2GRAY);
cvCvtColor(right13, img2, CV_BGR2GRAY);
break;
case 13:
cvCvtColor(left14, img1, CV_BGR2GRAY);
cvCvtColor(right14, img2, CV_BGR2GRAY);
break;
case 14:
cvCvtColor(left15, img1, CV_BGR2GRAY);
cvCvtColor(right15, img2, CV_BGR2GRAY);
break;
default:
break;
}
printf("Selected images based on value of i\n");
if(img1 && img2)
{
CvMat part;
cvRemap(img1, img1r, mx1, my1);
cvRemap(img2, img2r, mx2, my2);
printf("Called cvRemap\n");
if(!isVerticalStereo)
{
cvFindStereoCorrespondenceBM(img1r, img2r, disp, BMState);
cvNormalize(disp, vdisp, 0, 256, CV_MINMAX);
cvNamedWindow("disparity");
cvShowImage("disparity", vdisp);
cvGetCols(pair, &part, 0, imageSize.width);
cvCvtColor(img1r, &part, CV_GRAY2BGR);
cvGetCols(pair, &part, imageSize.width,imageSize.width*2);
cvCvtColor(img2r, &part, CV_GRAY2BGR);
for(j=0; j<imageSize.height; j +=16)
{
cvLine(pair, cvPoint(0,j), cvPoint(imageSize.width*2,j),CV_RGB(0,255,0));
}
}
cvShowImage("rectified", pair);
if(cvWaitKey() == 27)
break;
}
}
printf("\nEsc exits\n");
int stop = 0;
// Setup object tracking and gesture recognition by running through algorithm once
// on last set of images from both cameras
// Matrices to store x,y locations of tracked features for two images in sequence
// (both left and right camera images)
int *firstXLeft = new int[100];
int *firstYLeft = new int[100];
int *firstXRight = new int[100];
int *firstYRight = new int[100];
int *secondXLeft = new int[100];
int *secondYLeft = new int[100];
int *secondXRight = new int[100];
int *secondYRight = new int[100];
for(int k=0;k<100;k++)
{
firstXLeft[k] = 0;
firstYLeft[k] = 0;
firstXRight[k] = 0;
firstYRight[k] = 0;
secondXLeft[k] = 0;
secondYLeft[k] = 0;
secondXRight[k] = 0;
secondYRight[k] = 0;
}
int firstCounterLeft = 0;
int firstCounterRight = 0;
int secondCounterLeft = 0;
int secondCounterRight = 0;
// Convert last capture from left camera to HSV colorspace
cvCvtColor(left15, hsvFrame, CV_BGR2HSV);
// Threshold in HSV colorspace
cvInRangeS(hsvFrame, cvScalar(0, 110, 30), cvScalar(9, 255, 130), threshFrame);
cvShowImage("Thresholded HSV Image Left", threshFrame);
// Process thresholded image block by block and see where the areas of greatest intensity are
// At those locations right now just draw boxes over the feed image and display that
int sum = 0;
// Go through with a 11x11 block across the 640x480 image
for(int y=6;y<(threshFrame->height-10);y=y+10)
{
// Get pointers to desired rows
uchar* pointer1 = (uchar*) (threshFrame->imageData + (y-5) * threshFrame->widthStep);
uchar* pointer2 = (uchar*) (threshFrame->imageData + (y-4) * threshFrame->widthStep);
uchar* pointer3 = (uchar*) (threshFrame->imageData + (y-3) * threshFrame->widthStep);
uchar* pointer4 = (uchar*) (threshFrame->imageData + (y-2) * threshFrame->widthStep);
uchar* pointer5 = (uchar*) (threshFrame->imageData + (y-1) * threshFrame->widthStep);
uchar* pointer6 = (uchar*) (threshFrame->imageData + (y) * threshFrame->widthStep);
uchar* pointer7 = (uchar*) (threshFrame->imageData + (y+1) * threshFrame->widthStep);
uchar* pointer8 = (uchar*) (threshFrame->imageData + (y+2) * threshFrame->widthStep);
uchar* pointer9 = (uchar*) (threshFrame->imageData + (y+3) * threshFrame->widthStep);
uchar* pointer10 = (uchar*) (threshFrame->imageData + (y+4) * threshFrame->widthStep);
uchar* pointer11 = (uchar*) (threshFrame->imageData + (y+5) * threshFrame->widthStep);
// Go through the current row of the image by intervals of 10
for(int x=6;x<(threshFrame->width-10);x=x+10)
{
// reset sum
sum = 0;
// add up pixel values in 11x11 box
for(int i = 0; i<6; i++)
{
if(i==0)
{
sum = sum + pointer1[x] + pointer2[x] + pointer3[x] + pointer4[x] + pointer5[x] + pointer6[x]
+ pointer7[x] + pointer8[x] + pointer9[x] + pointer10[x] + pointer11[x];
}
else
{
sum = sum + pointer1[x-i] + pointer1[x+i] + pointer2[x-i] + pointer2[x+i]
+ pointer3[x-i] + pointer3[x+i] + pointer4[x-i] + pointer4[x+i]
+ pointer5[x-i] + pointer5[x+i] + pointer6[x-i] + pointer6[x+i]
+ pointer7[x-i] + pointer7[x+i] + pointer8[x-i] + pointer8[x+i]
+ pointer9[x-i] + pointer9[x+i] + pointer10[x-1] + pointer10[x+i]
+ pointer11[x-i] + pointer11[x+i];
}
}
// if the sum of the values across the entire 11x11 window is above a certain
// threshold value then draw a rectangle at that location and store the
// x and y coordinates of the center of the window
if(sum>1500)
{
CvPoint point0 = cvPoint(x-5,y-5);
CvPoint point1 = cvPoint(x+5,y+5);
cvRectangle(left15,point0,point1,CV_RGB(255,0,0),2);
firstXLeft[firstCounterLeft] = x;
firstYLeft[firstCounterLeft] = y;
firstCounterLeft++;
}
}
}
// show image with tracked points in left camera feed window
cvShowImage("Camera Window 1", left15);
// repeat with last frame from right camera
// Convert last capture from left camera to HSV colorspace
cvCvtColor(right15, hsvFrame, CV_BGR2HSV);
// Threshold in HSV colorspace
cvInRangeS(hsvFrame, cvScalar(0, 110, 30), cvScalar(9, 255, 130), threshFrame);
cvShowImage("Thresholded HSV Image Right", threshFrame);
// Process thresholded image block by block and see where the areas of greatest intensity are
// At those locations right now just draw boxes over the feed image and display that
sum = 0;
// Go through with a 11x11 block across the 640x480 image
for(int y=6;y<(threshFrame->height-10);y=y+10)
{
// Get pointers to desired rows
uchar* pointer1 = (uchar*) (threshFrame->imageData + (y-5) * threshFrame->widthStep);
uchar* pointer2 = (uchar*) (threshFrame->imageData + (y-4) * threshFrame->widthStep);
uchar* pointer3 = (uchar*) (threshFrame->imageData + (y-3) * threshFrame->widthStep);
uchar* pointer4 = (uchar*) (threshFrame->imageData + (y-2) * threshFrame->widthStep);
uchar* pointer5 = (uchar*) (threshFrame->imageData + (y-1) * threshFrame->widthStep);
uchar* pointer6 = (uchar*) (threshFrame->imageData + (y) * threshFrame->widthStep);
uchar* pointer7 = (uchar*) (threshFrame->imageData + (y+1) * threshFrame->widthStep);
uchar* pointer8 = (uchar*) (threshFrame->imageData + (y+2) * threshFrame->widthStep);
uchar* pointer9 = (uchar*) (threshFrame->imageData + (y+3) * threshFrame->widthStep);
uchar* pointer10 = (uchar*) (threshFrame->imageData + (y+4) * threshFrame->widthStep);
uchar* pointer11 = (uchar*) (threshFrame->imageData + (y+5) * threshFrame->widthStep);
// Go through the current row of the image by intervals of 10
for(int x=6;x<(threshFrame->width-10);x=x+10)
{
// reset sum
sum = 0;
// add up pixel values in 11x11 box
for(int i = 0; i<6; i++)
{
if(i==0)
{
sum = sum + pointer1[x] + pointer2[x] + pointer3[x] + pointer4[x] + pointer5[x] + pointer6[x]
+ pointer7[x] + pointer8[x] + pointer9[x] + pointer10[x] + pointer11[x];
}
else
{
sum = sum + pointer1[x-i] + pointer1[x+i] + pointer2[x-i] + pointer2[x+i]
+ pointer3[x-i] + pointer3[x+i] + pointer4[x-i] + pointer4[x+i]
+ pointer5[x-i] + pointer5[x+i] + pointer6[x-i] + pointer6[x+i]
+ pointer7[x-i] + pointer7[x+i] + pointer8[x-i] + pointer8[x+i]
+ pointer9[x-i] + pointer9[x+i] + pointer10[x-1] + pointer10[x+i]
+ pointer11[x-i] + pointer11[x+i];
}
}
// if the sum of the values across the entire 11x11 window is above a certain
// threshold value then draw a rectangle at that location and store the
// x and y coordinates of the center of the window
if(sum>1500)
{
CvPoint point0 = cvPoint(x-5,y-5);
CvPoint point1 = cvPoint(x+5,y+5);
cvRectangle(right15,point0,point1,CV_RGB(255,0,0),2);
firstXRight[firstCounterRight] = x;
firstYRight[firstCounterRight] = y;
firstCounterRight++;
}
}
}
// show image with tracked points in left camera feed window
cvShowImage("Camera Window 2", right15);
// Hit detection flag used to see if a tracked point was inside of the
// hit cube in the previous frame
// If a tracked point was inside of the hit cube in the previous frame