-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSpaceCarver.cpp
More file actions
246 lines (222 loc) · 7.44 KB
/
SpaceCarver.cpp
File metadata and controls
246 lines (222 loc) · 7.44 KB
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
/***********************************************************************
SpaceCarver - Utility to convert a set of colocated Kinect facades into
a watertight mesh using a space carving approach.
Copyright (c) 2011-2018 Oliver Kreylos
This file is part of the Kinect 3D Video Capture Project (Kinect).
The Kinect 3D Video Capture Project 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 Kinect 3D Video Capture Project 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 Kinect 3D Video Capture Project; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include <stdlib.h>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <Misc/Array.h>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <Geometry/ComponentArray.h>
#include <Geometry/Point.h>
#include <Geometry/Box.h>
#include <Geometry/OrthogonalTransformation.h>
#include <Geometry/ProjectiveTransformation.h>
#include <Geometry/GeometryMarshallers.h>
#include <Kinect/FrameBuffer.h>
#include <Kinect/DepthFrameReader.h>
typedef Geometry::Point<double,3> Point;
typedef Geometry::Box<double,3> Box;
typedef Geometry::ComponentArray<double,3> Size;
typedef Geometry::OrthogonalTransformation<double,3> OGTransform;
typedef Geometry::ProjectiveTransformation<double,3> Projection;
typedef unsigned char Voxel;
typedef Misc::Array<Voxel,3> Grid;
typedef Grid::Index Index;
FrameBuffer open(const FrameBuffer& frame)
{
FrameBuffer result(frame.getSize(0),frame.getSize(1),frame.getSize(0)*frame.getSize(1)*sizeof(unsigned short));
int stride=frame.getSize(0);
int noffs[9];
int* noffPtr=noffs;
for(int dy=-1;dy<=1;++dy)
for(int dx=-1;dx<=1;++dx,++noffPtr)
*noffPtr=dy*stride+dx;
const unsigned short* fPtr=frame.getTypedBuffer<unsigned short>();
unsigned short* rPtr=result.getTypedBuffer<unsigned short>();
for(int x=0;x<frame.getSize(0);++x,++fPtr,++rPtr)
*rPtr=*fPtr;
for(int y=1;y<frame.getSize(1)-1;++y)
{
*rPtr=*fPtr;
++fPtr;
++rPtr;
for(int x=1;x<frame.getSize(0)-1;++x,++fPtr,++rPtr)
{
if(*fPtr>=0x07feU)
{
unsigned int nd=0;
unsigned int nn=0;
for(int i=0;i<9;++i)
if(fPtr[noffs[i]]<0x07feU)
{
nd+=fPtr[noffs[i]];
++nn;
}
if(nn>0)
*rPtr=(nd+nn/2)/nn;
else
*rPtr=0x07ffU;
}
else
*rPtr=*fPtr;
}
*rPtr=*fPtr;
++fPtr;
++rPtr;
}
for(int x=0;x<frame.getSize(0);++x,++fPtr,++rPtr)
*rPtr=*fPtr;
return result;
}
FrameBuffer close(const FrameBuffer& frame)
{
FrameBuffer result(frame.getSize(0),frame.getSize(1),frame.getSize(0)*frame.getSize(1)*sizeof(unsigned short));
int stride=frame.getSize(0);
int noffs[9];
int* noffPtr=noffs;
for(int dy=-1;dy<=1;++dy)
for(int dx=-1;dx<=1;++dx,++noffPtr)
*noffPtr=dy*stride+dx;
const unsigned short* fPtr=frame.getTypedBuffer<unsigned short>();
unsigned short* rPtr=result.getTypedBuffer<unsigned short>();
for(int x=0;x<frame.getSize(0);++x,++fPtr,++rPtr)
*rPtr=*fPtr;
for(int y=1;y<frame.getSize(1)-1;++y)
{
*rPtr=*fPtr;
++fPtr;
++rPtr;
for(int x=1;x<frame.getSize(0)-1;++x,++fPtr,++rPtr)
{
if(*fPtr>=0x07feU)
{
for(int i=0;i<9;++i)
rPtr[noffs[i]]=0x07ffU;
}
else
*rPtr=*fPtr;
}
*rPtr=*fPtr;
++fPtr;
++rPtr;
}
for(int x=0;x<frame.getSize(0);++x,++fPtr,++rPtr)
*rPtr=*fPtr;
return result;
}
int main(int argc,char* argv[])
{
/* Set up the volumetric grid: */
Box gridBox=Box(Point(-32.0,-64.0,16.0),Point(32.0,0.0,80.0));
Index gridSize(256,256,256);
Grid grid(gridSize);
/* Initialize the grid: */
for(Grid::iterator gIt=grid.begin();gIt!=grid.end();++gIt)
*gIt=Voxel(255);
/* Carve away the n-th facade from each depth stream file listed on the command line: */
unsigned int facadeIndex=atoi(argv[1]);
for(int depthFileIndex=2;depthFileIndex<argc;++depthFileIndex)
{
try
{
/* Open the depth file: */
IO::AutoFile depthFile(IO::openFile(argv[depthFileIndex]));
depthFile->setEndianness(IO::File::LittleEndian);
/* Read the facade projection matrix and the projector transformation: */
Projection depthTransform;
depthFile->read<double>(depthTransform.getMatrix().getEntries(),4*4);
OGTransform projectorTransform=Misc::Marshaller<OGTransform>::read(*depthFile);
/* Calculate the joint projective transformation from 3D world space into depth image space: */
Projection proj=Geometry::invert(Projection(projectorTransform)*depthTransform);
/* Create a depth frame reader: */
DepthFrameReader depthFrameReader(*depthFile);
/* Read the n-th facade: */
FrameBuffer frame;
for(unsigned int i=0;i<facadeIndex;++i)
frame=depthFrameReader.readNextFrame();
/* Run a sequence of morphological open and close operators on the frame to fill holes: */
#if 1
for(int i=0;i<8;++i)
frame=open(frame);
#endif
#if 1
for(int i=0;i<8;++i)
frame=close(frame);
#endif
/* Carve the facade out of the grid: */
std::cout<<"Processing depth file "<<argv[depthFileIndex]<<std::flush;
double fmax[2];
for(int i=0;i<2;++i)
fmax[i]=double(frame.getSize(i));
unsigned short* frameBuffer=frame.getTypedBuffer<unsigned short>();
Size cellSize;
for(int i=0;i<3;++i)
cellSize[i]=(gridBox.max[i]-gridBox.min[i])/double(gridSize[i]);
Index index;
Point gridp;
gridp[0]=gridBox.min[0]+0.5*cellSize[0];
for(index[0]=0;index[0]<gridSize[0];++index[0],gridp[0]+=cellSize[0])
{
gridp[1]=gridBox.min[1]+0.5*cellSize[1];
for(index[1]=0;index[1]<gridSize[1];++index[1],gridp[1]+=cellSize[1])
{
gridp[2]=gridBox.min[2]+0.5*cellSize[2];
for(index[2]=0;index[2]<gridSize[2];++index[2],gridp[2]+=cellSize[2])
{
/* Project the grid point into the depth frame: */
Point fp=proj.transform(gridp);
/* Check if the projected grid point is inside the depth frame: */
if(fp[0]>=0.0&&fp[0]<fmax[0]&&fp[1]>=0.0&&fp[1]<fmax[1])
{
/* Check if the grid point is outside the facade: */
int x=int(fp[0]);
int y=int(fp[1]);
unsigned short depth=frameBuffer[y*frame.getSize(0)+x];
if(fp[2]<double(depth))
grid(index)=Voxel(0);
}
else
grid(index)=Voxel(0);
}
}
std::cout<<'.'<<std::flush;
}
std::cout<<"done"<<std::endl;
}
catch(const std::runtime_error& err)
{
std::cerr<<"Ignoring depth file "<<argv[depthFileIndex]<<" due to exception "<<err.what()<<std::endl;
}
catch(...)
{
std::cerr<<"Ignoring depth file "<<argv[depthFileIndex]<<" due to spurious exception"<<std::endl;
}
}
/* Save the result grid to a volume file: */
IO::AutoFile volFile(IO::openFile("SpaceCarverOut.vol",IO::File::WriteOnly));
volFile->setEndianness(IO::File::BigEndian);
for(int i=0;i<3;++i)
volFile->write<int>(int(gridSize[i]));
volFile->write<int>(0);
for(int i=0;i<3;++i)
volFile->write<float>((gridBox.max[i]-gridBox.min[i])*double(gridSize[i]-1)/double(gridSize[i]));
volFile->write<Voxel>(grid.getArray(),grid.getNumElements());
return 0;
}