1 |
//
|
1 |
//
|
2 |
// Line Shifting Structured Light
|
2 |
// Line Shifting Structured Light
|
3 |
//
|
3 |
//
|
4 |
// This implementation closely follows Jens Guhring "Dense 3D surface acquisition by structured light using off-the-shelf components" (2000).
|
4 |
// This implementation closely follows Jens Guhring "Dense 3D surface acquisition by structured light using off-the-shelf components" (2000).
|
5 |
//
|
5 |
//
|
6 |
|
6 |
|
7 |
#include "AlgorithmLineShift.h"
|
7 |
#include "AlgorithmLineShift.h"
|
8 |
#include <cmath>
|
8 |
#include <cmath>
|
9 |
#include "cvtools.h"
|
9 |
#include "cvtools.h"
|
10 |
#include "algorithmtools.h"
|
10 |
#include "algorithmtools.h"
|
11 |
|
11 |
|
12 |
#include <opencv2/imgproc/imgproc.hpp>
|
12 |
#include <opencv2/imgproc/imgproc.hpp>
|
13 |
|
13 |
|
14 |
static unsigned int nLineShifts = 8; // number of columns over which each line is shifted
|
14 |
static unsigned int nLineShifts = 8; // number of columns over which each line is shifted
|
15 |
|
15 |
|
16 |
// Algorithm
|
16 |
// Algorithm
|
17 |
AlgorithmLineShift::AlgorithmLineShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
|
17 |
AlgorithmLineShift::AlgorithmLineShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
|
18 |
|
18 |
|
19 |
int nTotalBits = ceilf(log2f((float)screenCols));
|
19 |
int nTotalBits = ceilf(log2f((float)screenCols));
|
20 |
|
20 |
|
21 |
// determine the necessary Gray code bits and add some robustness
|
21 |
// determine the necessary Gray code bits and add some robustness
|
22 |
nGrayBits = nTotalBits - floorf(log2f((float)nLineShifts)) + 2;
|
22 |
nGrayBits = nTotalBits - floorf(log2f((float)nLineShifts)) + 2;
|
23 |
|
23 |
|
24 |
N = 2 + 2*nGrayBits + nLineShifts;
|
24 |
N = 2 + 2*nGrayBits + nLineShifts;
|
25 |
|
25 |
|
26 |
// all on pattern
|
26 |
// all on pattern
|
27 |
cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
|
27 |
cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
|
28 |
patterns.push_back(allOn);
|
28 |
patterns.push_back(allOn);
|
29 |
|
29 |
|
30 |
// all off pattern
|
30 |
// all off pattern
|
31 |
cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
|
31 |
cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
|
32 |
patterns.push_back(allOff);
|
32 |
patterns.push_back(allOff);
|
33 |
|
33 |
|
34 |
|
34 |
|
35 |
// Gray code patterns
|
35 |
// Gray code patterns
|
36 |
for(unsigned int p=0; p<nGrayBits; p++){
|
36 |
for(unsigned int p=0; p<nGrayBits; p++){
|
37 |
cv::Mat pattern(1, screenCols, CV_8UC3);
|
37 |
cv::Mat pattern(1, screenCols, CV_8UC3);
|
38 |
cv::Mat patternInv(1, screenCols, CV_8UC3);
|
38 |
cv::Mat patternInv(1, screenCols, CV_8UC3);
|
39 |
|
39 |
|
40 |
for(unsigned int j=0; j<screenCols; j++){
|
40 |
for(unsigned int j=0; j<screenCols; j++){
|
41 |
|
41 |
|
42 |
unsigned int jGray = binaryToGray(j);
|
42 |
unsigned int jGray = binaryToGray(j);
|
43 |
// Amplitude of channels
|
43 |
// Amplitude of channels
|
44 |
int bit = (int)getBit(jGray, nTotalBits-p);
|
44 |
int bit = (int)getBit(jGray, nTotalBits-p);
|
45 |
pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
|
45 |
pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
|
46 |
int invBit = bit^1;
|
46 |
int invBit = bit^1;
|
47 |
patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
|
47 |
patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
|
48 |
}
|
48 |
}
|
49 |
patterns.push_back(pattern);
|
49 |
patterns.push_back(pattern);
|
50 |
patterns.push_back(patternInv);
|
50 |
patterns.push_back(patternInv);
|
51 |
}
|
51 |
}
|
52 |
|
52 |
|
53 |
// line shifts
|
53 |
// line shifts
|
54 |
for(unsigned int p=0; p<nLineShifts; p++){
|
54 |
for(unsigned int p=0; p<nLineShifts; p++){
|
55 |
cv::Mat pattern(1, screenCols, CV_8UC3, cv::Scalar(0));
|
55 |
cv::Mat pattern(1, screenCols, CV_8UC3, cv::Scalar(0));
|
56 |
|
56 |
|
57 |
for(unsigned int j=p; j<screenCols; j+= nLineShifts)
|
57 |
for(unsigned int j=p; j<screenCols; j+= nLineShifts)
|
58 |
pattern.at<cv::Vec3b>(0, j) = cv::Vec3b(255, 255, 255);
|
58 |
pattern.at<cv::Vec3b>(0, j) = cv::Vec3b(255, 255, 255);
|
59 |
|
59 |
|
60 |
patterns.push_back(pattern);
|
60 |
patterns.push_back(pattern);
|
61 |
}
|
61 |
}
|
62 |
|
62 |
|
63 |
}
|
63 |
}
|
64 |
|
64 |
|
65 |
cv::Mat AlgorithmLineShift::getEncodingPattern(unsigned int depth){
|
65 |
cv::Mat AlgorithmLineShift::getEncodingPattern(unsigned int depth){
|
66 |
return patterns[depth];
|
66 |
return patterns[depth];
|
67 |
}
|
67 |
}
|
68 |
|
68 |
|
69 |
//static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
|
69 |
//static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
|
70 |
// assert(!img.empty());
|
70 |
// assert(!img.empty());
|
71 |
// assert(img.channels() == 3);
|
71 |
// assert(img.channels() == 3);
|
72 |
|
72 |
|
73 |
// int x = (int)pt.x;
|
73 |
// int x = (int)pt.x;
|
74 |
// int y = (int)pt.y;
|
74 |
// int y = (int)pt.y;
|
75 |
|
75 |
|
76 |
// int x0 = cv::borderInterpolate(x, img.cols, cv::BORDER_REFLECT_101);
|
76 |
// int x0 = cv::borderInterpolate(x, img.cols, cv::BORDER_REFLECT_101);
|
77 |
// int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
|
77 |
// int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
|
78 |
// int y0 = cv::borderInterpolate(y, img.rows, cv::BORDER_REFLECT_101);
|
78 |
// int y0 = cv::borderInterpolate(y, img.rows, cv::BORDER_REFLECT_101);
|
79 |
// int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
|
79 |
// int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
|
80 |
|
80 |
|
81 |
// float a = pt.x - (float)x;
|
81 |
// float a = pt.x - (float)x;
|
82 |
// float c = pt.y - (float)y;
|
82 |
// float c = pt.y - (float)y;
|
83 |
|
83 |
|
84 |
// uchar b = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[0] * a) * (1.f - c)
|
84 |
// uchar b = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[0] * a) * (1.f - c)
|
85 |
// + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
|
85 |
// + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
|
86 |
// uchar g = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[1] * a) * (1.f - c)
|
86 |
// uchar g = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[1] * a) * (1.f - c)
|
87 |
// + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
|
87 |
// + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
|
88 |
// uchar r = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[2] * a) * (1.f - c)
|
88 |
// uchar r = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[2] * a) * (1.f - c)
|
89 |
// + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
|
89 |
// + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
|
90 |
|
90 |
|
91 |
// return cv::Vec3b(b, g, r);
|
91 |
// return cv::Vec3b(b, g, r);
|
92 |
//}
|
92 |
//}
|
93 |
|
93 |
|
94 |
void getlineCenters(const cv::Mat& linesScanLine, const cv::Mat& codeScanLine, std::vector<cv::Vec2f>& lineCenters){
|
94 |
void getlineCenters(const cv::Mat& linesScanLine, const cv::Mat& codeScanLine, std::vector<cv::Vec2f>& lineCenters){
|
95 |
|
95 |
|
96 |
int nCols = linesScanLine.cols;
|
96 |
int nCols = linesScanLine.cols;
|
97 |
|
97 |
|
98 |
// finite derivative
|
98 |
// finite derivative
|
99 |
cv::Mat g(1, nCols, CV_32F);
|
99 |
cv::Mat g(1, nCols, CV_32F);
|
100 |
cv::Mat h(1, nCols, CV_32F);
|
100 |
cv::Mat h(1, nCols, CV_32F);
|
101 |
for(int i=2; i<nCols-2; i++){
|
101 |
for(int i=2; i<nCols-2; i++){
|
102 |
// g.at<float>(0, i) = linesScanLine.at<unsigned char>(0, i+2)+linesScanLine.at<unsigned char>(0, i+1)-
|
102 |
// g.at<float>(0, i) = linesScanLine.at<unsigned char>(0, i+2)+linesScanLine.at<unsigned char>(0, i+1)-
|
103 |
// linesScanLine.at<unsigned char>(0, i-1)-linesScanLine.at<unsigned char>(0, i-2);
|
103 |
// linesScanLine.at<unsigned char>(0, i-1)-linesScanLine.at<unsigned char>(0, i-2);
|
104 |
g.at<float>(0, i) = 1.0*linesScanLine.at<unsigned char>(0, i+2)+8.0*linesScanLine.at<unsigned char>(0, i+1)-
|
104 |
g.at<float>(0, i) = 1.0*linesScanLine.at<unsigned char>(0, i+2)+8.0*linesScanLine.at<unsigned char>(0, i+1)-
|
105 |
8.0*linesScanLine.at<unsigned char>(0, i-1)-1.0*linesScanLine.at<unsigned char>(0, i-2);
|
105 |
8.0*linesScanLine.at<unsigned char>(0, i-1)-1.0*linesScanLine.at<unsigned char>(0, i-2);
|
106 |
h.at<float>(0, i) = -1.0*linesScanLine.at<unsigned char>(0, i+2)+16.0*linesScanLine.at<unsigned char>(0, i+1)-
|
106 |
h.at<float>(0, i) = -1.0*linesScanLine.at<unsigned char>(0, i+2)+16.0*linesScanLine.at<unsigned char>(0, i+1)-
|
107 |
30.0*linesScanLine.at<unsigned char>(0, i)+
|
107 |
30.0*linesScanLine.at<unsigned char>(0, i)+
|
108 |
16.0*linesScanLine.at<unsigned char>(0, i-1)-1.0*linesScanLine.at<unsigned char>(0, i-2);
|
108 |
16.0*linesScanLine.at<unsigned char>(0, i-1)-1.0*linesScanLine.at<unsigned char>(0, i-2);
|
109 |
}
|
109 |
}
|
110 |
// cvtools::writeMat(codeScanLine, "codeScanLine.mat", "codeScanLine");
|
110 |
// cvtools::writeMat(codeScanLine, "codeScanLine.mat", "codeScanLine");
|
111 |
// cvtools::writeMat(linesScanLine, "linesScanLine.mat", "linesScanLine");
|
111 |
// cvtools::writeMat(linesScanLine, "linesScanLine.mat", "linesScanLine");
|
112 |
// cvtools::writeMat(der, "der.mat", "der");
|
112 |
// cvtools::writeMat(der, "der.mat", "der");
|
113 |
|
113 |
|
114 |
for(int i=0; i<nCols-1; i++){
|
114 |
for(int i=0; i<nCols-1; i++){
|
115 |
// float fLeft = linesScanLine.at<unsigned char>(0, i-1);
|
115 |
// float fLeft = linesScanLine.at<unsigned char>(0, i-1);
|
116 |
float fI = linesScanLine.at<unsigned char>(0, i);
|
116 |
float fI = linesScanLine.at<unsigned char>(0, i);
|
117 |
// float fRight = linesScanLine.at<unsigned char>(0, i+1);
|
117 |
// float fRight = linesScanLine.at<unsigned char>(0, i+1);
|
118 |
|
118 |
|
119 |
float gI = g.at<float>(0, i);
|
119 |
float gI = g.at<float>(0, i);
|
120 |
float gRight = g.at<float>(0, i+1);
|
120 |
float gRight = g.at<float>(0, i+1);
|
121 |
|
121 |
|
122 |
float hI = h.at<float>(0, i);
|
122 |
float hI = h.at<float>(0, i);
|
123 |
|
123 |
|
124 |
int codeI = codeScanLine.at<int>(0, i);
|
124 |
int codeI = codeScanLine.at<int>(0, i);
|
125 |
//int codeRight = codeScanLine.at<int>(0, i+1);
|
125 |
//int codeRight = codeScanLine.at<int>(0, i+1);
|
126 |
|
126 |
|
127 |
if((codeI != -1) && (fI > 10) && (gI >= 0.0) && (gRight <= 0.0) && (gRight < gI) && (hI < -1.0)){
|
127 |
if((codeI != -1) && (fI > 10) && (gI >= 0.0) && (gRight <= 0.0) && (gRight < gI) && (hI < -1.0)){
|
128 |
float delta = gI/(gI - gRight);
|
128 |
float delta = gI/(gI - gRight);
|
129 |
lineCenters.push_back(cv::Vec2f(i + delta, codeI));
|
129 |
lineCenters.push_back(cv::Vec2f(i + delta, codeI));
|
130 |
}
|
130 |
}
|
131 |
|
131 |
|
132 |
}
|
132 |
}
|
133 |
|
133 |
|
134 |
}
|
134 |
}
|
135 |
|
135 |
|
136 |
void AlgorithmLineShift::get3DPoints(const SMCalibrationParameters & calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
|
136 |
void AlgorithmLineShift::get3DPoints(const SMCalibrationParameters & calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3f>& color){
|
137 |
|
137 |
|
138 |
assert(frames0.size() == N);
|
138 |
assert(frames0.size() == N);
|
139 |
assert(frames1.size() == N);
|
139 |
assert(frames1.size() == N);
|
140 |
|
140 |
|
141 |
int frameRows = frames0[0].rows;
|
141 |
int frameRows = frames0[0].rows;
|
142 |
int frameCols = frames0[0].cols;
|
142 |
int frameCols = frames0[0].cols;
|
143 |
|
143 |
|
144 |
// rectifying homographies (rotation+projections)
|
144 |
// rectifying homographies (rotation+projections)
|
145 |
cv::Size frameSize(frameCols, frameRows);
|
145 |
cv::Size frameSize(frameCols, frameRows);
|
146 |
cv::Mat R, T;
|
146 |
cv::Mat R, T;
|
147 |
// stereoRectify segfaults unless R is double precision
|
147 |
// stereoRectify segfaults unless R is double precision
|
148 |
cv::Mat(calibration.R1).convertTo(R, CV_64F);
|
148 |
cv::Mat(calibration.R1).convertTo(R, CV_64F);
|
149 |
cv::Mat(calibration.T1).convertTo(T, CV_64F);
|
149 |
cv::Mat(calibration.T1).convertTo(T, CV_64F);
|
150 |
cv::Mat R0, R1, P0, P1, QRect;
|
150 |
cv::Mat R0, R1, P0, P1, QRect;
|
151 |
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
|
151 |
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
|
152 |
|
152 |
|
153 |
// interpolation maps
|
153 |
// interpolation maps
|
154 |
cv::Mat map0X, map0Y, map1X, map1Y;
|
154 |
cv::Mat map0X, map0Y, map1X, map1Y;
|
155 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
155 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
156 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
156 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
157 |
|
157 |
|
158 |
// gray-scale and remap
|
158 |
// gray-scale and remap
|
159 |
std::vector<cv::Mat> frames0Rect(N);
|
159 |
std::vector<cv::Mat> frames0Rect(N);
|
160 |
std::vector<cv::Mat> frames1Rect(N);
|
160 |
std::vector<cv::Mat> frames1Rect(N);
|
161 |
for(unsigned int i=0; i<N; i++){
|
161 |
for(unsigned int i=0; i<N; i++){
|
162 |
cv::Mat temp;
|
162 |
cv::Mat temp;
|
163 |
cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
|
163 |
cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
|
164 |
cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_CUBIC);
|
164 |
cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_CUBIC);
|
165 |
cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
|
165 |
cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
|
166 |
cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_CUBIC);
|
166 |
cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_CUBIC);
|
167 |
}
|
167 |
}
|
168 |
|
168 |
|
169 |
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
|
169 |
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
|
170 |
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
|
170 |
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
|
171 |
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
|
171 |
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
|
172 |
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
|
172 |
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
|
173 |
|
173 |
|
174 |
// color debayer and remap
|
174 |
// color debayer and remap
|
175 |
cv::Mat color0Rect, color1Rect;
|
175 |
cv::Mat color0Rect, color1Rect;
|
176 |
cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
|
- |
|
177 |
cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_CUBIC);
|
176 |
cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
|
178 |
|
- |
|
179 |
cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
|
- |
|
180 |
cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_CUBIC);
|
177 |
cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
|
181 |
|
178 |
|
182 |
int frameRectRows = frames0Rect[0].rows;
|
179 |
int frameRectRows = frames0Rect[0].rows;
|
183 |
int frameRectCols = frames0Rect[0].cols;
|
180 |
int frameRectCols = frames0Rect[0].cols;
|
184 |
|
181 |
|
185 |
// occlusion masks
|
182 |
// occlusion masks
|
186 |
cv::Mat occlusion0Rect, occlusion1Rect;
|
183 |
cv::Mat occlusion0Rect, occlusion1Rect;
|
187 |
cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
|
184 |
cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
|
188 |
occlusion0Rect = (occlusion0Rect > 20) & (occlusion0Rect < 250);
|
185 |
occlusion0Rect = (occlusion0Rect > 0.1) & (occlusion0Rect < 0.98);
|
189 |
cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
|
186 |
cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
|
190 |
occlusion1Rect = (occlusion1Rect > 20) & (occlusion1Rect < 250);
|
187 |
occlusion1Rect = (occlusion1Rect > 0.1) & (occlusion1Rect < 0.98);
|
191 |
|
188 |
|
192 |
// cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
|
189 |
// cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
|
193 |
// cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
|
190 |
// cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
|
194 |
|
191 |
|
195 |
// erode occlusion masks
|
192 |
// erode occlusion masks
|
196 |
cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
|
193 |
cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
|
197 |
cv::erode(occlusion0Rect, occlusion0Rect, strel);
|
194 |
cv::erode(occlusion0Rect, occlusion0Rect, strel);
|
198 |
cv::erode(occlusion1Rect, occlusion1Rect, strel);
|
195 |
cv::erode(occlusion1Rect, occlusion1Rect, strel);
|
199 |
|
196 |
|
200 |
// // correct for texture modulation and ambient
|
197 |
// // correct for texture modulation and ambient
|
201 |
// cv::Mat A0 = frames0Rect[1];
|
198 |
// cv::Mat A0 = frames0Rect[1];
|
202 |
// cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
|
199 |
// cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
|
203 |
|
200 |
|
204 |
// cv::divide(256.0, M0, M0, CV_32F);
|
201 |
// cv::divide(256.0, M0, M0, CV_32F);
|
205 |
// cv::Mat A1 = frames1Rect[1];
|
202 |
// cv::Mat A1 = frames1Rect[1];
|
206 |
// cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
|
203 |
// cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
|
207 |
// cv::divide(256.0, M1, M1, CV_32F);
|
204 |
// cv::divide(256.0, M1, M1, CV_32F);
|
208 |
|
205 |
|
209 |
// for(int i=2; i<N; i++){
|
206 |
// for(int i=2; i<N; i++){
|
210 |
// cv::multiply(frames0Rect[i]-A0, M0, frames0Rect[i], 1.0, CV_8UC1);
|
207 |
// cv::multiply(frames0Rect[i]-A0, M0, frames0Rect[i], 1.0, CV_8UC1);
|
211 |
// cv::multiply(frames1Rect[i]-A1, M1, frames1Rect[i], 1.0, CV_8UC1);
|
208 |
// cv::multiply(frames1Rect[i]-A1, M1, frames1Rect[i], 1.0, CV_8UC1);
|
212 |
// }
|
209 |
// }
|
213 |
|
210 |
|
214 |
//cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
|
211 |
//cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
|
215 |
//cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
|
212 |
//cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
|
216 |
|
213 |
|
217 |
// divide into Gray coding frames and line shift frames
|
214 |
// divide into Gray coding frames and line shift frames
|
218 |
std::vector<cv::Mat> frames0GrayCode(frames0Rect.begin()+2, frames0Rect.begin()+2+2*nGrayBits);
|
215 |
std::vector<cv::Mat> frames0GrayCode(frames0Rect.begin()+2, frames0Rect.begin()+2+2*nGrayBits);
|
219 |
std::vector<cv::Mat> frames0LineShift(frames0Rect.begin()+2+2*nGrayBits, frames0Rect.end());
|
216 |
std::vector<cv::Mat> frames0LineShift(frames0Rect.begin()+2+2*nGrayBits, frames0Rect.end());
|
220 |
std::vector<cv::Mat> frames1GrayCode(frames1Rect.begin()+2, frames1Rect.begin()+2+2*nGrayBits);
|
217 |
std::vector<cv::Mat> frames1GrayCode(frames1Rect.begin()+2, frames1Rect.begin()+2+2*nGrayBits);
|
221 |
std::vector<cv::Mat> frames1LineShift(frames1Rect.begin()+2+2*nGrayBits, frames1Rect.end());
|
218 |
std::vector<cv::Mat> frames1LineShift(frames1Rect.begin()+2+2*nGrayBits, frames1Rect.end());
|
222 |
|
219 |
|
223 |
// decode patterns
|
220 |
// decode patterns
|
224 |
cv::Mat code0Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
|
221 |
cv::Mat code0Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
|
225 |
cv::Mat code1Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
|
222 |
cv::Mat code1Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
|
226 |
|
223 |
|
227 |
// into gray code
|
224 |
// into gray code
|
228 |
for(unsigned int i=0; i<nGrayBits; i++){
|
225 |
for(unsigned int i=0; i<nGrayBits; i++){
|
229 |
cv::Mat temp, bit0, bit1;
|
226 |
cv::Mat temp, bit0, bit1;
|
230 |
|
227 |
|
231 |
cv::compare(frames0GrayCode[i*2], frames0GrayCode[i*2+1], temp, cv::CMP_GT);
|
228 |
cv::compare(frames0GrayCode[i*2], frames0GrayCode[i*2+1], temp, cv::CMP_GT);
|
232 |
temp.convertTo(bit0, CV_32S, 1.0/255.0);
|
229 |
temp.convertTo(bit0, CV_32S, 1.0/255.0);
|
233 |
cv::add(code0Gray, bit0*twopowi(nGrayBits-i-1), code0Gray, cv::noArray(), CV_32S);
|
230 |
cv::add(code0Gray, bit0*twopowi(nGrayBits-i-1), code0Gray, cv::noArray(), CV_32S);
|
234 |
|
231 |
|
235 |
cv::compare(frames1GrayCode[i*2], frames1GrayCode[i*2+1], temp, cv::CMP_GT);
|
232 |
cv::compare(frames1GrayCode[i*2], frames1GrayCode[i*2+1], temp, cv::CMP_GT);
|
236 |
temp.convertTo(bit1, CV_32S, 1.0/255.0);
|
233 |
temp.convertTo(bit1, CV_32S, 1.0/255.0);
|
237 |
cv::add(code1Gray, bit1*twopowi(nGrayBits-i-1), code1Gray, cv::noArray(), CV_32S);
|
234 |
cv::add(code1Gray, bit1*twopowi(nGrayBits-i-1), code1Gray, cv::noArray(), CV_32S);
|
238 |
}
|
235 |
}
|
239 |
|
236 |
|
240 |
// convert to standard binary
|
237 |
// convert to standard binary
|
241 |
cv::Mat code0Binary(code0Gray.rows, code0Gray.cols, CV_32S, cv::Scalar(-1));
|
238 |
cv::Mat code0Binary(code0Gray.rows, code0Gray.cols, CV_32S, cv::Scalar(-1));
|
242 |
cv::Mat code1Binary(code1Gray.rows, code1Gray.cols, CV_32S, cv::Scalar(-1));
|
239 |
cv::Mat code1Binary(code1Gray.rows, code1Gray.cols, CV_32S, cv::Scalar(-1));
|
243 |
for(int r=0; r<frameRectRows; r++){
|
240 |
for(int r=0; r<frameRectRows; r++){
|
244 |
for(int c=0; c<frameRectCols; c++){
|
241 |
for(int c=0; c<frameRectCols; c++){
|
245 |
code0Binary.at<int>(r,c) = grayToBinary(code0Gray.at<int>(r,c));
|
242 |
code0Binary.at<int>(r,c) = grayToBinary(code0Gray.at<int>(r,c));
|
246 |
code1Binary.at<int>(r,c) = grayToBinary(code1Gray.at<int>(r,c));
|
243 |
code1Binary.at<int>(r,c) = grayToBinary(code1Gray.at<int>(r,c));
|
247 |
}
|
244 |
}
|
248 |
}
|
245 |
}
|
249 |
|
246 |
|
250 |
// set occluded pixels to -1
|
247 |
// set occluded pixels to -1
|
251 |
for(int r=0; r<frameRectRows; r++){
|
248 |
for(int r=0; r<frameRectRows; r++){
|
252 |
for(int c=0; c<frameRectCols; c++){
|
249 |
for(int c=0; c<frameRectCols; c++){
|
253 |
if(occlusion0Rect.at<unsigned char>(r,c) == 0)
|
250 |
if(occlusion0Rect.at<unsigned char>(r,c) == 0)
|
254 |
code0Binary.at<int>(r,c) = -1;
|
251 |
code0Binary.at<int>(r,c) = -1;
|
255 |
if(occlusion1Rect.at<unsigned char>(r,c) == 0)
|
252 |
if(occlusion1Rect.at<unsigned char>(r,c) == 0)
|
256 |
code1Binary.at<int>(r,c) = -1;
|
253 |
code1Binary.at<int>(r,c) = -1;
|
257 |
}
|
254 |
}
|
258 |
}
|
255 |
}
|
259 |
|
256 |
|
260 |
//cvtools::writeMat(code0Gray, "code0Gray.mat", "code0Gray");
|
257 |
//cvtools::writeMat(code0Gray, "code0Gray.mat", "code0Gray");
|
261 |
//cvtools::writeMat(code1Gray, "code1Gray.mat", "code1Gray");
|
258 |
//cvtools::writeMat(code1Gray, "code1Gray.mat", "code1Gray");
|
262 |
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
|
259 |
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
|
263 |
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
|
260 |
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
|
264 |
|
261 |
|
265 |
// matching
|
262 |
// matching
|
266 |
std::vector<cv::Vec2f> q0, q1;
|
263 |
std::vector<cv::Vec2f> q0, q1;
|
267 |
for(unsigned int s=0; s<nLineShifts; s++){
|
264 |
for(unsigned int s=0; s<nLineShifts; s++){
|
268 |
|
265 |
|
269 |
cv::Mat lines0 = frames0LineShift[s];
|
266 |
cv::Mat lines0 = frames0LineShift[s];
|
270 |
cv::Mat lines1 = frames1LineShift[s];
|
267 |
cv::Mat lines1 = frames1LineShift[s];
|
271 |
|
268 |
|
272 |
for(int row=0; row<frameRectRows; row++){
|
269 |
for(int row=0; row<frameRectRows; row++){
|
273 |
|
270 |
|
274 |
// line center data structure containing [x-coordinate (sub-px), region-code]
|
271 |
// line center data structure containing [x-coordinate (sub-px), region-code]
|
275 |
std::vector<cv::Vec2f> lineCenters0, lineCenters1;
|
272 |
std::vector<cv::Vec2f> lineCenters0, lineCenters1;
|
276 |
|
273 |
|
277 |
// sorted, unique line centers
|
274 |
// sorted, unique line centers
|
278 |
getlineCenters(lines0.row(row), code0Binary.row(row), lineCenters0);
|
275 |
getlineCenters(lines0.row(row), code0Binary.row(row), lineCenters0);
|
279 |
getlineCenters(lines1.row(row), code1Binary.row(row), lineCenters1);
|
276 |
getlineCenters(lines1.row(row), code1Binary.row(row), lineCenters1);
|
280 |
|
277 |
|
281 |
// if(s==0 && row==1300){
|
278 |
// if(s==0 && row==1300){
|
282 |
// std::cout << cv::Mat(lineCenters0) << std::endl;
|
279 |
// std::cout << cv::Mat(lineCenters0) << std::endl;
|
283 |
// std::cout << cv::Mat(lineCenters1) << std::endl;
|
280 |
// std::cout << cv::Mat(lineCenters1) << std::endl;
|
284 |
|
281 |
|
285 |
// cvtools::writeMat(lines0.row(row), "lines0.mat", "lines0");
|
282 |
// cvtools::writeMat(lines0.row(row), "lines0.mat", "lines0");
|
286 |
// cvtools::writeMat(lines1.row(row), "lines1.mat", "lines1");
|
283 |
// cvtools::writeMat(lines1.row(row), "lines1.mat", "lines1");
|
287 |
// cvtools::writeMat(code0Binary.row(row), "code0Binary.mat", "code0Binary");
|
284 |
// cvtools::writeMat(code0Binary.row(row), "code0Binary.mat", "code0Binary");
|
288 |
// cvtools::writeMat(code1Binary.row(row), "code1Binary.mat", "code1Binary");
|
285 |
// cvtools::writeMat(code1Binary.row(row), "code1Binary.mat", "code1Binary");
|
289 |
// }
|
286 |
// }
|
290 |
|
287 |
|
291 |
// match and store
|
288 |
// match and store
|
292 |
unsigned int i=0, j=0;
|
289 |
unsigned int i=0, j=0;
|
293 |
while(i<lineCenters0.size() && j<lineCenters1.size()){
|
290 |
while(i<lineCenters0.size() && j<lineCenters1.size()){
|
294 |
|
291 |
|
295 |
if(lineCenters0[i][1] == lineCenters1[j][1]){
|
292 |
if(lineCenters0[i][1] == lineCenters1[j][1]){
|
296 |
q0.push_back(cv::Point2f(lineCenters0[i][0], row));
|
293 |
q0.push_back(cv::Point2f(lineCenters0[i][0], row));
|
297 |
q1.push_back(cv::Point2f(lineCenters1[j][0], row));
|
294 |
q1.push_back(cv::Point2f(lineCenters1[j][0], row));
|
298 |
i += 1;
|
295 |
i += 1;
|
299 |
j += 1;
|
296 |
j += 1;
|
300 |
} else if(lineCenters0[i][1] < lineCenters1[j][1]){
|
297 |
} else if(lineCenters0[i][1] < lineCenters1[j][1]){
|
301 |
i += 1;
|
298 |
i += 1;
|
302 |
} else if(lineCenters0[i][1] > lineCenters1[j][1]){
|
299 |
} else if(lineCenters0[i][1] > lineCenters1[j][1]){
|
303 |
j += 1;
|
300 |
j += 1;
|
304 |
}
|
301 |
}
|
305 |
}
|
302 |
}
|
306 |
|
303 |
|
307 |
}
|
304 |
}
|
308 |
}
|
305 |
}
|
309 |
|
306 |
|
310 |
int nMatches = q0.size();
|
307 |
int nMatches = q0.size();
|
311 |
|
308 |
|
312 |
if(nMatches < 1){
|
309 |
if(nMatches < 1){
|
313 |
Q.resize(0);
|
310 |
Q.resize(0);
|
314 |
color.resize(0);
|
311 |
color.resize(0);
|
315 |
|
312 |
|
316 |
return;
|
313 |
return;
|
317 |
}
|
314 |
}
|
318 |
|
315 |
|
319 |
// retrieve color information (at integer coordinates)
|
316 |
// retrieve color information (at integer coordinates)
|
320 |
color.resize(nMatches);
|
317 |
color.resize(nMatches);
|
321 |
for(int i=0; i<nMatches; i++){
|
318 |
for(int i=0; i<nMatches; i++){
|
322 |
|
319 |
|
323 |
cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0[i][1], q0[i][0]);
|
320 |
cv::Vec3f c0 = color0Rect.at<cv::Vec3f>(q0[i][1], q0[i][0]);
|
324 |
cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1[i][1], q1[i][0]);
|
321 |
cv::Vec3f c1 = color1Rect.at<cv::Vec3f>(q1[i][1], q1[i][0]);
|
325 |
// cv::Vec3b c0 = getColorSubpix(color0Rect, q0[i]);
|
322 |
// cv::Vec3b c0 = getColorSubpix(color0Rect, q0[i]);
|
326 |
// cv::Vec3b c1 = getColorSubpix(color1Rect, q0[i]);
|
323 |
// cv::Vec3b c1 = getColorSubpix(color1Rect, q0[i]);
|
327 |
|
324 |
|
328 |
color[i] = 0.5*c0 + 0.5*c1;
|
325 |
color[i] = 0.5*c0 + 0.5*c1;
|
329 |
}
|
326 |
}
|
330 |
|
327 |
|
331 |
// Triangulate by means of disparity projection
|
328 |
// Triangulate by means of disparity projection
|
332 |
Q.resize(q0.size());
|
329 |
Q.resize(q0.size());
|
333 |
cv::Matx44f QRectx = cv::Matx44f(QRect);
|
330 |
cv::Matx44f QRectx = cv::Matx44f(QRect);
|
334 |
cv::Matx33f R0invx = cv::Matx33f(cv::Mat(R0.t()));
|
331 |
cv::Matx33f R0invx = cv::Matx33f(cv::Mat(R0.t()));
|
335 |
|
332 |
|
336 |
#pragma omp parallel for
|
333 |
#pragma omp parallel for
|
337 |
for(unsigned int i=0; i<q0.size(); i++){
|
334 |
for(unsigned int i=0; i<q0.size(); i++){
|
338 |
float disparity = q0[i][0]-q1[i][0];
|
335 |
float disparity = q0[i][0]-q1[i][0];
|
339 |
cv::Vec4f Qih = QRectx*cv::Vec4f(q0[i][0], q0[i][1], disparity, 1.0);
|
336 |
cv::Vec4f Qih = QRectx*cv::Vec4f(q0[i][0], q0[i][1], disparity, 1.0);
|
340 |
float winv = float(1.0)/Qih[3];
|
337 |
float winv = float(1.0)/Qih[3];
|
341 |
Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
|
338 |
Q[i] = R0invx * cv::Point3f(Qih[0]*winv, Qih[1]*winv, Qih[2]*winv);
|
342 |
}
|
339 |
}
|
343 |
|
340 |
|
344 |
}
|
341 |
}
|
345 |
|
342 |
|