Line 5... |
Line 5... |
5 |
|
5 |
|
6 |
#ifndef M_PI
|
6 |
#ifndef M_PI
|
7 |
#define M_PI 3.14159265358979323846
|
7 |
#define M_PI 3.14159265358979323846
|
8 |
#endif
|
8 |
#endif
|
9 |
|
9 |
|
10 |
static unsigned int nSteps = 20; // number of shifts/steps in primary
|
10 |
static unsigned int nStepsPrimary = 24; // number of shifts/steps in primary
|
- |
|
11 |
static unsigned int nStepsSecondary = 12; // number of shifts/steps in secondary
|
11 |
static float pPrimary = 64; // primary period
|
12 |
static float periodPrimary = 24; // primary period
|
12 |
|
13 |
|
13 |
// Algorithm
|
14 |
// Algorithm
|
14 |
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
|
15 |
static cv::Mat computePhaseVector(unsigned int length, float phase, float pitch){
|
15 |
|
16 |
|
16 |
cv::Mat phaseVector(length, 1, CV_8UC3);
|
17 |
cv::Mat phaseVector(length, 1, CV_8UC3);
|
Line 29... |
Line 30... |
29 |
}
|
30 |
}
|
30 |
|
31 |
|
31 |
AlgorithmPhaseShift::AlgorithmPhaseShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
|
32 |
AlgorithmPhaseShift::AlgorithmPhaseShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
|
32 |
|
33 |
|
33 |
// Set N
|
34 |
// Set N
|
34 |
N = 2+2*nSteps;
|
35 |
N = 2+nStepsPrimary+nStepsSecondary;
|
35 |
|
36 |
|
36 |
// Determine the secondary (wider) period
|
37 |
// Determine the secondary (wider) period
|
37 |
float pSecondary = (screenCols*pPrimary)/(screenCols-pPrimary);
|
38 |
float pSecondary = (screenCols*periodPrimary)/(screenCols-periodPrimary);
|
38 |
|
39 |
|
39 |
// all on pattern
|
40 |
// all on pattern
|
40 |
cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
|
41 |
cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
|
41 |
patterns.push_back(allOn);
|
42 |
patterns.push_back(allOn);
|
42 |
|
43 |
|
Line 46... |
Line 47... |
46 |
|
47 |
|
47 |
// Precompute encoded patterns
|
48 |
// Precompute encoded patterns
|
48 |
const float pi = M_PI;
|
49 |
const float pi = M_PI;
|
49 |
|
50 |
|
50 |
// Primary encoding patterns
|
51 |
// Primary encoding patterns
|
51 |
for(unsigned int i=0; i<nSteps; i++){
|
52 |
for(unsigned int i=0; i<nStepsPrimary; i++){
|
52 |
float phase = 2.0*pi/nSteps * i;
|
53 |
float phase = 2.0*pi/nStepsPrimary * i;
|
53 |
float pitch = pPrimary;
|
54 |
float pitch = periodPrimary;
|
54 |
cv::Mat patternI(1,1,CV_8U);
|
55 |
cv::Mat patternI(1,1,CV_8U);
|
55 |
patternI = computePhaseVector(screenCols, phase, pitch);
|
56 |
patternI = computePhaseVector(screenCols, phase, pitch);
|
56 |
patterns.push_back(patternI.t());
|
57 |
patterns.push_back(patternI.t());
|
57 |
}
|
58 |
}
|
58 |
|
59 |
|
59 |
// Secondary encoding patterns
|
60 |
// Secondary encoding patterns
|
60 |
for(unsigned int i=0; i<nSteps; i++){
|
61 |
for(unsigned int i=0; i<nStepsSecondary; i++){
|
61 |
float phase = 2.0*pi/nSteps * i;
|
62 |
float phase = 2.0*pi/nStepsSecondary * i;
|
62 |
float pitch = pSecondary;
|
63 |
float pitch = pSecondary;
|
63 |
cv::Mat patternI(1,1,CV_8U);
|
64 |
cv::Mat patternI(1,1,CV_8U);
|
64 |
patternI = computePhaseVector(screenCols, phase, pitch);
|
65 |
patternI = computePhaseVector(screenCols, phase, pitch);
|
65 |
patterns.push_back(patternI.t());
|
66 |
patterns.push_back(patternI.t());
|
66 |
}
|
67 |
}
|
Line 152... |
Line 153... |
152 |
cv::cvtColor(frames0[i], frames0Gray[i], CV_BayerBG2GRAY);
|
153 |
cv::cvtColor(frames0[i], frames0Gray[i], CV_BayerBG2GRAY);
|
153 |
cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
|
154 |
cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
|
154 |
}
|
155 |
}
|
155 |
|
156 |
|
156 |
// Decode camera0
|
157 |
// Decode camera0
|
157 |
std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nSteps);
|
158 |
std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
|
158 |
std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nSteps, frames0Gray.begin()+2+2*nSteps);
|
159 |
std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end());
|
159 |
std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
|
160 |
std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
|
160 |
cv::Mat up0Primary;
|
161 |
cv::Mat up0Primary;
|
161 |
cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
|
162 |
cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
|
162 |
//cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
|
163 |
//cv::Mat up0Secondary = getPhase(frames0Secondary[0], frames0Secondary[1], frames0Secondary[2]);
|
163 |
std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
|
164 |
std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
|
164 |
cv::Mat up0Secondary;
|
165 |
cv::Mat up0Secondary;
|
165 |
cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
|
166 |
cv::phase(F0Secondary[2], -F0Secondary[3], up0Secondary);
|
166 |
cv::Mat up0Equivalent = up0Primary - up0Secondary;
|
167 |
cv::Mat up0Equivalent = up0Primary - up0Secondary;
|
167 |
up0Equivalent = cvtools::modulo(up0Equivalent, 2*pi);
|
168 |
up0Equivalent = cvtools::modulo(up0Equivalent, 2*pi);
|
168 |
cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/pPrimary);
|
169 |
cv::Mat up0 = unwrapWithCue(up0Primary, up0Equivalent, (float)screenCols/periodPrimary);
|
169 |
up0 *= screenCols/(2*pi);
|
170 |
up0 *= screenCols/(2*pi);
|
170 |
|
171 |
|
171 |
// Decode camera1
|
172 |
// Decode camera1
|
172 |
std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nSteps);
|
173 |
std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
|
173 |
std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nSteps, frames1Gray.begin()+2+2*nSteps);
|
174 |
std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end());
|
174 |
std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
|
175 |
std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
|
175 |
cv::Mat up1Primary;
|
176 |
cv::Mat up1Primary;
|
176 |
cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
|
177 |
cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
|
177 |
//cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
|
178 |
//cv::Mat up1Secondary = getPhase(frames1Secondary[0], frames1Secondary[1], frames1Secondary[2]);
|
178 |
std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
|
179 |
std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
|
179 |
cv::Mat up1Secondary;
|
180 |
cv::Mat up1Secondary;
|
180 |
cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
|
181 |
cv::phase(F1Secondary[2], -F1Secondary[3], up1Secondary);
|
181 |
cv::Mat up1Equivalent = up1Primary - up1Secondary;
|
182 |
cv::Mat up1Equivalent = up1Primary - up1Secondary;
|
182 |
up1Equivalent = cvtools::modulo(up1Equivalent, 2*pi);
|
183 |
up1Equivalent = cvtools::modulo(up1Equivalent, 2*pi);
|
183 |
cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/pPrimary);
|
184 |
cv::Mat up1 = unwrapWithCue(up1Primary, up1Equivalent, (float)screenCols/periodPrimary);
|
184 |
up1 *= screenCols/(2*pi);
|
185 |
up1 *= screenCols/(2*pi);
|
185 |
|
186 |
|
186 |
|
187 |
|
187 |
// Rectifying homographies (rotation+projections)
|
188 |
// Rectifying homographies (rotation+projections)
|
188 |
cv::Size frameSize(frameCols, frameRows);
|
189 |
cv::Size frameSize(frameCols, frameRows);
|
Line 198... |
Line 199... |
198 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
199 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
199 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
200 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
200 |
|
201 |
|
201 |
// Phase remaps
|
202 |
// Phase remaps
|
202 |
cv::Mat up0Rect, up1Rect;
|
203 |
cv::Mat up0Rect, up1Rect;
|
203 |
cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_CUBIC);
|
204 |
cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
|
204 |
cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_CUBIC);
|
205 |
cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
|
205 |
|
206 |
|
206 |
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
|
207 |
//cvtools::writeMat(up0Rect, "up0Rect.mat", "up0Rect");
|
207 |
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
|
208 |
//cvtools::writeMat(up1Rect, "up1Rect.mat", "up1Rect");
|
208 |
|
209 |
|
209 |
// Color debayer and remaps
|
210 |
// color debayer and remap
|
210 |
cv::Mat temp;
|
- |
|
211 |
cv::Mat color0Rect, color1Rect;
|
211 |
cv::Mat color0Rect, color1Rect;
|
- |
|
212 |
frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
|
212 |
cv::cvtColor(frames0[0], temp, CV_BayerBG2RGB);
|
213 |
cv::cvtColor(color0Rect, color0Rect, CV_BayerBG2RGB);
|
213 |
cv::remap(temp, color0Rect, map0X, map0Y, CV_INTER_CUBIC);
|
214 |
cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
|
- |
|
215 |
|
- |
|
216 |
frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
|
214 |
cv::cvtColor(frames1[0], temp, CV_BayerBG2RGB);
|
217 |
cv::cvtColor(color1Rect, color1Rect, CV_BayerBG2RGB);
|
215 |
cv::remap(temp, color1Rect, map1X, map1Y, CV_INTER_CUBIC);
|
218 |
cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
|
- |
|
219 |
|
- |
|
220 |
//cvtools::writeMat(frames0Rect[18], "frames0Rect_18.mat", "frames0Rect_18");
|
- |
|
221 |
//cvtools::writeMat(frames0Rect[19], "frames0Rect_19.mat", "frames0Rect_19");
|
216 |
|
222 |
|
217 |
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
|
223 |
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
|
218 |
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
|
224 |
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
|
219 |
|
225 |
|
220 |
// On/off remaps
|
226 |
// On/off remaps
|
221 |
cv::Mat frames0OnRect, frames0OffRect;
|
227 |
cv::Mat frames0OnRect, frames0OffRect;
|
222 |
cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_CUBIC);
|
228 |
cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_LINEAR);
|
223 |
cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_CUBIC);
|
229 |
cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_LINEAR);
|
224 |
|
230 |
|
225 |
cv::Mat frames1OnRect, frames1OffRect;
|
231 |
cv::Mat frames1OnRect, frames1OffRect;
|
226 |
cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_CUBIC);
|
232 |
cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_LINEAR);
|
227 |
cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_CUBIC);
|
233 |
cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_LINEAR);
|
228 |
|
234 |
|
229 |
// Occlusion masks
|
235 |
// Occlusion masks
|
230 |
cv::Mat occlusion0Rect, occlusion1Rect;
|
236 |
cv::Mat occlusion0Rect, occlusion1Rect;
|
231 |
cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
|
237 |
cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
|
232 |
occlusion0Rect = occlusion0Rect > 10;
|
238 |
occlusion0Rect = (occlusion0Rect > 6400) & (occlusion0Rect < 50000);
|
233 |
cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
|
239 |
cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
|
234 |
occlusion1Rect = occlusion1Rect > 10;
|
240 |
occlusion1Rect = (occlusion1Rect > 6400) & (occlusion1Rect < 50000);
|
235 |
|
241 |
|
236 |
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
|
242 |
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
|
237 |
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
|
243 |
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
|
238 |
|
244 |
|
239 |
// Erode occlusion masks
|
245 |
// Erode occlusion masks
|