forked from KeckCAVES/SARndbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalibrateProjector.cpp
848 lines (747 loc) · 26.4 KB
/
CalibrateProjector.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
/***********************************************************************
CalibrateProjector - Utility to calculate the calibration transformation
of a projector into a Kinect-captured 3D space.
Copyright (c) 2012-2013 Oliver Kreylos
This file is part of the Augmented Reality Sandbox (SARndbox).
The Augmented Reality Sandbox is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Augmented Reality Sandbox is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Augmented Reality Sandbox; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "CalibrateProjector.h"
#include <stdlib.h>
#include <string.h>
#include <string>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <Misc/FunctionCalls.h>
#include <Misc/File.h>
#include <IO/ValueSource.h>
#include <IO/CSVSource.h>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <Math/Math.h>
#include <Math/Interval.h>
#include <Geometry/GeometryValueCoders.h>
#include <GL/GLContextData.h>
#include <GL/Extensions/GLARBTextureNonPowerOfTwo.h>
#include <Images/ExtractBlobs.h>
#include <Vrui/Vrui.h>
#include <Vrui/ToolManager.h>
#include <Vrui/OpenFile.h>
/********************************************************
Static elements of class CalibrateProjector::CaptureTool:
********************************************************/
CalibrateProjector::CaptureToolFactory* CalibrateProjector::CaptureTool::factory=0;
/************************************************
Methods of class CalibrateProjector::CaptureTool:
************************************************/
CalibrateProjector::CaptureTool::CaptureTool(const Vrui::ToolFactory* factory,const Vrui::ToolInputAssignment& inputAssignment)
:Vrui::Tool(factory,inputAssignment)
{
}
CalibrateProjector::CaptureTool::~CaptureTool(void)
{
}
const Vrui::ToolFactory* CalibrateProjector::CaptureTool::getFactory(void) const
{
return factory;
}
void CalibrateProjector::CaptureTool::buttonCallback(int buttonSlotIndex,Vrui::InputDevice::ButtonCallbackData* cbData)
{
/* Start capturing a depth frame if the button was just pressed: */
if(cbData->newButtonState)
{
if(buttonSlotIndex==0)
application->startTiePointCapture();
else
application->startBackgroundCapture();
}
}
/*********************************************
Methods of class CalibrateProjector::DataItem:
*********************************************/
CalibrateProjector::DataItem::DataItem(void)
:blobImageTextureId(0),blobImageVersion(0)
{
glGenTextures(1,&blobImageTextureId);
}
CalibrateProjector::DataItem::~DataItem(void)
{
glDeleteTextures(1,&blobImageTextureId);
}
namespace {
/**************
Helper classes:
**************/
class BlobForegroundSelector // Functor class to select foreground pixels
{
/* Methods: */
public:
bool operator()(unsigned int x,unsigned int y,const Kinect::FrameSource::DepthPixel& pixel) const
{
return pixel<Kinect::FrameSource::invalidDepth;
}
};
class BlobMergeChecker // Functor class to check whether two pixels can belong to the same blob
{
/* Elements: */
private:
int maxDepthDist;
/* Constructors and destructors: */
public:
BlobMergeChecker(int sMaxDepthDist)
:maxDepthDist(sMaxDepthDist)
{
}
/* Methods: */
bool operator()(unsigned int x1,unsigned int y1,const Kinect::FrameSource::DepthPixel& pixel1,unsigned int x2,unsigned int y2,const Kinect::FrameSource::DepthPixel& pixel2) const
{
return Math::abs(int(pixel1)-int(pixel2))<=maxDepthDist;
}
};
}
/***********************************
Methods of class CalibrateProjector:
***********************************/
void CalibrateProjector::depthStreamingCallback(const Kinect::FrameBuffer& frameBuffer)
{
/* Do nothing if currently capturing background frames: */
if(capturingBackground)
return;
/* Put the new raw frame into the triple buffer: */
rawFrames.postNewValue(frameBuffer);
/* Wake up the foreground thread: */
Vrui::requestUpdate();
}
void CalibrateProjector::backgroundCaptureCompleteCallback(Kinect::Camera&)
{
/* Reset the background capture flag: */
std::cout<<" done"<<std::endl;
camera->setRemoveBackground(true);
capturingBackground=false;
/* Wake up the foreground thread: */
Vrui::requestUpdate();
}
CalibrateProjector::CalibrateProjector(int& argc,char**& argv)
:Vrui::Application(argc,argv),
numTiePointFrames(60),numBackgroundFrames(120),
blobMergeDepth(1),
camera(0),
pixelDepthCorrection(0),
capturingBackground(false),
blobIdImage(0),blobImage(0),blobImageVersion(0),
currentBlob(0),
capturingTiePoint(false),numCaptureFrames(0),
haveProjection(false),projection(4,4)
{
/* Register the custom tool class: */
CaptureToolFactory* toolFactory1=new CaptureToolFactory("CaptureTool","Capture",0,*Vrui::getToolManager());
toolFactory1->setNumButtons(2);
toolFactory1->setButtonFunction(0,"Capture Tie Point");
toolFactory1->setButtonFunction(1,"Capture Background");
Vrui::getToolManager()->addClass(toolFactory1,Vrui::ToolManager::defaultToolFactoryDestructor);
/* Process command line parameters: */
bool printHelp=false;
std::string sandboxLayoutFileName=CONFIGDIR;
sandboxLayoutFileName.push_back('/');
sandboxLayoutFileName.append("BoxLayout.txt");
int cameraIndex=0;
imageSize[0]=1024;
imageSize[1]=768;
numTiePoints[0]=4;
numTiePoints[1]=3;
const char* tiePointFileName=0;
for(int i=1;i<argc;++i)
{
if(argv[i][0]=='-')
{
if(strcasecmp(argv[i]+1,"h")==0)
printHelp=true;
else if(strcasecmp(argv[i]+1,"slf")==0)
{
++i;
sandboxLayoutFileName=argv[i];
}
else if(strcasecmp(argv[i]+1,"c")==0)
{
++i;
cameraIndex=atoi(argv[i]);
}
else if(strcasecmp(argv[i]+1,"s")==0)
{
for(int j=0;j<2;++j)
{
++i;
imageSize[j]=atoi(argv[i]);
}
}
else if(strcasecmp(argv[i]+1,"tp")==0)
{
for(int j=0;j<2;++j)
{
++i;
numTiePoints[j]=atoi(argv[i]);
}
}
else if(strcasecmp(argv[i]+1,"bmd")==0)
{
++i;
blobMergeDepth=atoi(argv[i]);
}
else if(strcasecmp(argv[i]+1,"tpf")==0)
{
++i;
tiePointFileName=argv[i];
}
}
}
if(printHelp)
{
std::cout<<"Usage: CalibrateProjector [option 1] ... [option n]"<<std::endl;
std::cout<<" Options:"<<std::endl;
std::cout<<" -h"<<std::endl;
std::cout<<" Prints this help message"<<std::endl;
std::cout<<" -slf <sandbox layout file name>"<<std::endl;
std::cout<<" Loads the sandbox layout file of the given name"<<std::endl;
std::cout<<" Default: "<<CONFIGDIR<<"/BoxLayout.txt"<<std::endl;
std::cout<<" -c <camera index>"<<std::endl;
std::cout<<" Selects the local Kinect camera of the given index (0: first camera"<<std::endl;
std::cout<<" on USB bus)"<<std::endl;
std::cout<<" Default: 0"<<std::endl;
std::cout<<" -s <projector image width> <projector image height>"<<std::endl;
std::cout<<" Sets the width and height of the projector image in pixels. This"<<std::endl;
std::cout<<" must match the actual resolution of the projector."<<std::endl;
std::cout<<" Default: 1024 768"<<std::endl;
std::cout<<" -tp <grid width> <grid height>"<<std::endl;
std::cout<<" Sets the number of tie points to be collected before a calibration"<<std::endl;
std::cout<<" is computed."<<std::endl;
std::cout<<" Default: 4 3"<<std::endl;
std::cout<<" -bmd <mamximum blob merge depth distance>"<<std::endl;
std::cout<<" Maximum depth distance between adjacent pixels in the same blob."<<std::endl;
std::cout<<" Default: 1"<<std::endl;
std::cout<<" -tpf <tie point file name>"<<std::endl;
std::cout<<" Reads initial calibration tie points from a CSV file"<<std::endl;
}
/* Read the sandbox layout file: */
{
IO::ValueSource layoutSource(Vrui::openFile(sandboxLayoutFileName.c_str()));
layoutSource.skipWs();
std::string s=layoutSource.readLine();
basePlane=Misc::ValueCoder<OPlane>::decode(s.c_str(),s.c_str()+s.length());
basePlane.normalize();
for(int i=0;i<4;++i)
{
layoutSource.skipWs();
s=layoutSource.readLine();
basePlaneCorners[i]=Misc::ValueCoder<OPoint>::decode(s.c_str(),s.c_str()+s.length());
}
}
if(tiePointFileName!=0)
{
/* Read the tie point file: */
IO::CSVSource tiePointFile(IO::openFile(tiePointFileName));
while(!tiePointFile.eof())
{
/* Read the tie point: */
TiePoint tp;
for(int i=0;i<2;++i)
tp.p[i]=tiePointFile.readField<double>();
for(int i=0;i<3;++i)
tp.o[i]=tiePointFile.readField<double>();
tiePoints.push_back(tp);
}
if(tiePoints.size()>=numTiePoints[0]*numTiePoints[1])
{
/* Calculate an initial calibration: */
calcCalibration();
}
}
/* Enable background USB event handling: */
usbContext.startEventHandling();
/* Open the Kinect camera device: */
camera=new Kinect::Camera(usbContext,cameraIndex);
camera->setCompressDepthFrames(true);
camera->setSmoothDepthFrames(false);
camera->setBackgroundRemovalFuzz(1);
/* Get the camera's depth frame size: */
for(int i=0;i<2;++i)
frameSize[i]=camera->getActualFrameSize(Kinect::FrameSource::DEPTH)[i];
/* Get the camera's depth correction coefficients and create the per-pixel correction buffer: */
Kinect::FrameSource::DepthCorrection* dc=camera->getDepthCorrectionParameters();
pixelDepthCorrection=dc->getPixelCorrection(frameSize);
delete dc;
/* Get the camera's intrinsic parameters: */
cameraIps=camera->getIntrinsicParameters();
/* Create the blob ID image: */
blobIdImage=new unsigned int[frameSize[1]*frameSize[0]];
blobImage=new GLColor<GLubyte,3>[frameSize[1]*frameSize[0]];
GLColor<GLubyte,3>* biPtr=blobImage;
for(unsigned int y=0;y<frameSize[1];++y)
for(unsigned int x=0;x<frameSize[0];++x,++biPtr)
*biPtr=GLColor<GLubyte,3>(0,0,0);
blobImageVersion=1;
/* Start streaming depth frames: */
camera->startStreaming(0,Misc::createFunctionCall(this,&CalibrateProjector::depthStreamingCallback));
/* Start capturing the initial background frame: */
startBackgroundCapture();
}
CalibrateProjector::~CalibrateProjector(void)
{
/* Delete blob extraction state: */
delete currentBlob;
/* Stop streaming and close the Kinect camera device: */
camera->stopStreaming();
delete camera;
/* Delete allocated buffers: */
delete[] blobIdImage;
delete[] blobImage;
delete[] pixelDepthCorrection;
}
void CalibrateProjector::frame(void)
{
/* Check if there is a new raw depth frame: */
if(rawFrames.lockNewValue())
{
/* Extract all foreground blobs from the raw depth frame: */
const DepthPixel* framePixels=static_cast<const DepthPixel*>(rawFrames.getLockedValue().getBuffer());
BlobForegroundSelector bfs;
BlobMergeChecker bmc(blobMergeDepth);
DepthCentroidBlob::Creator blobCreator;
for(int i=0;i<2;++i)
blobCreator.frameSize[i]=frameSize[i];
blobCreator.pixelDepthCorrection=pixelDepthCorrection;
blobCreator.depthProjection=cameraIps.depthProjection;
std::vector<DepthCentroidBlob> blobs=Images::extractBlobs<DepthCentroidBlob>(frameSize,framePixels,bfs,bmc,blobCreator,blobIdImage);
/* Find the largest blob that is inside the sandbox area and roughly disk-shaped: */
std::vector<DepthCentroidBlob>::iterator biggestBlobIt=blobs.end();
size_t maxNumPixels=50;
for(std::vector<DepthCentroidBlob>::iterator bIt=blobs.begin();bIt!=blobs.end();++bIt)
if(maxNumPixels<bIt->numPixels)
{
/* Check if the blob is inside the configured sandbox area and roughly blob-shaped: */
OPoint blobCentroid=bIt->getCentroid(cameraIps.depthProjection);
bool inside=true;
inside=inside&&Geometry::cross(basePlane.getNormal(),basePlaneCorners[1]-basePlaneCorners[0])*(blobCentroid-basePlaneCorners[0])>=0.0;
inside=inside&&Geometry::cross(basePlane.getNormal(),basePlaneCorners[3]-basePlaneCorners[1])*(blobCentroid-basePlaneCorners[1])>=0.0;
inside=inside&&Geometry::cross(basePlane.getNormal(),basePlaneCorners[2]-basePlaneCorners[3])*(blobCentroid-basePlaneCorners[3])>=0.0;
inside=inside&&Geometry::cross(basePlane.getNormal(),basePlaneCorners[0]-basePlaneCorners[2])*(blobCentroid-basePlaneCorners[2])>=0.0;
double fillRatio=double(bIt->numPixels)/(double(bIt->bbMax[0]-bIt->bbMin[0])*double(bIt->bbMax[1]-bIt->bbMin[1]));
if(inside&&fillRatio>=0.7) // Approximate fill ratio of a circle inside a square
{
/* Use this blob for now: */
biggestBlobIt=bIt;
maxNumPixels=bIt->numPixels;
}
}
/* Update the current blob: */
delete currentBlob;
currentBlob=0;
if(biggestBlobIt!=blobs.end())
{
currentBlob=new DepthCentroidBlob(*biggestBlobIt);
currentCentroid=currentBlob->getCentroid(cameraIps.depthProjection);
}
/* Create the blob image: */
#if VISUALIZE_BLOBS
GLColor<GLubyte,3> blobColors[]=
{
GLColor<GLubyte,3>(255,0,0),
GLColor<GLubyte,3>(255,255,0),
GLColor<GLubyte,3>(0,255,255),
GLColor<GLubyte,3>(0,0,255),
GLColor<GLubyte,3>(255,0,255),
GLColor<GLubyte,3>(128,0,0),
GLColor<GLubyte,3>(128,128,0),
GLColor<GLubyte,3>(0,128,0),
GLColor<GLubyte,3>(0,128,128),
GLColor<GLubyte,3>(0,0,128),
GLColor<GLubyte,3>(128,0,128),
GLColor<GLubyte,3>(255,128,128),
GLColor<GLubyte,3>(255,255,128),
GLColor<GLubyte,3>(128,255,128),
GLColor<GLubyte,3>(128,255,255),
GLColor<GLubyte,3>(128,128,255),
GLColor<GLubyte,3>(255,128,255)
};
const unsigned int numBlobColors=sizeof(blobColors)/sizeof(blobColors[0]);
#endif
const unsigned int* biiPtr=blobIdImage;
GLColor<GLubyte,3>* biPtr=blobImage;
for(unsigned int y=0;y<frameSize[1];++y)
for(unsigned int x=0;x<frameSize[0];++x,++biiPtr,++biPtr)
{
#if VISUALIZE_BLOBS
/* Assign different colors to each blob: */
if(currentBlob!=0&&*biiPtr==currentBlob->blobId)
*biPtr=GLColor<GLubyte,3>(0,255,0);
else if(*biiPtr==~0x0U)
*biPtr=GLColor<GLubyte,3>(0,0,0);
else
*biPtr=blobColors[(*biiPtr)%numBlobColors];
#else
/* Make the current target blob green and all others yellow: */
if(currentBlob!=0&&*biiPtr==currentBlob->blobId)
*biPtr=GLColor<GLubyte,3>(0,255,0);
else if(*biiPtr!=~0x0U)
*biPtr=GLColor<GLubyte,3>(255,255,0);
else
*biPtr=GLColor<GLubyte,3>(0,0,0);
#endif
}
++blobImageVersion;
/* Check if we are currently capturing a tie point: */
if(capturingTiePoint&¤tBlob!=0)
{
/* Add the current target blob to the tie point combiner: */
tiePointCombiner.addPoint(currentCentroid);
--numCaptureFrames;
if(numCaptureFrames==0)
{
/* Store the just-captured tie point: */
TiePoint tp;
int pointIndex=int(tiePoints.size());
int xIndex=pointIndex%numTiePoints[0];
int yIndex=(pointIndex/numTiePoints[0])%numTiePoints[1];
int x=(xIndex+1)*imageSize[0]/(numTiePoints[0]+1);
int y=(yIndex+1)*imageSize[1]/(numTiePoints[1]+1);
tp.p=PPoint(Scalar(x)+Scalar(0.5),Scalar(y)+Scalar(0.5));
tp.o=tiePointCombiner.getPoint();
tiePoints.push_back(tp);
std::cout<<" done"<<std::endl;
std::cout<<"Tie point: "<<tp.p[0]<<", "<<tp.p[1]<<"; "<<tp.o[0]<<", "<<tp.o[1]<<", "<<tp.o[2]<<std::endl;
capturingTiePoint=false;
/* Check if the calibration is complete: */
if(tiePoints.size()>=numTiePoints[0]*numTiePoints[1])
{
/* Calculate the calibration transformation: */
calcCalibration();
}
}
}
}
}
void CalibrateProjector::display(GLContextData& contextData) const
{
/* Get the context data item: */
DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this);
glPushAttrib(GL_ENABLE_BIT|GL_LINE_BIT);
glDisable(GL_LIGHTING);
glLineWidth(1.0f);
/* Go to screen space: */
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0,double(imageSize[0]),0.0,double(imageSize[1]),-1.0,1.0);
if(capturingBackground)
{
/* Indicate that a background frame is being captured: */
glBegin(GL_QUADS);
glColor3f(1.0f,0.0f,0.0f);
glVertex2f(0.0f,0.0f);
glVertex2f(float(imageSize[0]),0.0f);
glVertex2f(float(imageSize[0]),float(imageSize[1]));
glVertex2f(0.0f,float(imageSize[1]));
glEnd();
}
else
{
/* Calculate the screen-space position of the next tie point: */
int pointIndex=int(tiePoints.size());
int xIndex=pointIndex%numTiePoints[0];
int yIndex=(pointIndex/numTiePoints[0])%numTiePoints[1];
int x=(xIndex+1)*imageSize[0]/(numTiePoints[0]+1);
int y=(yIndex+1)*imageSize[1]/(numTiePoints[1]+1);
/* Draw the next tie point: */
glBegin(GL_LINES);
glColor3f(1.0f,1.0f,1.0f);
glVertex2f(0.0f,float(y)+0.5f);
glVertex2f(float(imageSize[0]),float(y)+0.5f);
glVertex2f(float(x)+0.5f,0.0f);
glVertex2f(float(x)+0.5f,float(imageSize[1]));
glEnd();
/* Draw the current blob image: */
glBindTexture(GL_TEXTURE_2D,dataItem->blobImageTextureId);
if(dataItem->blobImageVersion!=blobImageVersion)
{
/* Upload the new blob image into the texture: */
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,frameSize[0],frameSize[1],GL_RGB,GL_UNSIGNED_BYTE,blobImage);
dataItem->blobImageVersion=blobImageVersion;
}
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE),
glBegin(GL_QUADS);
glTexCoord2f(dataItem->texMin[0],dataItem->texMin[1]);
glVertex3f(0.0f,0.0f,-0.01);
glTexCoord2f(dataItem->texMax[0],dataItem->texMin[1]);
glVertex3f(float(imageSize[0]),0.0f,-0.01);
glTexCoord2f(dataItem->texMax[0],dataItem->texMax[1]);
glVertex3f(float(imageSize[0]),float(imageSize[1]),-0.01);
glTexCoord2f(dataItem->texMin[0],dataItem->texMax[1]);
glVertex3f(0.0f,float(imageSize[1]),-0.01);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,0);
if(currentBlob!=0)
{
#if 0
/* Draw the currently selected target blob: */
glScaled(double(imageSize[0])/double(frameSize[0]),double(imageSize[1])/double(frameSize[1]),1.0);
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex2i(currentBlob->bbMin[0],currentBlob->bbMin[1]);
glVertex2i(currentBlob->bbMax[0],currentBlob->bbMin[1]);
glVertex2i(currentBlob->bbMax[0],currentBlob->bbMax[1]);
glVertex2i(currentBlob->bbMin[0],currentBlob->bbMax[1]);
glEnd();
#endif
if(haveProjection)
{
/* Draw the currently selected target blob using the current calibration: */
glLoadIdentity();
Math::Matrix blob(4,1);
for(int i=0;i<3;++i)
blob(i)=currentCentroid[i];
blob(3)=1.0;
Math::Matrix projBlob=projection*blob;
double x=projBlob(0)/projBlob(3);
double y=projBlob(1)/projBlob(3);
glBegin(GL_LINES);
glColor3f(1.0f,0.0f,0.0f);
glVertex2d(x,-1.0);
glVertex2d(x,1.0);
glVertex2d(-1.0,y);
glVertex2d(1.0,y);
glEnd();
}
}
}
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
}
void CalibrateProjector::initContext(GLContextData& contextData) const
{
/* Create the data item: */
DataItem* dataItem=new DataItem;
contextData.addDataItem(this,dataItem);
/* Check whether non-power-of-two-dimension textures are supported: */
bool haveNpotdt=GLARBTextureNonPowerOfTwo::isSupported();
if(haveNpotdt)
GLARBTextureNonPowerOfTwo::initExtension();
/* Calculate the texture coordinate rectangle: */
unsigned int texSize[2];
if(haveNpotdt)
{
for(int i=0;i<2;++i)
texSize[i]=frameSize[i];
}
else
{
for(int i=0;i<2;++i)
for(texSize[i]=1U;texSize[i]<frameSize[i];texSize[i]<<=1)
;
}
for(int i=0;i<2;++i)
{
dataItem->texMin[i]=0.0f;
dataItem->texMax[i]=GLfloat(frameSize[i])/GLfloat(texSize[i]);
}
/* Initialize the texture object: */
glBindTexture(GL_TEXTURE_2D,dataItem->blobImageTextureId);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_BASE_LEVEL,0);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAX_LEVEL,0);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,texSize[0],texSize[1],0,GL_RGB,GL_UNSIGNED_BYTE,0);
/* Protect the texture object: */
glBindTexture(GL_TEXTURE_2D,0);
}
void CalibrateProjector::startBackgroundCapture(void)
{
/* Bail out if already capturing a tie point or background: */
if(capturingBackground||capturingTiePoint)
return;
/* Tell the Kinect camera to capture a new background frame: */
capturingBackground=true;
std::cout<<"CalibrateProjector: Capturing "<<numBackgroundFrames<<" background frames..."<<std::flush;
camera->captureBackground(numBackgroundFrames,true,Misc::createFunctionCall(this,&CalibrateProjector::backgroundCaptureCompleteCallback));
}
void CalibrateProjector::startTiePointCapture(void)
{
/* Bail out if already capturing a tie point or background: */
if(capturingBackground||capturingTiePoint)
return;
/* Start capturing a new tie point: */
capturingTiePoint=true;
numCaptureFrames=numTiePointFrames;
std::cout<<"CalibrateProjector: Capturing "<<numTiePointFrames<<" tie point frames..."<<std::flush;
tiePointCombiner.reset();
}
void CalibrateProjector::calcCalibration(void)
{
/* Create the least-squares system: */
Math::Matrix a(12,12,0.0);
/* Process all tie points: */
for(std::vector<TiePoint>::iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
std::cout<<"Tie point: "<<tpIt->p[0]<<", "<<tpIt->p[1]<<", "<<tpIt->o[0]<<", "<<tpIt->o[1]<<", "<<tpIt->o[2]<<std::endl;
/* Create the tie point's associated two linear equations: */
double eq[2][12];
eq[0][0]=tpIt->o[0];
eq[0][1]=tpIt->o[1];
eq[0][2]=tpIt->o[2];
eq[0][3]=1.0;
eq[0][4]=0.0;
eq[0][5]=0.0;
eq[0][6]=0.0;
eq[0][7]=0.0;
eq[0][8]=-tpIt->p[0]*tpIt->o[0];
eq[0][9]=-tpIt->p[0]*tpIt->o[1];
eq[0][10]=-tpIt->p[0]*tpIt->o[2];
eq[0][11]=-tpIt->p[0];
eq[1][0]=0.0;
eq[1][1]=0.0;
eq[1][2]=0.0;
eq[1][3]=0.0;
eq[1][4]=tpIt->o[0];
eq[1][5]=tpIt->o[1];
eq[1][6]=tpIt->o[2];
eq[1][7]=1.0;
eq[1][8]=-tpIt->p[1]*tpIt->o[0];
eq[1][9]=-tpIt->p[1]*tpIt->o[1];
eq[1][10]=-tpIt->p[1]*tpIt->o[2];
eq[1][11]=-tpIt->p[1];
/* Insert the two equations into the least-squares system: */
for(int row=0;row<2;++row)
{
for(unsigned int i=0;i<12;++i)
for(unsigned int j=0;j<12;++j)
a(i,j)+=eq[row][i]*eq[row][j];
}
}
/* Find the least square system's smallest eigenvalue: */
std::pair<Math::Matrix,Math::Matrix> qe=a.jacobiIteration();
unsigned int minEIndex=0;
double minE=Math::abs(qe.second(0,0));
for(unsigned int i=1;i<12;++i)
{
if(minE>Math::abs(qe.second(i,0)))
{
minEIndex=i;
minE=Math::abs(qe.second(i,0));
}
}
/* Create the initial unscaled homography: */
Math::Matrix hom(3,4);
for(int i=0;i<3;++i)
for(int j=0;j<4;++j)
hom(i,j)=qe.first(i*4+j,minEIndex);
/* Scale the homography such that projected weights are positive distance from projector: */
double wLen=Math::sqrt(Math::sqr(hom(2,0))+Math::sqr(hom(2,1))+Math::sqr(hom(2,2)));
int numNegativeWeights=0;
for(std::vector<TiePoint>::iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
/* Calculate the object-space tie point's projected weight: */
double w=hom(2,3);
for(int j=0;j<3;++j)
w+=hom(2,j)*tpIt->o[j];
if(w<0.0)
++numNegativeWeights;
}
if(numNegativeWeights==0||numNegativeWeights==int(tiePoints.size()))
{
/* Scale the homography: */
if(numNegativeWeights>0)
wLen=-wLen;
for(int i=0;i<3;++i)
for(int j=0;j<4;++j)
hom(i,j)/=wLen;
/* Print the scaled homography: */
for(int i=0;i<3;++i)
{
std::cout<<std::setw(10)<<hom(i,0);
for(int j=1;j<4;++j)
std::cout<<" "<<std::setw(10)<<hom(i,j);
std::cout<<std::endl;
}
/* Calculate the calibration residual: */
double res=0.0;
for(std::vector<TiePoint>::iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
Math::Matrix op(4,1);
for(int i=0;i<3;++i)
op(i)=tpIt->o[i];
op(3)=1.0;
Math::Matrix pp=hom*op;
for(int i=0;i<2;++i)
pp(i)/=pp(2);
res+=Math::sqr(pp(0)-tpIt->p[0])+Math::sqr(pp(1)-tpIt->p[1]);
}
res=Math::sqrt(res/double(tiePoints.size()));
std::cout<<"RMS calibration residual: "<<res<<std::endl;
/* Calculate the full projector projection matrix: */
for(unsigned int i=0;i<2;++i)
for(unsigned int j=0;j<4;++j)
projection(i,j)=hom(i,j);
for(unsigned int j=0;j<3;++j)
projection(2,j)=0.0;
projection(2,3)=-1.0;
for(unsigned int j=0;j<4;++j)
projection(3,j)=hom(2,j);
/* Calculate the z range of all tie points: */
Math::Interval<double> zRange=Math::Interval<double>::empty;
int numNegativeWeights=0;
for(std::vector<TiePoint>::iterator tpIt=tiePoints.begin();tpIt!=tiePoints.end();++tpIt)
{
/* Transform the object-space tie point with the projection matrix: */
Math::Matrix op(4,1);
for(int i=0;i<3;++i)
op(i)=double(tpIt->o[i]);
op(3)=1.0;
Math::Matrix pp=projection*op;
if(pp(3)<0.0)
++numNegativeWeights;
zRange.addValue(pp(2)/pp(3));
}
std::cout<<"Z range of collected tie points: ["<<zRange.getMin()<<", "<<zRange.getMax()<<"]"<<std::endl;
/* Double the size of the range to include a safety margin on either side: */
zRange=Math::Interval<double>(zRange.getMin()*2.0,zRange.getMax()*0.5);
/* Pre-multiply the projection matrix with the inverse viewport matrix to go to clip coordinates: */
Math::Matrix invViewport(4,4,1.0);
invViewport(0,0)=2.0/double(imageSize[0]);
invViewport(0,3)=-1.0;
invViewport(1,1)=2.0/double(imageSize[1]);
invViewport(1,3)=-1.0;
invViewport(2,2)=2.0/(zRange.getSize());
invViewport(2,3)=-2.0*zRange.getMin()/(zRange.getSize())-1.0;
projection=invViewport*projection;
/* Write the projection matrix to a file: */
std::string projFileName=CONFIGDIR;
projFileName.push_back('/');
projFileName.append("ProjectorMatrix.dat");
IO::FilePtr projFile=Vrui::openFile(projFileName.c_str(),IO::File::WriteOnly);
projFile->setEndianness(Misc::LittleEndian);
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
projFile->write<double>(projection(i,j));
haveProjection=true;
}
else
std::cout<<"Calibration error: Some tie points have negative projection weights. Please start from scratch"<<std::endl;
}
/* Create and execute an application object: */
VRUI_APPLICATION_RUN(CalibrateProjector)