Subversion Repositories seema-scanner

Rev

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

Rev 122 Rev 133
1
#include "AlgorithmGrayCode.h"
1
#include "AlgorithmGrayCode.h"
2
#include <cmath>
2
#include <cmath>
3
#include "cvtools.h"
3
#include "cvtools.h"
4
 
4
 
5
#ifndef log2f
5
#ifndef log2f
6
#define log2f(x) (log(x)/log(2.0))
6
#define log2f(x) (log(x)/log(2.0))
7
#endif
7
#endif
8
 
8
 
9
//using namespace std;
9
//using namespace std;
10
 
10
 
11
/*
11
/*
12
 * The purpose of this function is to convert an unsigned
12
 * The purpose of this function is to convert an unsigned
13
 * binary number to reflected binary Gray code.
13
 * binary number to reflected binary Gray code.
14
 *
14
 *
15
 * The operator >> is shift right. The operator ^ is exclusive or.
15
 * The operator >> is shift right. The operator ^ is exclusive or.
16
 * Source: http://en.wikipedia.org/wiki/Gray_code
16
 * Source: http://en.wikipedia.org/wiki/Gray_code
17
 */
17
 */
18
static unsigned int binaryToGray(unsigned int num) {
18
static unsigned int binaryToGray(unsigned int num) {
19
    return (num >> 1) ^ num;
19
    return (num >> 1) ^ num;
20
}
20
}
21
 
21
 
22
/*
22
/*
23
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
23
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
24
 * The purpose of this function is to convert a reflected binary
24
 * The purpose of this function is to convert a reflected binary
25
 * Gray code number to a binary number.
25
 * Gray code number to a binary number.
26
 */
26
 */
27
static unsigned int grayToBinary(unsigned int num){
27
static unsigned int grayToBinary(unsigned int num){
28
    unsigned int mask;
28
    unsigned int mask;
29
    for(mask = num >> 1; mask != 0; mask = mask >> 1)
29
    for(mask = num >> 1; mask != 0; mask = mask >> 1)
30
        num = num ^ mask;
30
        num = num ^ mask;
31
    return num;
31
    return num;
32
}
32
}
33
 
33
 
34
/*
34
/*
35
 * Return the Nth bit of an unsigned integer number
35
 * Return the Nth bit of an unsigned integer number
36
 */
36
 */
37
static bool getBit(int decimal, int N){
37
static bool getBit(int decimal, int N){
38
 
38
 
39
    return decimal & 1 << (N-1);
39
    return decimal & 1 << (N-1);
40
}
40
}
41
 
41
 
42
/*
42
/*
43
 * Return the number of bits set in an integer
43
 * Return the number of bits set in an integer
44
 */
44
 */
45
static int countBits(int n) {
45
static int countBits(int n) {
46
  unsigned int c; // c accumulates the total bits set in v
46
  unsigned int c; // c accumulates the total bits set in v
47
  for (c = 0; n>0; c++)
47
  for (c = 0; n>0; c++)
48
    n &= n - 1; // clear the least significant bit set
48
    n &= n - 1; // clear the least significant bit set
49
  return c;
49
  return c;
50
}
50
}
51
 
51
 
52
/*
52
/*
53
 * Return the position of the least significant bit that is set
53
 * Return the position of the least significant bit that is set
54
 */
54
 */
55
static int leastSignificantBitSet(int x){
55
static int leastSignificantBitSet(int x){
56
  if(x == 0)
56
  if(x == 0)
57
      return 0;
57
      return 0;
58
 
58
 
59
  int val = 1;
59
  int val = 1;
60
  while(x>>=1)
60
  while(x>>=1)
61
      val++;
61
      val++;
62
 
62
 
63
  return val;
63
  return val;
64
}
64
}
65
 
65
 
66
//static int get_bit(int decimal, int N){
66
//static int get_bit(int decimal, int N){
67
 
67
 
68
//    // Shifting the 1 for N-1 bits
68
//    // Shifting the 1 for N-1 bits
69
//    int constant = 1 << (N-1);
69
//    int constant = 1 << (N-1);
70
 
70
 
71
//    // If the bit is set, return 1
71
//    // If the bit is set, return 1
72
//    if( decimal & constant )
72
//    if( decimal & constant )
73
//        return 1;
73
//        return 1;
74
//    else
74
//    else
75
//        return 0;
75
//        return 0;
76
//}
76
//}
77
 
77
 
78
static inline unsigned int powi(int num, unsigned int exponent){
78
static inline unsigned int powi(int num, unsigned int exponent){
79
 
79
 
80
    if(exponent == 0)
80
    if(exponent == 0)
81
        return 1;
81
        return 1;
82
 
82
 
83
    float res = num;
83
    float res = num;
84
    for(unsigned int i=0; i<exponent-1; i++)
84
    for(unsigned int i=0; i<exponent-1; i++)
85
        res *= num;
85
        res *= num;
86
 
86
 
87
    return res;
87
    return res;
88
}
88
}
89
 
89
 
90
static inline unsigned int twopowi(unsigned int exponent){
90
static inline unsigned int twopowi(unsigned int exponent){
91
 
91
 
92
    return 1 << exponent;
92
    return 1 << exponent;
93
}
93
}
94
 
94
 
95
// Algorithm
95
// Algorithm
96
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
96
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
97
 
97
 
98
    Nbits = ceilf(log2f((float)screenCols));
98
    Nbits = ceilf(log2f((float)screenCols)) - 1;
99
    N = 2 + Nbits*2;
99
    N = 2 + Nbits*2;
100
 
100
 
101
    // all on pattern
101
    // all on pattern
102
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
102
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
103
    patterns.push_back(allOn);
103
    patterns.push_back(allOn);
104
 
104
 
105
    // all off pattern
105
    // all off pattern
106
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
106
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
107
    patterns.push_back(allOff);
107
    patterns.push_back(allOff);
108
 
108
 
109
 
109
 
110
    // horizontally encoding patterns
110
    // horizontally encoding patterns
111
    for(unsigned int p=0; p<Nbits; p++){
111
    for(unsigned int p=0; p<Nbits; p++){
112
        cv::Mat pattern(1, screenCols, CV_8UC3);
112
        cv::Mat pattern(1, screenCols, CV_8UC3);
113
        cv::Mat patternInv(1, screenCols, CV_8UC3);
113
        cv::Mat patternInv(1, screenCols, CV_8UC3);
114
 
114
 
115
        for(unsigned int j=0; j<screenCols; j++){
115
        for(unsigned int j=0; j<screenCols; j++){
116
 
116
 
117
            unsigned int jGray = binaryToGray(j);
117
            unsigned int jGray = binaryToGray(j);
118
            // Amplitude of channels
118
            // Amplitude of channels
119
            int bit = (int)getBit(jGray, Nbits-p);
119
            int bit = (int)getBit(jGray, Nbits-p+1);
120
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
120
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
121
            int invBit = bit^1;
121
            int invBit = bit^1;
122
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
122
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
123
        }
123
        }
124
        patterns.push_back(pattern);
124
        patterns.push_back(pattern);
125
        patterns.push_back(patternInv);
125
        patterns.push_back(patternInv);
126
    }
126
    }
127
 
127
 
128
 
128
 
129
}
129
}
130
 
130
 
131
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
131
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
132
    return patterns[depth];
132
    return patterns[depth];
133
}
133
}
134
 
134
 
135
 
135
 
136
bool sortingLarger(cv::Vec4i i,cv::Vec4i j){ return (i[3]<j[3]);}
136
bool sortingLarger(cv::Vec4i i,cv::Vec4i j){ return (i[3]<j[3]);}
137
bool sortingEqual(cv::Vec4i i,cv::Vec4i j){ return (i[3]==j[3]);}
137
bool sortingEqual(cv::Vec4i i,cv::Vec4i j){ return (i[3]==j[3]);}
138
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4i>& edges){
138
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4i>& edges){
139
 
139
 
140
    int nCols = scanLine.cols;
140
    int nCols = scanLine.cols;
141
    const int *data = scanLine.ptr<const int>(0);
141
    const int *data = scanLine.ptr<const int>(0);
142
 
142
 
143
    int labelLeft;
143
    int labelLeft;
144
    int labelRight = data[0];
144
    int labelRight = data[0];
145
 
145
 
146
    // collect edges
146
    // collect edges
147
    for(int col=1; col<nCols; col++){
147
    for(int col=1; col<nCols; col++){
148
 
148
 
149
        labelLeft = labelRight;
149
        labelLeft = labelRight;
150
        labelRight = data[col];
150
        labelRight = data[col];
151
 
151
 
152
        // labels need to be non-background, and differ in exactly one bit
152
        // labels need to be non-background, and differ in exactly one bit
153
        if(labelLeft != -1 && labelRight != -1 && (grayToBinary(labelRight) == grayToBinary(labelLeft)+1)){
153
        if(labelLeft != -1 && labelRight != -1 && (grayToBinary(labelRight) == grayToBinary(labelLeft)+1)){
154
            int orderingRelation = (labelLeft << Nbits) + labelRight;
154
            int orderingRelation = (labelLeft << Nbits) + labelRight;
155
            // store left label column
155
            // store left label column
156
            edges.push_back(cv::Vec4i(col-1, labelLeft, labelRight, orderingRelation));
156
            edges.push_back(cv::Vec4i(col-1, labelLeft, labelRight, orderingRelation));
157
        }
157
        }
158
    }
158
    }
159
 
159
 
160
    // sort
160
    // sort
161
    std::sort(edges.begin(), edges.end(), sortingLarger);
161
    std::sort(edges.begin(), edges.end(), sortingLarger);
162
 
162
 
163
    // remove duplicates
163
    // remove duplicates
164
    std::vector<cv::Vec4i>::iterator it;
164
    std::vector<cv::Vec4i>::iterator it;
165
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
165
    it = std::unique(edges.begin(), edges.end(), sortingEqual);
166
    edges.resize(std::distance(edges.begin(),it));
166
    edges.resize(std::distance(edges.begin(),it));
167
}
167
}
168
 
168
 
169
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
169
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
170
    assert(!img.empty());
170
    assert(!img.empty());
171
    assert(img.channels() == 3);
171
    assert(img.channels() == 3);
172
 
172
 
173
    int x = (int)pt.x;
173
    int x = (int)pt.x;
174
    int y = (int)pt.y;
174
    int y = (int)pt.y;
175
 
175
 
176
    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
176
    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
177
    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
177
    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
178
    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
178
    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
179
    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
179
    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
180
 
180
 
181
    float a = pt.x - (float)x;
181
    float a = pt.x - (float)x;
182
    float c = pt.y - (float)y;
182
    float c = pt.y - (float)y;
183
 
183
 
184
    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)
184
    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)
185
                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
185
                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
186
    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)
186
    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)
187
                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
187
                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
188
    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)
188
    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)
189
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
189
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
190
 
190
 
191
    return cv::Vec3b(b, g, r);
191
    return cv::Vec3b(b, g, r);
192
}
192
}
193
 
193
 
194
void AlgorithmGrayCode::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){
194
void AlgorithmGrayCode::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){
195
 
195
 
196
    assert(frames0.size() == N);
196
    assert(frames0.size() == N);
197
    assert(frames1.size() == N);
197
    assert(frames1.size() == N);
198
 
198
 
199
//    for(int i=0; i<1920; i++){
199
//    for(int i=0; i<1920; i++){
200
//        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
200
//        std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
201
//    }
201
//    }
202
 
202
 
203
    int frameRows = frames0[0].rows;
203
    int frameRows = frames0[0].rows;
204
    int frameCols = frames0[0].cols;
204
    int frameCols = frames0[0].cols;
205
 
205
 
206
    // rectifying homographies (rotation+projections)
206
    // rectifying homographies (rotation+projections)
207
    cv::Size frameSize(frameCols, frameRows);
207
    cv::Size frameSize(frameCols, frameRows);
208
    cv::Mat R, T;
208
    cv::Mat R, T;
209
    // stereoRectify segfaults unless R is double precision
209
    // stereoRectify segfaults unless R is double precision
210
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
210
    cv::Mat(calibration.R1).convertTo(R, CV_64F);
211
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
211
    cv::Mat(calibration.T1).convertTo(T, CV_64F);
212
    cv::Mat R0, R1, P0, P1, QRect;
212
    cv::Mat R0, R1, P0, P1, QRect;
213
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
213
    cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
214
 
214
 
215
//    std::cout << "R0" << std::endl << R0 << std::endl;
215
//    std::cout << "R0" << std::endl << R0 << std::endl;
216
//    std::cout << "P0" << std::endl << P0 << std::endl;
216
//    std::cout << "P0" << std::endl << P0 << std::endl;
217
//    std::cout << "R1" << std::endl << R1 << std::endl;
217
//    std::cout << "R1" << std::endl << R1 << std::endl;
218
//    std::cout << "P1" << std::endl << P1 << std::endl;
218
//    std::cout << "P1" << std::endl << P1 << std::endl;
219
 
219
 
220
    // interpolation maps
220
    // interpolation maps
221
    cv::Mat map0X, map0Y, map1X, map1Y;
221
    cv::Mat map0X, map0Y, map1X, map1Y;
222
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
222
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
223
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
223
    cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
224
 
224
 
225
    // gray-scale and remap
225
    // gray-scale and remap
226
    std::vector<cv::Mat> frames0Rect(N);
226
    std::vector<cv::Mat> frames0Rect(N);
227
    std::vector<cv::Mat> frames1Rect(N);
227
    std::vector<cv::Mat> frames1Rect(N);
228
    for(int i=0; i<N; i++){
228
    for(int i=0; i<N; i++){
229
        cv::Mat temp;
229
        cv::Mat temp;
230
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
230
        cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
231
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
231
        cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
232
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
232
        cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
233
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
233
        cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
234
    }
234
    }
235
 
235
 
236
 
236
 
237
//    cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
237
//    cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
238
//    cvtools::writeMat(frames0[0], "frames0_0.mat", "frames0_0");
238
//    cvtools::writeMat(frames0[0], "frames0_0.mat", "frames0_0");
239
 
239
 
240
//    cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
240
//    cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
241
//    cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
241
//    cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
242
 
242
 
243
//    cv::imwrite("frames0[0].png", frames0[0]);
243
//    cv::imwrite("frames0[0].png", frames0[0]);
244
//    cv::imwrite("frames0Rect[0].png", frames0Rect[0]);
244
//    cv::imwrite("frames0Rect[0].png", frames0Rect[0]);
245
 
245
 
246
//    cv::imwrite("frames1[0].png", frames1[0]);
246
//    cv::imwrite("frames1[0].png", frames1[0]);
247
//    cv::imwrite("frames1Rect[0].png", frames1Rect[0]);
247
//    cv::imwrite("frames1Rect[0].png", frames1Rect[0]);
248
 
248
 
249
    // color debayer and remap
249
    // color debayer and remap
250
    cv::Mat color0Rect, color1Rect;
250
    cv::Mat color0Rect, color1Rect;
251
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
251
//    frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
252
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
252
    cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
253
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
253
    cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
254
 
254
 
255
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
255
//    frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
256
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
256
    cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
257
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
257
    cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
258
 
258
 
259
    int frameRectRows = frames0Rect[0].rows;
259
    int frameRectRows = frames0Rect[0].rows;
260
    int frameRectCols = frames0Rect[0].cols;
260
    int frameRectCols = frames0Rect[0].cols;
261
 
261
 
262
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
262
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
263
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
263
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
264
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
264
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
265
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
265
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
266
 
266
 
267
    // occlusion masks
267
    // occlusion masks
268
    cv::Mat occlusion0Rect, occlusion1Rect;
268
    cv::Mat occlusion0Rect, occlusion1Rect;
269
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
269
    cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
270
    occlusion0Rect = (occlusion0Rect > 20) & (occlusion0Rect < 250);
270
    occlusion0Rect = (occlusion0Rect > 10) & (occlusion0Rect < 250);
271
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
271
    cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
272
    occlusion1Rect = (occlusion1Rect > 20) & (occlusion1Rect < 250);
272
    occlusion1Rect = (occlusion1Rect > 10) & (occlusion1Rect < 250);
273
 
273
 
274
    // erode occlusion masks
274
    // erode occlusion masks
275
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
275
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
276
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
276
    cv::erode(occlusion0Rect, occlusion0Rect, strel);
277
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
277
    cv::erode(occlusion1Rect, occlusion1Rect, strel);
278
 
278
 
279
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
279
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
280
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
280
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
281
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
281
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
282
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
282
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
283
 
283
 
284
//    // correct for projector inversion error
284
//    // correct for projector inversion error
285
//    cv::Mat W;
285
//    cv::Mat W;
286
//    cv::add(frames0Rect[0], frames0Rect[1], W, cv::noArray(), CV_32F);
286
//    cv::add(frames0Rect[0], frames0Rect[1], W, cv::noArray(), CV_32F);
287
//    for(int i=2; i<N; i+=2){
287
//    for(int i=2; i<N; i+=2){
288
//        cv::Mat S, E;
288
//        cv::Mat S, E;
289
//        cv::add(frames0Rect[i], frames0Rect[i+1], S, cv::noArray(), CV_32F);
289
//        cv::add(frames0Rect[i], frames0Rect[i+1], S, cv::noArray(), CV_32F);
290
//        cv::subtract(W, S, E, cv::noArray(), CV_32F);
290
//        cv::subtract(W, S, E, cv::noArray(), CV_32F);
291
//        E *= 0.5;
291
//        E *= 0.5;
292
//        cv::add(frames0Rect[i], E, frames0Rect[i], cv::noArray(), CV_16UC1);
292
//        cv::add(frames0Rect[i], E, frames0Rect[i], cv::noArray(), CV_16UC1);
293
//        cv::add(frames0Rect[i+1], E, frames0Rect[i+1], cv::noArray(), CV_16UC1);
293
//        cv::add(frames0Rect[i+1], E, frames0Rect[i+1], cv::noArray(), CV_16UC1);
294
//    }
294
//    }
295
 
295
 
296
 
296
 
297
    // correct for texture modulation and ambient
297
//    // correct for texture modulation and ambient
298
    cv::Mat A0 = frames0Rect[1];
298
//    cv::Mat A0 = frames0Rect[1];
299
    cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
299
//    cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
300
//cvtools::writeMat(A0, "A0.mat", "A0");
300
////cvtools::writeMat(A0, "A0.mat", "A0");
301
//cvtools::writeMat(M0, "M0.mat", "M0");
301
////cvtools::writeMat(M0, "M0.mat", "M0");
302
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
302
////cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
303
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
303
////cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
304
    cv::divide(256.0, M0, M0, CV_32F);
304
//    cv::divide(256.0, M0, M0, CV_32F);
305
    cv::Mat A1 = frames1Rect[1];
305
//    cv::Mat A1 = frames1Rect[1];
306
    cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
306
//    cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
307
    cv::divide(256.0, M1, M1, CV_32F);
307
//    cv::divide(256.0, M1, M1, CV_32F);
308
 
308
 
309
    for(int i=2; i<N; i++){
309
//    for(int i=2; i<N; i++){
310
        cv::multiply(frames0Rect[i]-A0, M0, frames0Rect[i], 1.0, CV_8UC1);
310
//        cv::multiply(frames0Rect[i]-A0, M0, frames0Rect[i], 1.0, CV_8UC1);
311
        cv::multiply(frames1Rect[i]-A1, M1, frames1Rect[i], 1.0, CV_8UC1);
311
//        cv::multiply(frames1Rect[i]-A1, M1, frames1Rect[i], 1.0, CV_8UC1);
312
    }
312
//    }
313
 
313
 
314
//cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
314
//cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
315
//cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
315
//cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
316
 
316
 
317
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
317
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
318
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
318
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
319
 
319
 
320
    // decode patterns
320
    // decode patterns
321
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
321
    cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
322
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
322
    cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
323
 
323
 
324
    // into gray code
324
    // into gray code
325
    for(int i=0; i<Nbits; i++){
325
    for(int i=0; i<Nbits; i++){
326
        cv::Mat temp, bit0, bit1;
326
        cv::Mat temp, bit0, bit1;
327
 
327
 
328
        cv::compare(frames0Rect[i*2+2], frames0Rect[i*2+3], temp, cv::CMP_GT);
328
        cv::compare(frames0Rect[i*2+2], frames0Rect[i*2+3], temp, cv::CMP_GT);
329
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
329
        temp.convertTo(bit0, CV_32S, 1.0/255.0);
330
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, cv::noArray(), CV_32S);
330
        cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, cv::noArray(), CV_32S);
331
 
331
 
332
        cv::compare(frames1Rect[i*2+2], frames1Rect[i*2+3], temp, cv::CMP_GT);
332
        cv::compare(frames1Rect[i*2+2], frames1Rect[i*2+3], temp, cv::CMP_GT);
333
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
333
        temp.convertTo(bit1, CV_32S, 1.0/255.0);
334
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, cv::noArray(), CV_32S);
334
        cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, cv::noArray(), CV_32S);
335
    }
335
    }
336
 
336
 
337
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
337
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
338
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
338
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
339
 
339
 
340
 
340
 
341
//    // convert to standard binary
341
//    // convert to standard binary
342
//    cv::Mat code0Binary(code0Rect.rows, code0Rect.cols, CV_32F);
342
//    cv::Mat code0Binary(code0Rect.rows, code0Rect.cols, CV_32F);
343
//    cv::Mat code1Binary(code1Rect.rows, code1Rect.cols, CV_32F);
343
//    cv::Mat code1Binary(code1Rect.rows, code1Rect.cols, CV_32F);
344
//    for(int r=0; r<frameRectRows; r++){
344
//    for(int r=0; r<frameRectRows; r++){
345
//        for(int c=0; c<frameRectCols; c++){
345
//        for(int c=0; c<frameRectCols; c++){
346
//            if(code0Rect.at<int>(r,c) != -1)
346
//            if(code0Rect.at<int>(r,c) != -1)
347
//                code0Binary.at<float>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
347
//                code0Binary.at<float>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
348
//            if(code1Rect.at<int>(r,c) != -1)
348
//            if(code1Rect.at<int>(r,c) != -1)
349
//                code1Binary.at<float>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
349
//                code1Binary.at<float>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
350
//        }
350
//        }
351
//    }
351
//    }
352
 
352
 
353
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
353
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
354
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
354
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
355
 
355
 
356
//    // threshold on vertical discontinuities (due to imperfect rectification)
356
//    // threshold on vertical discontinuities (due to imperfect rectification)
357
//    cv::Mat edges0;
357
//    cv::Mat edges0;
358
//    cv::Sobel(code0Binary, edges0, -1, 0, 1, 5);
358
//    cv::Sobel(code0Binary, edges0, -1, 0, 1, 5);
359
//    occlusion0Rect = occlusion0Rect & (abs(edges0) < 50);
359
//    occlusion0Rect = occlusion0Rect & (abs(edges0) < 50);
360
 
360
 
361
//    cv::Mat edges1;
361
//    cv::Mat edges1;
362
//    cv::Sobel(code1Binary, edges1, -1, 0, 1, 5);
362
//    cv::Sobel(code1Binary, edges1, -1, 0, 1, 5);
363
//    occlusion1Rect = occlusion1Rect & (abs(edges1) < 50);
363
//    occlusion1Rect = occlusion1Rect & (abs(edges1) < 50);
364
 
364
 
365
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
365
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
366
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
366
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
367
 
367
 
368
    // set occluded pixels to -1
368
    // set occluded pixels to -1
369
    for(int r=0; r<frameRectRows; r++){
369
    for(int r=0; r<frameRectRows; r++){
370
        for(int c=0; c<frameRectCols; c++){
370
        for(int c=0; c<frameRectCols; c++){
371
            if(occlusion0Rect.at<unsigned char>(r,c) == 0)
371
            if(occlusion0Rect.at<unsigned char>(r,c) == 0)
372
                code0Rect.at<int>(r,c) = -1;
372
                code0Rect.at<int>(r,c) = -1;
373
            if(occlusion1Rect.at<unsigned char>(r,c) == 0)
373
            if(occlusion1Rect.at<unsigned char>(r,c) == 0)
374
                code1Rect.at<int>(r,c) = -1;
374
                code1Rect.at<int>(r,c) = -1;
375
        }
375
        }
376
    }
376
    }
377
 
377
 
378
//    cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
378
//    cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
379
//    cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
379
//    cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
380
 
380
 
381
    // matching
381
    // matching
382
    std::vector<cv::Vec2f> q0Rect, q1Rect;
382
    std::vector<cv::Vec2f> q0Rect, q1Rect;
383
    for(int row=0; row<frameRectRows; row++){
383
    for(int row=0; row<frameRectRows; row++){
384
 
384
 
385
        // edge data structure containing [floor(column), labelLeft, labelRight, orderingRelation]
385
        // edge data structure containing [floor(column), labelLeft, labelRight, orderingRelation]
386
        std::vector<cv::Vec4i> edges0, edges1;
386
        std::vector<cv::Vec4i> edges0, edges1;
387
 
387
 
388
        // sorted, unique edges
388
        // sorted, unique edges
389
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
389
        getEdgeLabels(code0Rect.row(row), Nbits, edges0);
390
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
390
        getEdgeLabels(code1Rect.row(row), Nbits, edges1);
391
 
391
 
392
        // match edges
392
        // match edges
393
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
393
        std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
394
        int i=0, j=0;
394
        int i=0, j=0;
395
        while(i<edges0.size() && j<edges1.size()){
395
        while(i<edges0.size() && j<edges1.size()){
396
 
396
 
397
            if(edges0[i][3] == edges1[j][3]){
397
            if(edges0[i][3] == edges1[j][3]){
398
                matchedEdges0.push_back(edges0[i]);
398
                matchedEdges0.push_back(edges0[i]);
399
                matchedEdges1.push_back(edges1[j]);
399
                matchedEdges1.push_back(edges1[j]);
400
                i += 1;
400
                i += 1;
401
                j += 1;
401
                j += 1;
402
            } else if(edges0[i][3] < edges1[j][3]){
402
            } else if(edges0[i][3] < edges1[j][3]){
403
                i += 1;
403
                i += 1;
404
            } else if(edges0[i][3] > edges1[j][3]){
404
            } else if(edges0[i][3] > edges1[j][3]){
405
                j += 1;
405
                j += 1;
406
            }
406
            }
407
        }
407
        }
408
 
408
 
409
        // crude subpixel refinement
409
        // crude subpixel refinement
410
        // finds the intersection of linear interpolants in the positive/negative pattern
410
        // finds the intersection of linear interpolants in the positive/negative pattern
411
        for(int i=0; i<matchedEdges0.size(); i++){
411
        for(int i=0; i<matchedEdges0.size(); i++){
412
 
412
 
413
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
413
            int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
414
 
414
 
415
            // refine for camera 0
415
            // refine for camera 0
416
            float c0 = matchedEdges0[i][0];
416
            float c0 = matchedEdges0[i][0];
417
            float c1 = c0+1;
417
            float c1 = c0+1;
418
 
418
 
419
            float pos0 = frames0Rect[2*level+2].at<unsigned char>(row, c0);
419
            float pos0 = frames0Rect[2*level+2].at<unsigned char>(row, c0);
420
            float pos1 = frames0Rect[2*level+2].at<unsigned char>(row, c1);
420
            float pos1 = frames0Rect[2*level+2].at<unsigned char>(row, c1);
421
            float neg0 = frames0Rect[2*level+3].at<unsigned char>(row, c0);
421
            float neg0 = frames0Rect[2*level+3].at<unsigned char>(row, c0);
422
            float neg1 = frames0Rect[2*level+3].at<unsigned char>(row, c1);
422
            float neg1 = frames0Rect[2*level+3].at<unsigned char>(row, c1);
423
 
423
 
424
            float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
424
            float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
425
            q0Rect.push_back(cv::Point2f(col, row));
425
            q0Rect.push_back(cv::Point2f(col, row));
426
 
426
 
427
            // refine for camera 1
427
            // refine for camera 1
428
            c0 = matchedEdges1[i][0];
428
            c0 = matchedEdges1[i][0];
429
            c1 = c0+1;
429
            c1 = c0+1;
430
 
430
 
431
            pos0 = frames1Rect[2*level+2].at<unsigned char>(row, c0);
431
            pos0 = frames1Rect[2*level+2].at<unsigned char>(row, c0);
432
            pos1 = frames1Rect[2*level+2].at<unsigned char>(row, c1);
432
            pos1 = frames1Rect[2*level+2].at<unsigned char>(row, c1);
433
            neg0 = frames1Rect[2*level+3].at<unsigned char>(row, c0);
433
            neg0 = frames1Rect[2*level+3].at<unsigned char>(row, c0);
434
            neg1 = frames1Rect[2*level+3].at<unsigned char>(row, c1);
434
            neg1 = frames1Rect[2*level+3].at<unsigned char>(row, c1);
435
 
435
 
436
            col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
436
            col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
437
            q1Rect.push_back(cv::Point2f(col, row));
437
            q1Rect.push_back(cv::Point2f(col, row));
438
 
438
 
439
        }
439
        }
440
 
440
 
441
    }
441
    }
442
 
442
 
443
    int nMatches = q0Rect.size();
443
    int nMatches = q0Rect.size();
444
 
444
 
445
    if(nMatches < 1){
445
    if(nMatches < 1){
446
        Q.resize(0);
446
        Q.resize(0);
447
        color.resize(0);
447
        color.resize(0);
448
 
448
 
449
        return;
449
        return;
450
    }
450
    }
451
 
451
 
452
    // retrieve color information (at integer coordinates)
452
    // retrieve color information (at integer coordinates)
453
    color.resize(nMatches);
453
    color.resize(nMatches);
454
    for(int i=0; i<nMatches; i++){
454
    for(int i=0; i<nMatches; i++){
455
 
455
 
456
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
456
        cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
457
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
457
        cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
458
//        cv::Vec3b c0 = getColorSubpix(color0Rect, q0Rect[i]);
458
//        cv::Vec3b c0 = getColorSubpix(color0Rect, q0Rect[i]);
459
//        cv::Vec3b c1 = getColorSubpix(color1Rect, q0Rect[i]);
459
//        cv::Vec3b c1 = getColorSubpix(color1Rect, q0Rect[i]);
460
 
460
 
461
        color[i] = 0.5*c0 + 0.5*c1;
461
        color[i] = 0.5*c0 + 0.5*c1;
462
    }
462
    }
463
 
463
 
464
    // triangulate points
464
    // triangulate points
465
    cv::Mat QMatHomogenous, QMat;
465
    cv::Mat QMatHomogenous, QMat;
466
//    cv::Mat C0 = P0.clone();
466
//    cv::Mat C0 = P0.clone();
467
//    cv::Mat C1 = P1.clone();
467
//    cv::Mat C1 = P1.clone();
468
//    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
468
//    C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
469
//    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
469
//    C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
470
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
470
    cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
471
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
471
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
472
 
472
 
473
    // undo rectifying rotation
473
    // undo rectifying rotation
474
    cv::Mat R0Inv;
474
    cv::Mat R0Inv;
475
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
475
    cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
476
    QMat = R0Inv*QMat;
476
    QMat = R0Inv*QMat;
477
 
477
 
478
    cvtools::matToPoints3f(QMat, Q);
478
    cvtools::matToPoints3f(QMat, Q);
479
 
479
 
480
}
480
}
481
 
481