Subversion Repositories seema-scanner

Rev

Rev 123 | Rev 125 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 123 Rev 124
1
#include "AlgorithmLineShift.h"
1
#include "AlgorithmLineShift.h"
2
#include <cmath>
2
#include <cmath>
3
#include "cvtools.h"
3
#include "cvtools.h"
4
 
4
 
5
#include <opencv2/imgproc.hpp>
5
#include <opencv2/imgproc/imgproc.hpp>
6
 
6
 
7
#ifndef log2f
7
#ifndef log2f
8
#define log2f(x) (log(x)/log(2.0))
8
#define log2f(x) (log(x)/log(2.0))
9
#endif
9
#endif
10
 
10
 
11
static unsigned int nLineShifts = 8; // number of columns over which each line is shifted
11
static unsigned int nLineShifts = 8; // number of columns over which each line is shifted
12
 
12
 
13
/*
13
/*
14
 * The purpose of this function is to convert an unsigned
14
 * The purpose of this function is to convert an unsigned
15
 * binary number to reflected binary Gray code.
15
 * binary number to reflected binary Gray code.
16
 *
16
 *
17
 * The operator >> is shift right. The operator ^ is exclusive or.
17
 * The operator >> is shift right. The operator ^ is exclusive or.
18
 * Source: http://en.wikipedia.org/wiki/Gray_code
18
 * Source: http://en.wikipedia.org/wiki/Gray_code
19
 */
19
 */
20
static unsigned int binaryToGray(unsigned int num) {
20
static unsigned int binaryToGray(unsigned int num) {
21
    return (num >> 1) ^ num;
21
    return (num >> 1) ^ num;
22
}
22
}
23
 
23
 
24
/*
24
/*
25
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
25
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
26
 * The purpose of this function is to convert a reflected binary
26
 * The purpose of this function is to convert a reflected binary
27
 * Gray code number to a binary number.
27
 * Gray code number to a binary number.
28
 */
28
 */
29
static unsigned int grayToBinary(unsigned int num){
29
static unsigned int grayToBinary(unsigned int num){
30
    unsigned int mask;
30
    unsigned int mask;
31
    for(mask = num >> 1; mask != 0; mask = mask >> 1)
31
    for(mask = num >> 1; mask != 0; mask = mask >> 1)
32
        num = num ^ mask;
32
        num = num ^ mask;
33
    return num;
33
    return num;
34
}
34
}
35
 
35
 
36
/*
36
/*
37
 * Return the Nth bit of an unsigned integer number
37
 * Return the Nth bit of an unsigned integer number
38
 */
38
 */
39
static bool getBit(int decimal, int N){
39
static bool getBit(int decimal, int N){
40
 
40
 
41
    return decimal & 1 << (N-1);
41
    return decimal & 1 << (N-1);
42
}
42
}
43
 
43
 
44
/*
44
/*
45
 * Return the number of bits set in an integer
45
 * Return the number of bits set in an integer
46
 */
46
 */
47
static int countBits(int n) {
47
static int countBits(int n) {
48
  unsigned int c; // c accumulates the total bits set in v
48
  unsigned int c; // c accumulates the total bits set in v
49
  for (c = 0; n>0; c++)
49
  for (c = 0; n>0; c++)
50
    n &= n - 1; // clear the least significant bit set
50
    n &= n - 1; // clear the least significant bit set
51
  return c;
51
  return c;
52
}
52
}
53
 
53
 
54
/*
54
/*
55
 * Return the position of the least significant bit that is set
55
 * Return the position of the least significant bit that is set
56
 */
56
 */
57
static int leastSignificantBitSet(int x){
57
static int leastSignificantBitSet(int x){
58
  if(x == 0)
58
  if(x == 0)
59
      return 0;
59
      return 0;
60
 
60
 
61
  int val = 1;
61
  int val = 1;
62
  while(x>>=1)
62
  while(x>>=1)
63
      val++;
63
      val++;
64
 
64
 
65
  return val;
65
  return val;
66
}
66
}
67
 
67
 
68
static inline unsigned int powi(int num, unsigned int exponent){
68
static inline unsigned int powi(int num, unsigned int exponent){
69
 
69
 
70
    if(exponent == 0)
70
    if(exponent == 0)
71
        return 1;
71
        return 1;
72
 
72
 
73
    float res = num;
73
    float res = num;
74
    for(unsigned int i=0; i<exponent-1; i++)
74
    for(unsigned int i=0; i<exponent-1; i++)
75
        res *= num;
75
        res *= num;
76
 
76
 
77
    return res;
77
    return res;
78
}
78
}
79
 
79
 
80
static inline unsigned int twopowi(unsigned int exponent){
80
static inline unsigned int twopowi(unsigned int exponent){
81
 
81
 
82
    return 1 << exponent;
82
    return 1 << exponent;
83
}
83
}
84
 
84
 
85
// Algorithm
85
// Algorithm
86
AlgorithmLineShift::AlgorithmLineShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
86
AlgorithmLineShift::AlgorithmLineShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
87
 
87
 
88
    nGrayBits = ceilf(log2f((float)screenCols)) - floorf(log2f((float)nLineShifts));
88
    nGrayBits = ceilf(log2f((float)screenCols)) - floorf(log2f((float)nLineShifts));
89
 
89
 
90
    N = 2 + 2*nGrayBits + nLineShifts;
90
    N = 2 + 2*nGrayBits + nLineShifts;
91
 
91
 
92
    // all on pattern
92
    // all on pattern
93
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
93
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
94
    patterns.push_back(allOn);
94
    patterns.push_back(allOn);
95
 
95
 
96
    // all off pattern
96
    // all off pattern
97
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
97
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
98
    patterns.push_back(allOff);
98
    patterns.push_back(allOff);
99
 
99
 
100
 
100
 
101
    // Gray code patterns
101
    // Gray code patterns
102
    for(unsigned int p=0; p<nGrayBits; p++){
102
    for(unsigned int p=0; p<nGrayBits; p++){
103
        cv::Mat pattern(1, screenCols, CV_8UC3);
103
        cv::Mat pattern(1, screenCols, CV_8UC3);
104
        cv::Mat patternInv(1, screenCols, CV_8UC3);
104
        cv::Mat patternInv(1, screenCols, CV_8UC3);
105
 
105
 
106
        for(unsigned int j=0; j<screenCols; j++){
106
        for(unsigned int j=0; j<screenCols; j++){
107
 
107
 
108
            unsigned int jGray = binaryToGray(j);
108
            unsigned int jGray = binaryToGray(j);
109
            // Amplitude of channels
109
            // Amplitude of channels
110
            int bit = (int)getBit(jGray, nGrayBits-p);
110
            int bit = (int)getBit(jGray, nGrayBits-p);
111
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
111
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
112
            int invBit = bit^1;
112
            int invBit = bit^1;
113
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
113
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
114
        }
114
        }
115
        patterns.push_back(pattern);
115
        patterns.push_back(pattern);
116
        patterns.push_back(patternInv);
116
        patterns.push_back(patternInv);
117
    }
117
    }
118
 
118
 
119
    // line shifts
119
    // line shifts
120
    for(unsigned int p=0; p<nLineShifts; p++){
120
    for(unsigned int p=0; p<nLineShifts; p++){
121
        cv::Mat pattern(1, screenCols, CV_8UC3, cv::Vec3b(0, 0, 0));
121
        cv::Mat pattern(1, screenCols, CV_8UC3, cv::Scalar(0));
122
 
122
 
123
        for(unsigned int j=p; j<screenCols; j+= nLineShifts)
123
        for(unsigned int j=p; j<screenCols; j+= nLineShifts)
124
            pattern.at<cv::Vec3b>(0, j) = cv::Vec3b(255, 255, 255);
124
            pattern.at<cv::Vec3b>(0, j) = cv::Vec3b(255, 255, 255);
125
 
125
 
126
        patterns.push_back(pattern);
126
        patterns.push_back(pattern);
127
    }
127
    }
128
 
128
 
129
}
129
}
130
 
130
 
131
cv::Mat AlgorithmLineShift::getEncodingPattern(unsigned int depth){
131
cv::Mat AlgorithmLineShift::getEncodingPattern(unsigned int depth){
132
    return patterns[depth];
132
    return patterns[depth];
133
}
133
}
134
 
134
 
135
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
135
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
136
    assert(!img.empty());
136
    assert(!img.empty());
137
    assert(img.channels() == 3);
137
    assert(img.channels() == 3);
138
 
138
 
139
    int x = (int)pt.x;
139
    int x = (int)pt.x;
140
    int y = (int)pt.y;
140
    int y = (int)pt.y;
141
 
141
 
142
    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
142
    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
143
    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
143
    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
144
    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
144
    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
145
    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
145
    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
146
 
146
 
147
    float a = pt.x - (float)x;
147
    float a = pt.x - (float)x;
148
    float c = pt.y - (float)y;
148
    float c = pt.y - (float)y;
149
 
149
 
150
    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)
150
    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)
151
                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
151
                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
152
    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)
152
    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)
153
                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
153
                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
154
    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)
154
    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)
155
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
155
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
156
 
156
 
157
    return cv::Vec3b(b, g, r);
157
    return cv::Vec3b(b, g, r);
158
}
158
}
159
 
159
 
160
void getlineCenters(const cv::Mat& linesScanLine, const cv::Mat& codeScanLine, std::vector<cv::Vec4i>& lineCenters){
160
void getlineCenters(const cv::Mat& linesScanLine, const cv::Mat& codeScanLine, std::vector<cv::Vec2f>& lineCenters){
161
 
161
 
162
    int nCols = linesScanLine.cols;
162
    int nCols = linesScanLine.cols;
163
 
163
 
164
    // fourth order derivative filtering
164
    // fourth order derivative filtering
165
    cv::Mat der(1, nCols, CV_8U);
165
    cv::Mat der(1, nCols, CV_8U);
166
    for(int i=2; i<nCols-2; i++)
166
    for(int i=2; i<nCols-2; i++)
167
        der.at<unsigned char>(0, i) = linesScanLine.at<unsigned char>(0, i-2)+linesScanLine.at<unsigned char>(0, i-1)-
167
        der.at<unsigned char>(0, i) = linesScanLine.at<unsigned char>(0, i-2)+linesScanLine.at<unsigned char>(0, i-1)-
168
                                      linesScanLine.at<unsigned char>(0, i+1)-linesScanLine.at<unsigned char>(0, i+2);
168
                                      linesScanLine.at<unsigned char>(0, i+1)-linesScanLine.at<unsigned char>(0, i+2);
169
 
169
 
170
    for(int i=0; i<nCols; i++){
170
    for(int i=0; i<nCols; i++){
171
 
171
 
172
        if(der.at<unsigned char>(0, i) > 0 && der.at<unsigned char>(0, i+1) < 0){
172
        if(der.at<unsigned char>(0, i) > 0 && der.at<unsigned char>(0, i+1) < 0){
173
 
173
 
174
            lineCenters.push_back(i, codeScanLine.at<unsigned char>(0, i));
174
            lineCenters.push_back(cv::Vec2f(i, codeScanLine.at<unsigned char>(0, i)));
175
            // TODO: subpixel interpolation, non-max suppression
175
            // TODO: subpixel interpolation, non-max suppression
176
        }
176
        }
177
 
177
 
178
    }
178
    }
179
 
179
 
180
}
180
}
181
 
181
 
182
void AlgorithmLineShift::get3DPoints(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
182
void AlgorithmLineShift::get3DPoints(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
183
 
183
 
184
    assert(frames0.size() == N);
184
    assert(frames0.size() == N);
185
    assert(frames1.size() == N);
185
    assert(frames1.size() == N);
186
 
186
 
187
    int frameRows = frames0[0].rows;
187
    int frameRows = frames0[0].rows;
188
    int frameCols = frames0[0].cols;
188
    int frameCols = frames0[0].cols;
189
 
189
 
190
    // rectifying homographies (rotation+projections)
190
    // rectifying homographies (rotation+projections)
191
    cv::Size frameSize(frameCols, frameRows);
191
    cv::Size frameSize(frameCols, frameRows);
192
    cv::Mat R, T;
192
    cv::Mat R, T;
193
    // stereoRectify segfaults unless R is double precision
193
    // stereoRectify segfaults unless R is double precision
194
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
194
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
195
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
195
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
196
    cv::Mat R0, R1, P0, P1, QRect;
196
    cv::Mat R0, R1, P0, P1, QRect;
197
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
197
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
198
 
198
 
199
    // interpolation maps
199
    // interpolation maps
200
    cv::Mat map0X, map0Y, map1X, map1Y;
200
    cv::Mat map0X, map0Y, map1X, map1Y;
201
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
201
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
202
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
202
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
203
 
203
 
204
    // gray-scale and remap
204
    // gray-scale and remap
205
    std::vector<cv::Mat> frames0Rect(N);
205
    std::vector<cv::Mat> frames0Rect(N);
206
    std::vector<cv::Mat> frames1Rect(N);
206
    std::vector<cv::Mat> frames1Rect(N);
207
    for(int i=0; i<N; i++){
207
    for(int i=0; i<N; i++){
208
        cv::Mat temp;
208
        cv::Mat temp;
209
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
209
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
210
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
210
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
211
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
211
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
212
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
212
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
213
    }
213
    }
214
 
214
 
215
    //cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
215
    //cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
216
    //cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
216
    //cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
217
    //cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
217
    //cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
218
    //cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
218
    //cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
219
 
219
 
220
    // color debayer and remap
220
    // color debayer and remap
221
    cv::Mat color0Rect, color1Rect;
221
    cv::Mat color0Rect, color1Rect;
222
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
222
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
223
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
223
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
224
 
224
 
225
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
225
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
226
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
226
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
227
 
227
 
228
    int frameRectRows = frames0Rect[0].rows;
228
    int frameRectRows = frames0Rect[0].rows;
229
    int frameRectCols = frames0Rect[0].cols;
229
    int frameRectCols = frames0Rect[0].cols;
230
 
230
 
231
    // occlusion masks
231
    // occlusion masks
232
    cv::Mat occlusion0Rect, occlusion1Rect;
232
    cv::Mat occlusion0Rect, occlusion1Rect;
233
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
233
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
234
    occlusion0Rect = (occlusion0Rect > 20) & (occlusion0Rect < 250);
234
    occlusion0Rect = (occlusion0Rect > 20) & (occlusion0Rect < 250);
235
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
235
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
236
    occlusion1Rect = (occlusion1Rect > 20) & (occlusion1Rect < 250);
236
    occlusion1Rect = (occlusion1Rect > 20) & (occlusion1Rect < 250);
237
 
237
 
238
    //cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
238
    //cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
239
    //cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
239
    //cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
240
 
240
 
241
    // erode occlusion masks
241
    // erode occlusion masks
242
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
242
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
243
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
243
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
244
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
244
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
245
 
245
 
246
    // correct for texture modulation and ambient
246
    // correct for texture modulation and ambient
247
    cv::Mat A0 = frames0Rect[1];
247
    cv::Mat A0 = frames0Rect[1];
248
    cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
248
    cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
249
 
249
 
250
    cv::divide(256.0, M0, M0, CV_32F);
250
    cv::divide(256.0, M0, M0, CV_32F);
251
    cv::Mat A1 = frames1Rect[1];
251
    cv::Mat A1 = frames1Rect[1];
252
    cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
252
    cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
253
    cv::divide(256.0, M1, M1, CV_32F);
253
    cv::divide(256.0, M1, M1, CV_32F);
254
 
254
 
255
    for(int i=2; i<N; i++){
255
    for(int i=2; i<N; i++){
256
        cv::multiply(frames0Rect[i]-A0, M0, frames0Rect[i], 1.0, CV_8UC1);
256
        cv::multiply(frames0Rect[i]-A0, M0, frames0Rect[i], 1.0, CV_8UC1);
257
        cv::multiply(frames1Rect[i]-A1, M1, frames1Rect[i], 1.0, CV_8UC1);
257
        cv::multiply(frames1Rect[i]-A1, M1, frames1Rect[i], 1.0, CV_8UC1);
258
    }
258
    }
259
 
259
 
260
    //cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
260
    //cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
261
    //cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
261
    //cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
262
 
262
 
263
    // divide into Gray coding frames and line shift frames
263
    // divide into Gray coding frames and line shift frames
264
    std::vector<cv::Mat> frames0GrayCode(frames0Rect.begin()+2, frames0Rect.begin()+2+nGrayBits);
264
    std::vector<cv::Mat> frames0GrayCode(frames0Rect.begin()+2, frames0Rect.begin()+2+nGrayBits);
265
    std::vector<cv::Mat> frames0LineShift(frames0Rect.begin()+2+nGrayBits, frames0Rect.end());
265
    std::vector<cv::Mat> frames0LineShift(frames0Rect.begin()+2+nGrayBits, frames0Rect.end());
266
    std::vector<cv::Mat> frames1GrayCode(frames1Rect.begin()+2, frames1Rect.begin()+2+nGrayBits);
266
    std::vector<cv::Mat> frames1GrayCode(frames1Rect.begin()+2, frames1Rect.begin()+2+nGrayBits);
267
    std::vector<cv::Mat> frames1LineShift(frames1Rect.begin()+2+nGrayBits, frames1Rect.end());
267
    std::vector<cv::Mat> frames1LineShift(frames1Rect.begin()+2+nGrayBits, frames1Rect.end());
268
 
268
 
269
    // decode patterns
269
    // decode patterns
270
    cv::Mat code0Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
270
    cv::Mat code0Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
271
    cv::Mat code1Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
271
    cv::Mat code1Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
272
 
272
 
273
    // into gray code
273
    // into gray code
274
    for(int i=0; i<nGrayBits; i++){
274
    for(int i=0; i<nGrayBits; i++){
275
        cv::Mat temp, bit0, bit1;
275
        cv::Mat temp, bit0, bit1;
276
 
276
 
277
        cv::compare(frames0GrayCode[i*2], frames0GrayCode[i*2+1], temp, cv::CMP_GT);
277
        cv::compare(frames0GrayCode[i*2], frames0GrayCode[i*2+1], temp, cv::CMP_GT);
278
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
278
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
279
        cv::add(code0Gray, bit0*twopowi(Nbits-i-1), code0Gray, cv::noArray(), CV_32S);
279
        cv::add(code0Gray, bit0*twopowi(nGrayBits-i-1), code0Gray, cv::noArray(), CV_32S);
280
 
280
 
281
        cv::compare(frames1GrayCode[i*2], frames1GrayCode[i*2+1], temp, cv::CMP_GT);
281
        cv::compare(frames1GrayCode[i*2], frames1GrayCode[i*2+1], temp, cv::CMP_GT);
282
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
282
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
283
        cv::add(code1Gray, bit1*twopowi(Nbits-i-1), code1Gray, cv::noArray(), CV_32S);
283
        cv::add(code1Gray, bit1*twopowi(nGrayBits-i-1), code1Gray, cv::noArray(), CV_32S);
284
    }
284
    }
285
 
285
 
286
    // set occluded pixels to -1
286
    // set occluded pixels to -1
287
    for(int r=0; r<frameRectRows; r++){
287
    for(int r=0; r<frameRectRows; r++){
288
        for(int c=0; c<frameRectCols; c++){
288
        for(int c=0; c<frameRectCols; c++){
289
            if(occlusion0Rect.at<unsigned char>(r,c) == 0)
289
            if(occlusion0Rect.at<unsigned char>(r,c) == 0)
290
                code0Gray.at<int>(r,c) = -1;
290
                code0Gray.at<int>(r,c) = -1;
291
            if(occlusion1Rect.at<unsigned char>(r,c) == 0)
291
            if(occlusion1Rect.at<unsigned char>(r,c) == 0)
292
                code1Gray.at<int>(r,c) = -1;
292
                code1Gray.at<int>(r,c) = -1;
293
        }
293
        }
294
    }
294
    }
295
 
295
 
296
    // convert to standard binary
296
    // convert to standard binary
297
    cv::Mat code0Binary(code0Gray.rows, code0Gray.cols, CV_32F);
297
    cv::Mat code0Binary(code0Gray.rows, code0Gray.cols, CV_32F);
298
    cv::Mat code1Binary(code1Gray.rows, code1Gray.cols, CV_32F);
298
    cv::Mat code1Binary(code1Gray.rows, code1Gray.cols, CV_32F);
299
    for(int r=0; r<frameRectRows; r++){
299
    for(int r=0; r<frameRectRows; r++){
300
        for(int c=0; c<frameRectCols; c++){
300
        for(int c=0; c<frameRectCols; c++){
301
            if(code0Gray.at<int>(r,c) != -1)
301
            if(code0Gray.at<int>(r,c) != -1)
302
                code0Binary.at<float>(r,c) = grayToBinary(code0Gray.at<int>(r,c));
302
                code0Binary.at<float>(r,c) = grayToBinary(code0Gray.at<int>(r,c));
303
            if(code1Gray.at<int>(r,c) != -1)
303
            if(code1Gray.at<int>(r,c) != -1)
304
                code1Binary.at<float>(r,c) = grayToBinary(code1Gray.at<int>(r,c));
304
                code1Binary.at<float>(r,c) = grayToBinary(code1Gray.at<int>(r,c));
305
        }
305
        }
306
    }
306
    }
307
 
307
 
308
    //cvtools::writeMat(code0Gray, "code0Gray.mat", "code0Gray");
308
    //cvtools::writeMat(code0Gray, "code0Gray.mat", "code0Gray");
309
    //cvtools::writeMat(code1Gray, "code1Gray.mat", "code1Gray");
309
    //cvtools::writeMat(code1Gray, "code1Gray.mat", "code1Gray");
310
 
310
 
311
    // TODO: iterate through all line frames
311
    // TODO: iterate through all line frames
312
    cv::Mat lines0 = frames0LineShift[0];
312
    cv::Mat lines0 = frames0LineShift[0];
313
    cv::Mat lines1 = frames1LineShift[0];
313
    cv::Mat lines1 = frames1LineShift[0];
314
 
314
 
315
    // matching
315
    // matching
316
    std::vector<cv::Vec2f> q0Rect, q1Rect;
316
    std::vector<cv::Vec2f> q0Rect, q1Rect;
317
    for(int row=0; row<frameRectRows; row++){
317
    for(int row=0; row<frameRectRows; row++){
318
 
318
 
319
        // line center data structure containing [x-coordinate (sub-px), region-code]
319
        // line center data structure containing [x-coordinate (sub-px), region-code]
320
        std::vector<cv::Vec2f> lineCenters0, lineCenters1;
320
        std::vector<cv::Vec2f> lineCenters0, lineCenters1;
321
 
321
 
322
        // sorted, unique line centers
322
        // sorted, unique line centers
323
        getlineCenters(lines0.row(row), code0Binary.row(row), lineCenters0);
323
        getlineCenters(lines0.row(row), code0Binary.row(row), lineCenters0);
324
        getlineCenters(lines1.row(row), code1Binary.row(row), lineCenters1);
324
        getlineCenters(lines1.row(row), code1Binary.row(row), lineCenters1);
325
 
325
 
326
        // match and store
326
        // match and store
327
        int i=0, j=0;
327
        int i=0, j=0;
328
        while(i<lineCenters0.size() && j<lineCenters1.size()){
328
        while(i<lineCenters0.size() && j<lineCenters1.size()){
329
 
329
 
330
            if(lineCenters0[i][1] == lineCenters1[j][1]){
330
            if(lineCenters0[i][1] == lineCenters1[j][1]){
331
                q0Rect.push_back(cv::Point2f(lineCenters0[0][0], row));
331
                q0Rect.push_back(cv::Point2f(lineCenters0[0][0], row));
332
                q1Rect.push_back(cv::Point2f(lineCenters1[0][0], row));
332
                q1Rect.push_back(cv::Point2f(lineCenters1[0][0], row));
333
                i += 1;
333
                i += 1;
334
                j += 1;
334
                j += 1;
335
            } else if(lineCenters0[i][1] < lineCenters1[j][1]){
335
            } else if(lineCenters0[i][1] < lineCenters1[j][1]){
336
                i += 1;
336
                i += 1;
337
            } else if(lineCenters0[i][1] > lineCenters1[j][1]){
337
            } else if(lineCenters0[i][1] > lineCenters1[j][1]){
338
                j += 1;
338
                j += 1;
339
            }
339
            }
340
        }
340
        }
341
 
341
 
342
    }
342
    }
343
 
343
 
344
    int nMatches = q0Rect.size();
344
    int nMatches = q0Rect.size();
345
 
345
 
346
    if(nMatches < 1){
346
    if(nMatches < 1){
347
        Q.resize(0);
347
        Q.resize(0);
348
        color.resize(0);
348
        color.resize(0);
349
 
349
 
350
        return;
350
        return;
351
    }
351
    }
352
 
352
 
353
    // retrieve color information (at integer coordinates)
353
    // retrieve color information (at integer coordinates)
354
    color.resize(nMatches);
354
    color.resize(nMatches);
355
    for(int i=0; i<nMatches; i++){
355
    for(int i=0; i<nMatches; i++){
356
 
356
 
357
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
357
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
358
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
358
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
359
//        cv::Vec3b c0 = getColorSubpix(color0Rect, q0Rect[i]);
359
//        cv::Vec3b c0 = getColorSubpix(color0Rect, q0Rect[i]);
360
//        cv::Vec3b c1 = getColorSubpix(color1Rect, q0Rect[i]);
360
//        cv::Vec3b c1 = getColorSubpix(color1Rect, q0Rect[i]);
361
 
361
 
362
        color[i] = 0.5*c0 + 0.5*c1;
362
        color[i] = 0.5*c0 + 0.5*c1;
363
    }
363
    }
364
 
364
 
365
    // triangulate points
365
    // triangulate points
366
    cv::Mat QMatHomogenous, QMat;
366
    cv::Mat QMatHomogenous, QMat;
367
 
367
 
368
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
368
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
369
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
369
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
370
 
370
 
371
    // undo rectifying rotation
371
    // undo rectifying rotation
372
    cv::Mat R0Inv;
372
    cv::Mat R0Inv;
373
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
373
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
374
    QMat = R0Inv*QMat;
374
    QMat = R0Inv*QMat;
375
 
375
 
376
    cvtools::matToPoints3f(QMat, Q);
376
    cvtools::matToPoints3f(QMat, Q);
377
 
377
 
378
}
378
}
379
 
379