forked from phreax/structured_light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree_step_phase_shift.cpp
234 lines (188 loc) · 6.81 KB
/
three_step_phase_shift.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
#include "three_step_phase_shift.h"
#include <cstdio>
ThreeStepPhaseShift::ThreeStepPhaseShift(
IplImage *imgPhase1
, IplImage *imgPhase2
, IplImage *imgPhase3):
imgPhase1(imgPhase1),
imgPhase2(imgPhase2),
imgPhase3(imgPhase3),
mask(0),
process(0)
{
width = imgPhase1->width;
height = imgPhase1->height;
if( width != imgPhase2->width ||
width != imgPhase3->width ||
height != imgPhase2->height ||
height != imgPhase3->height ) {
throw "invalid arguments: input images must have same dimension!";
}
int size = width*height;
imgColor = cvCreateImage(cvGetSize(imgPhase1),IPL_DEPTH_8U,3);
imgPhase1Gray = cvCreateImage(cvGetSize(imgPhase1),IPL_DEPTH_8U,1);
imgWrappedPhase = cvCreateImage(cvGetSize(imgPhase1),IPL_DEPTH_32F,1);
imgUnwrappedPhase = cvCreateImage(cvGetSize(imgPhase1),IPL_DEPTH_32F,1);
mask = new bool [size];
process = new bool [size];
quality = new float [size];
range = new float [size];
depth = new float [size];
// initilize matrices
noiseThreshold = 0.1;
zscale = 130;
zskew = 24;
// init step width for color and single channel images
step = width;
cout << "width: " << width << "\nheight: " << height << endl;
cout << "size " << size << endl;
}
// dtor
ThreeStepPhaseShift::~ThreeStepPhaseShift() {
cvReleaseImage(&imgPhase1);
cvReleaseImage(&imgPhase2);
cvReleaseImage(&imgPhase3);
cvReleaseImage(&imgColor);
cvReleaseImage(&imgWrappedPhase);
delete[] mask;
delete[] process;
delete[] quality;
delete[] depth;
}
void ThreeStepPhaseShift::phaseDecode()
{
// Some initializing and optimization( only the sqrt thing )
float sqrt3 = sqrt(3);
//int step = imgPhase1->widthStep;
uchar* ptrPhase1 = (uchar *)imgPhase1->imageData;
uchar* ptrPhase2 = (uchar *)imgPhase2->imageData;
uchar* ptrPhase3 = (uchar *)imgPhase3->imageData;
uchar* ptrColor = (uchar *)imgColor->imageData;
uchar* ptrGray = (uchar *)imgPhase1Gray->imageData;
float* ptrWrappedPhase = (float *)imgWrappedPhase->imageData;
int ii,iic;
float phi1;
float phi2;
float phi3;
float phiSum;
float phiRange;
float phiMax;
float phiMin;
float noise;
float twoPi = M_PI * 2;
int stepc = imgPhase1->widthStep;
cout << "step " << step<< endl;
for (int i = 0; i<height; i++) {
for (int j = 0; j<width; j++) {
ii = i*step+j;
iic = i*stepc+j*3;
// get intensity of each (rgb) phi image
phi1 =luminance(ptrPhase1+iic);
phi2 =luminance(ptrPhase2+iic);
phi3 =luminance(ptrPhase3+iic);
ptrGray[i*imgPhase1Gray->widthStep + j] = phi1*255;
phiSum = phi1 + phi2 + phi3;
phiMax = max_phase(phi1,phi2,phi3);
phiMin = min_phase(phi1,phi2,phi3);
phiRange = phiMax - phiMin;
// compute phase quality, try to filter background pixel
// i.e. where the phase range is too low
noise = phiRange / phiSum;
mask[ii] = (noise < noiseThreshold);
process[ii] = !mask[ii];
range[ii] = phiRange;
//compute phase: phi <- [-1,1]
if(!mask[ii])
ptrWrappedPhase[ii] = (float)atan2(sqrt3 * (phi1 - phi3), 2*phi2 - phi1 - phi3) / twoPi;
else
ptrWrappedPhase[ii] = 0.f;
// user lightest pixel of all phase images as color
if(phiMax==phi1) copy_channels(ptrColor+iic,ptrPhase1+iic);
else if(phiMax==phi2) copy_channels(ptrColor+iic,ptrPhase2+iic);
else if(phiMax==phi3) copy_channels(ptrColor+iic,ptrPhase3+iic);
}
}
computeQuality();
}
void ThreeStepPhaseShift::computeDepth () {
float* ptrUnwrappedPhase = (float *)imgUnwrappedPhase->imageData;
for(int i = 0; i<height; i++) {
float planephase = 0.5 - (i - (height/2))/zskew;
for(int j=0; j<width; j++) {
int ii = i*step+j;
if(!mask[ii]) {
depth[ii] = (ptrUnwrappedPhase[ii] - planephase) * zscale;
}
else
depth[ii] = 0.f;
}
}
cout << "Computed zmatrix" << endl;
}
void ThreeStepPhaseShift::phaseUnwrap(int x, int y, float phi, float q) {
float* ptrUnwrappedPhase = (float *)imgUnwrappedPhase->imageData;
if(process[y*step+x]) {
float frac = phi -(int)phi; // discontinue unwrapped phase
float diff = ptrUnwrappedPhase[y*step+x] - frac; // get phase difference
q += quality[y*step+x]; // add current quality
if ( diff > 0.5 ) {
diff--;
}
if ( diff < -0.5) {
diff++;
}
//procQueue.push_back(UnwrapPath(x, y, phi+diff));
processHeap.push(UnwrapPath(x, y, phi+diff, q));
}
}
void ThreeStepPhaseShift::phaseUnwrap()
{
int startX = width/2;
int startY = height/2;
uchar* ptrPhase1 = (uchar *)imgPhase1->imageData;
uchar* ptrPhase2 = (uchar *)imgPhase2->imageData;
uchar* ptrPhase3 = (uchar *)imgPhase3->imageData;
cvCopy(imgWrappedPhase, imgUnwrappedPhase);
float* ptrUnwrappedPhase = (float *)imgUnwrappedPhase->imageData;
UnwrapPath path = UnwrapPath(startX, startY, ptrUnwrappedPhase[startY*step+startX],0);
//procQueue.push_back(p);
processHeap.push(path);
while(!processHeap.empty()) {
UnwrapPath current = processHeap.top();
processHeap.pop();
int x = current.x;
int y = current.y;
float q = current.q;
if(process[y*step+x]) {
ptrUnwrappedPhase[y*step+x] = current.phi;
process[y*width+x] = false;
// follow path in each direction
if (y > 0) {
phaseUnwrap(x, y-1, current.phi, q);
}
if (y < height-1) {
phaseUnwrap(x, y+1, current.phi, q);
}
if (x > 0) {
phaseUnwrap(x-1, y, current.phi, q);
}
if (x < width - 1) {
phaseUnwrap(x+1, y, current.phi, q);
}
}
}
}
void ThreeStepPhaseShift::computeQuality() {
uchar *ptrPhase = (uchar *)imgWrappedPhase->imageData;
for(int i=1;i<height-1;i++) {
for(int j=1;j<width-1;j++) {
int ii = i*step+j;
float phi = ptrPhase[ii];
quality[ii] = sqdist(phi,ptrPhase[ii+1])+
sqdist(phi,ptrPhase[ii-1])+
sqdist(phi,ptrPhase[ii+step])+
sqdist(phi,ptrPhase[ii-step]);
quality[ii] /= range[ii];
}
}
}