Subversion Repositories seema-scanner

Rev

Rev 96 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 96 Rev 98
1
#include "AlgorithmGrayCodeHQ.h"
1
#include "AlgorithmGrayCodeHQ.h"
2
#include <cmath>
2
#include <cmath>
3
#include <assert.h>
3
#include <assert.h>
4
#include "cvtools.h"
4
#include "cvtools.h"
5
 
5
 
6
#ifndef log2f
6
#ifndef log2f
7
#define log2f(x) (log(x)/log(2.0))
7
#define log2f(x) (log(x)/log(2.0))
8
#endif
8
#endif
9
 
9
 
10
//using namespace std;
10
//using namespace std;
11
 
11
 
12
/*
12
/*
13
 * The purpose of this function is to convert an unsigned
13
 * The purpose of this function is to convert an unsigned
14
 * binary number to reflected binary Gray code.
14
 * binary number to reflected binary Gray code.
15
 *
15
 *
16
 * The operator >> is shift right. The operator ^ is exclusive or.
16
 * The operator >> is shift right. The operator ^ is exclusive or.
17
 * Source: http://en.wikipedia.org/wiki/Gray_code
17
 * Source: http://en.wikipedia.org/wiki/Gray_code
18
 */
18
 */
19
static unsigned int binaryToGray(unsigned int num) {
19
static unsigned int binaryToGray(unsigned int num) {
20
    return (num >> 1) ^ num;
20
    return (num >> 1) ^ num;
21
}
21
}
22
 
22
 
23
/*
23
/*
24
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
24
 * From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
25
 * The purpose of this function is to convert a reflected binary
25
 * The purpose of this function is to convert a reflected binary
26
 * Gray code number to a binary number.
26
 * Gray code number to a binary number.
27
 */
27
 */
28
static unsigned int grayToBinary(unsigned int num){
28
static unsigned int grayToBinary(unsigned int num){
29
    unsigned int mask;
29
    unsigned int mask;
30
    for(mask = num >> 1; mask != 0; mask = mask >> 1)
30
    for(mask = num >> 1; mask != 0; mask = mask >> 1)
31
        num = num ^ mask;
31
        num = num ^ mask;
32
    return num;
32
    return num;
33
}
33
}
34
 
34
 
35
/*
35
/*
36
 * Return the Nth bit of an unsigned integer number
36
 * Return the Nth bit of an unsigned integer number
37
 */
37
 */
38
static bool getBit(int decimal, int N){
38
static bool getBit(int decimal, int N){
39
 
39
 
40
    return decimal & 1 << (N-1);
40
    return decimal & 1 << (N-1);
41
}
41
}
42
 
42
 
43
/*
43
/*
44
 * Return the number of bits set in an integer
44
 * Return the number of bits set in an integer
45
 */
45
 */
46
static int countBits(int n) {
46
static int countBits(int n) {
47
  unsigned int c; // c accumulates the total bits set in v
47
  unsigned int c; // c accumulates the total bits set in v
48
  for (c = 0; n>0; c++)
48
  for (c = 0; n>0; c++)
49
    n &= n - 1; // clear the least significant bit set
49
    n &= n - 1; // clear the least significant bit set
50
  return c;
50
  return c;
51
}
51
}
52
 
52
 
53
/*
53
/*
54
 * Return the position of the least significant bit that is set
54
 * Return the position of the least significant bit that is set
55
 */
55
 */
56
static int leastSignificantBitSet(int x){
56
static int leastSignificantBitSet(int x){
57
  if(x == 0)
57
  if(x == 0)
58
      return 0;
58
      return 0;
59
 
59
 
60
  int val = 1;
60
  int val = 1;
61
  while(x>>=1)
61
  while(x>>=1)
62
      val++;
62
      val++;
63
 
63
 
64
  return val;
64
  return val;
65
}
65
}
66
 
66
 
67
//static int get_bit(int decimal, int N){
67
//static int get_bit(int decimal, int N){
68
 
68
 
69
//    // Shifting the 1 for N-1 bits
69
//    // Shifting the 1 for N-1 bits
70
//    int constant = 1 << (N-1);
70
//    int constant = 1 << (N-1);
71
 
71
 
72
//    // If the bit is set, return 1
72
//    // If the bit is set, return 1
73
//    if( decimal & constant )
73
//    if( decimal & constant )
74
//        return 1;
74
//        return 1;
75
//    else
75
//    else
76
//        return 0;
76
//        return 0;
77
//}
77
//}
78
 
78
 
79
static inline unsigned int powi(int num, unsigned int exponent){
79
static inline unsigned int powi(int num, unsigned int exponent){
80
 
80
 
81
    if(exponent == 0)
81
    if(exponent == 0)
82
        return 1;
82
        return 1;
83
 
83
 
84
    float res = num;
84
    float res = num;
85
    for(unsigned int i=0; i<exponent-1; i++)
85
    for(unsigned int i=0; i<exponent-1; i++)
86
        res *= num;
86
        res *= num;
87
 
87
 
88
    return res;
88
    return res;
89
}
89
}
90
 
90
 
91
static inline unsigned int twopowi(unsigned int exponent){
91
static inline unsigned int twopowi(unsigned int exponent){
92
 
92
 
93
    return 1 << exponent;
93
    return 1 << exponent;
94
}
94
}
95
 
95
 
96
// Algorithm
96
// Algorithm
97
AlgorithmGrayCodeHQ::AlgorithmGrayCodeHQ(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
97
AlgorithmGrayCodeHQ::AlgorithmGrayCodeHQ(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
98
 
98
 
99
    NbitsHorz = ceilf(log2f((float)screenCols));
99
    NbitsHorz = ceilf(log2f((float)screenCols));
100
    NbitsVert =  ceilf(log2f((float)screenRows));
100
    NbitsVert =  ceilf(log2f((float)screenRows));
101
    N = 2 + (NbitsHorz+NbitsVert)*2;
101
    N = 2 + (NbitsHorz+NbitsVert)*2;
102
 
102
 
103
    // all on pattern
103
    // all on pattern
104
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
104
    cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
105
    patterns.push_back(allOn);
105
    patterns.push_back(allOn);
106
 
106
 
107
    // all off pattern
107
    // all off pattern
108
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
108
    cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
109
    patterns.push_back(allOff);
109
    patterns.push_back(allOff);
110
 
110
 
111
 
111
 
112
    // horizontally encoding patterns
112
    // horizontally encoding patterns
113
    for(unsigned int p=0; p<NbitsHorz; p++){
113
    for(unsigned int p=0; p<NbitsHorz; p++){
114
        cv::Mat pattern(1, screenCols, CV_8UC3);
114
        cv::Mat pattern(1, screenCols, CV_8UC3);
115
        cv::Mat patternInv(1, screenCols, CV_8UC3);
115
        cv::Mat patternInv(1, screenCols, CV_8UC3);
116
 
116
 
117
        for(unsigned int j=0; j<screenCols; j++){
117
        for(unsigned int j=0; j<screenCols; j++){
118
 
118
 
119
            unsigned int jGray = binaryToGray(j);
119
            unsigned int jGray = binaryToGray(j);
120
            // Amplitude of channels
120
            // Amplitude of channels
121
            int bit = (int)getBit(jGray, NbitsHorz-p);
121
            int bit = (int)getBit(jGray, NbitsHorz-p);
122
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
122
            pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
123
            int invBit = bit^1;
123
            int invBit = bit^1;
124
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
124
            patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
125
        }
125
        }
126
        patterns.push_back(pattern);
126
        patterns.push_back(pattern);
127
        patterns.push_back(patternInv);
127
        patterns.push_back(patternInv);
128
    }
128
    }
129
 
129
 
130
    // vertical encoding patterns
130
    // vertical encoding patterns
131
    for(unsigned int p=0; p<NbitsVert; p++){
131
    for(unsigned int p=0; p<NbitsVert; p++){
132
        cv::Mat pattern(screenRows, 1, CV_8UC3);
132
        cv::Mat pattern(screenRows, 1, CV_8UC3);
133
        cv::Mat patternInv(screenRows, 1, CV_8UC3);
133
        cv::Mat patternInv(screenRows, 1, CV_8UC3);
134
 
134
 
135
        for(unsigned int j=0; j<screenRows; j++){
135
        for(unsigned int j=0; j<screenRows; j++){
136
 
136
 
137
            unsigned int jGray = binaryToGray(j);
137
            unsigned int jGray = binaryToGray(j);
138
            // Amplitude of channels
138
            // Amplitude of channels
139
            int bit = (int)getBit(jGray, NbitsVert-p);
139
            int bit = (int)getBit(jGray, NbitsVert-p);
140
            pattern.at<cv::Vec3b>(j,0) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
140
            pattern.at<cv::Vec3b>(j,0) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
141
            int invBit = bit^1;
141
            int invBit = bit^1;
142
            patternInv.at<cv::Vec3b>(j,0) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
142
            patternInv.at<cv::Vec3b>(j,0) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
143
        }
143
        }
144
        patterns.push_back(pattern);
144
        patterns.push_back(pattern);
145
        patterns.push_back(patternInv);
145
        patterns.push_back(patternInv);
146
    }
146
    }
147
 
147
 
148
}
148
}
149
 
149
 
150
cv::Mat AlgorithmGrayCodeHQ::getEncodingPattern(unsigned int depth){
150
cv::Mat AlgorithmGrayCodeHQ::getEncodingPattern(unsigned int depth){
151
    return patterns[depth];
151
    return patterns[depth];
152
}
152
}
153
 
153
 
154
 
154
 
155
bool sortingLarger(cv::Vec3i i,cv::Vec3i j){ return (i[2]<j[2]);}
155
static bool sortingLarger(cv::Vec3d i,cv::Vec3d j){ return (i[2]<j[2]);}
156
bool sortingEqual(cv::Vec3i i,cv::Vec3i j){ return (i[2]==j[2]);}
156
static bool sortingEqual(cv::Vec3d i,cv::Vec3d j){ return (i[2]==j[2]);}
157
 
157
 
158
void getIntersectionLabels(const cv::Mat& codeHorz, const cv::Mat& codeVert, int NbitsHorz, int NbitsVert, std::vector<cv::Vec3i>& intersections){
158
static void getIntersectionLabels(const cv::Mat& codeHorz, const cv::Mat& codeVert, int NbitsHorz, int NbitsVert, std::vector< cv::Vec3d >& intersections){
159
 
159
 
160
    int nRows = codeHorz.rows;
160
    int nRows = codeHorz.rows;
161
    int nCols = codeHorz.cols;
161
    int nCols = codeHorz.cols;
162
 
162
 
163
    int labelHorz;
163
    int labelHorz;
164
    int labelVert;
164
    int labelVert;
165
    int labelHorzRight;
165
    int labelHorzRight;
166
    int labelVertBelow;
166
    int labelVertBelow;
167
 
167
 
168
    // collect intersections
168
    // collect intersections
169
    for(int row=0; row<nRows+1; row++){
169
    for(int row=0; row<nRows+1; row++){
170
        for(int col=0; col<nCols+1; col++){
170
        for(int col=0; col<nCols+1; col++){
171
 
171
 
172
            labelHorz = codeHorz.at<int>(row, col);
172
            labelHorz = codeHorz.at<int>(row, col);
173
            labelHorzRight = codeHorz.at<int>(row, col+1);
173
            labelHorzRight = codeHorz.at<int>(row, col+1);
174
 
174
 
175
            labelVert = codeVert.at<int>(row, col);
175
            labelVert = codeVert.at<int>(row, col);
176
            labelVertBelow = codeVert.at<int>(row+1, col);
176
            labelVertBelow = codeVert.at<int>(row+1, col);
177
 
177
 
178
            // labels need to be non-background, and differ in exactly one bit
178
            // labels need to be non-background, and differ in exactly one bit
179
            if(labelHorz != -1 && labelHorzRight != -1 &&
179
            if(labelHorz != -1 && labelHorzRight != -1 &&
180
               countBits(labelHorz^labelHorzRight) == 1 &&
180
               countBits(labelHorz^labelHorzRight) == 1 &&
181
               labelVert != -1 && labelVertBelow != -1 &&
181
               labelVert != -1 && labelVertBelow != -1 &&
182
               countBits(labelVert^labelVertBelow) == 1){
182
               countBits(labelVert^labelVertBelow) == 1){
183
 
183
 
184
                // OVERFLOW??
184
                // OVERFLOW??
185
                int orderingRelation = (labelHorz << NbitsHorz+2*NbitsVert) + (labelHorzRight << 2*NbitsVert) +
185
                double orderingRelation = ((ulong)labelHorz << NbitsHorz+2*NbitsVert) + ((ulong)labelHorzRight << 2*NbitsVert) +
186
                                       (labelVert << NbitsVert) + labelVertBelow;
186
                                          ((ulong)labelVert << NbitsVert) + (ulong)labelVertBelow;
187
                // store left label column
187
                // store left label column
188
                intersections.push_back(cv::Vec3i(row, col, orderingRelation));
188
                intersections.push_back(cv::Vec3d(row, col, orderingRelation));
189
            }
189
            }
190
        }
190
        }
191
    }
191
    }
192
    // sort
192
    // sort
193
    std::sort(intersections.begin(), intersections.end(), sortingLarger);
193
    std::sort(intersections.begin(), intersections.end(), sortingLarger);
194
 
194
 
195
    // remove duplicates
195
    // remove duplicates
196
    std::vector<cv::Vec3i>::iterator it;
196
    std::vector< cv::Vec3d >::iterator it;
197
    it = std::unique(intersections.begin(), intersections.end(), sortingEqual);
197
    it = std::unique(intersections.begin(), intersections.end(), sortingEqual);
198
    intersections.resize(std::distance(intersections.begin(),it));
198
    intersections.resize(std::distance(intersections.begin(),it));
199
}
199
}
200
 
200
 
201
cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
201
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
202
    assert(!img.empty());
202
    assert(!img.empty());
203
    assert(img.channels() == 3);
203
    assert(img.channels() == 3);
204
 
204
 
205
    int x = (int)pt.x;
205
    int x = (int)pt.x;
206
    int y = (int)pt.y;
206
    int y = (int)pt.y;
207
 
207
 
208
    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
208
    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
209
    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
209
    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
210
    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
210
    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
211
    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
211
    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
212
 
212
 
213
    float a = pt.x - (float)x;
213
    float a = pt.x - (float)x;
214
    float c = pt.y - (float)y;
214
    float c = pt.y - (float)y;
215
 
215
 
216
    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)
216
    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)
217
                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
217
                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
218
    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)
218
    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)
219
                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
219
                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
220
    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)
220
    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)
221
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
221
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
222
 
222
 
223
    return cv::Vec3b(b, g, r);
223
    return cv::Vec3b(b, g, r);
224
}
224
}
225
 
225
 
226
void AlgorithmGrayCodeHQ::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){
226
void AlgorithmGrayCodeHQ::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){
227
 
227
 
228
    assert(frames0.size() == N);
228
    assert(frames0.size() == N);
229
    assert(frames1.size() == N);
229
    assert(frames1.size() == N);
230
 
230
 
231
    int frameRows = frames0[0].rows;
231
    int frameRows = frames0[0].rows;
232
    int frameCols = frames0[0].cols;
232
    int frameCols = frames0[0].cols;
233
 
233
 
234
    // gray-scale
234
    // gray-scale
235
    std::vector<cv::Mat> frames0Gray(N);
235
    std::vector<cv::Mat> frames0Gray(N);
236
    std::vector<cv::Mat> frames1Gray(N);
236
    std::vector<cv::Mat> frames1Gray(N);
237
    for(int i=0; i<N; i++){
237
    for(int i=0; i<N; i++){
238
        cv::cvtColor(frames0[i], frames0Gray, CV_RGB2GRAY);
238
        cv::cvtColor(frames0[i], frames0Gray[i], CV_RGB2GRAY);
239
        cv::cvtColor(frames1[i], frames1Gray, CV_RGB2GRAY);
239
        cv::cvtColor(frames1[i], frames1Gray[i], CV_RGB2GRAY);
240
    }
240
    }
241
 
241
 
242
    // colors
242
    // colors
243
    cv::Mat color0 = frames0[0];
243
    cv::Mat color0 = frames0[0];
244
    cv::Mat color1 = frames1[0];
244
    cv::Mat color1 = frames1[0];
245
 
245
 
246
    // occlusion masks
246
    // occlusion masks
247
    cv::Mat occlusion0, occlusion1;
247
    cv::Mat occlusion0, occlusion1;
248
    cv::subtract(frames0Gray[0], frames0Gray[1], occlusion0);
248
    cv::subtract(frames0Gray[0], frames0Gray[1], occlusion0);
249
    occlusion0 = occlusion0 > 25;
249
    occlusion0 = occlusion0 > 25;
250
    cv::subtract(frames1Gray[0], frames1Gray[1], occlusion1);
250
    cv::subtract(frames1Gray[0], frames1Gray[1], occlusion1);
251
    occlusion1 = occlusion1 > 25;
251
    occlusion1 = occlusion1 > 25;
252
 
252
 
253
    // erode occlusion masks
253
    // erode occlusion masks
254
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
254
    cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
255
    cv::erode(occlusion0, occlusion0, strel);
255
    cv::erode(occlusion0, occlusion0, strel);
256
    cv::erode(occlusion1, occlusion1, strel);
256
    cv::erode(occlusion1, occlusion1, strel);
257
 
257
 
258
//cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
258
//cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
259
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
259
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
260
 
260
 
261
    // decode patterns
261
    // decode patterns
262
    cv::Mat code0Horz(frameRows, frameCols, CV_32S, cv::Scalar(0));
262
    cv::Mat code0Horz(frameRows, frameCols, CV_32S, cv::Scalar(0));
263
    cv::Mat code1Horz(frameRows, frameCols, CV_32S, cv::Scalar(0));
263
    cv::Mat code1Horz(frameRows, frameCols, CV_32S, cv::Scalar(0));
264
    cv::Mat code0Vert(frameRows, frameCols, CV_32S, cv::Scalar(0));
264
    cv::Mat code0Vert(frameRows, frameCols, CV_32S, cv::Scalar(0));
265
    cv::Mat code1Vert(frameRows, frameCols, CV_32S, cv::Scalar(0));
265
    cv::Mat code1Vert(frameRows, frameCols, CV_32S, cv::Scalar(0));
266
 
266
 
267
    // horizontal codes into gray code
267
    // horizontal codes into gray code
268
    for(int i=0; i<NbitsHorz; i++){
268
    for(int i=0; i<NbitsHorz; i++){
269
        cv::Mat bit0;
269
        cv::Mat bit0;
270
        cv::subtract(frames0Gray[i*2+2], frames0Gray[i*2+3], bit0);
270
        cv::subtract(frames0Gray[i*2+2], frames0Gray[i*2+3], bit0);
271
        bit0 = bit0 > 0;
271
        bit0 = bit0 > 0;
272
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
272
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
273
        cv::add(code0Horz, bit0*twopowi(NbitsHorz-i-1), code0Horz, cv::Mat(), CV_32S);
273
        cv::add(code0Horz, bit0*twopowi(NbitsHorz-i-1), code0Horz, cv::Mat(), CV_32S);
274
 
274
 
275
        cv::Mat bit1;
275
        cv::Mat bit1;
276
        cv::subtract(frames1Gray[i*2+2], frames1Gray[i*2+3], bit1);
276
        cv::subtract(frames1Gray[i*2+2], frames1Gray[i*2+3], bit1);
277
        bit1 = bit1 > 0;
277
        bit1 = bit1 > 0;
278
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
278
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
279
        cv::add(code1Horz, bit1*twopowi(NbitsHorz-i-1), code1Horz, cv::Mat(), CV_32S);
279
        cv::add(code1Horz, bit1*twopowi(NbitsHorz-i-1), code1Horz, cv::Mat(), CV_32S);
280
    }
280
    }
281
 
281
 
282
    // vertical codes into gray code
282
    // vertical codes into gray code
283
    for(int i=0; i<NbitsVert; i++){
283
    for(int i=0; i<NbitsVert; i++){
284
        cv::Mat bit0;
284
        cv::Mat bit0;
285
        cv::subtract(frames0Gray[i*2+NbitsHorz+2], frames0Gray[i*2+NbitsHorz+3], bit0);
285
        cv::subtract(frames0Gray[i*2+NbitsHorz+2], frames0Gray[i*2+NbitsHorz+3], bit0);
286
        bit0 = bit0 > 0;
286
        bit0 = bit0 > 0;
287
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
287
        bit0.convertTo(bit0, CV_32S, 1.0/255.0);
288
        cv::add(code0Vert, bit0*twopowi(NbitsVert-i-1), code0Vert, cv::Mat(), CV_32S);
288
        cv::add(code0Vert, bit0*twopowi(NbitsVert-i-1), code0Vert, cv::Mat(), CV_32S);
289
 
289
 
290
        cv::Mat bit1;
290
        cv::Mat bit1;
291
        cv::subtract(frames1Gray[i*2+NbitsHorz+2], frames1Gray[i*2+NbitsHorz+3], bit1);
291
        cv::subtract(frames1Gray[i*2+NbitsHorz+2], frames1Gray[i*2+NbitsHorz+3], bit1);
292
        bit1 = bit1 > 0;
292
        bit1 = bit1 > 0;
293
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
293
        bit1.convertTo(bit1, CV_32S, 1.0/255.0);
294
        cv::add(code1Vert, bit1*twopowi(NbitsVert-i-1), code1Vert, cv::Mat(), CV_32S);
294
        cv::add(code1Vert, bit1*twopowi(NbitsVert-i-1), code1Vert, cv::Mat(), CV_32S);
295
    }
295
    }
296
 
296
 
297
//cvtools::writeMat(code0Horz, "code0Horz.mat", "code0Horz");
297
//cvtools::writeMat(code0Horz, "code0Horz.mat", "code0Horz");
298
//cvtools::writeMat(code1Horz, "code1Horz.mat", "code1Horz");
298
//cvtools::writeMat(code1Horz, "code1Horz.mat", "code1Horz");
299
//cvtools::writeMat(code0Vert, "code0Vert.mat", "code0Vert");
299
//cvtools::writeMat(code0Vert, "code0Vert.mat", "code0Vert");
300
//cvtools::writeMat(code1Vert, "code1Vert.mat", "code1Vert");
300
//cvtools::writeMat(code1Vert, "code1Vert.mat", "code1Vert");
301
 
301
 
302
    // set occluded pixels to -1
302
    // set occluded pixels to -1
303
    for(int r=0; r<frameRows; r++){
303
    for(int r=0; r<frameRows; r++){
304
        for(int c=0; c<frameCols; c++){
304
        for(int c=0; c<frameCols; c++){
305
            if(occlusion0.at<char>(r,c) == 0){
305
            if(occlusion0.at<char>(r,c) == 0){
306
                code0Horz.at<float>(r,c) = -1;
306
                code0Horz.at<float>(r,c) = -1;
307
                code0Vert.at<float>(r,c) = -1;
307
                code0Vert.at<float>(r,c) = -1;
308
            }
308
            }
309
            if(occlusion1.at<char>(r,c) == 0){
309
            if(occlusion1.at<char>(r,c) == 0){
310
                code1Horz.at<float>(r,c) = -1;
310
                code1Horz.at<float>(r,c) = -1;
311
                code1Vert.at<float>(r,c) = -1;
311
                code1Vert.at<float>(r,c) = -1;
312
            }
312
            }
313
        }
313
        }
314
    }
314
    }
315
 
315
 
316
    // matching
316
    // matching
317
    std::vector<cv::Vec3i> intersections0, intersections1;
317
    std::vector< cv::Vec3d > intersections0, intersections1;
318
 
318
 
319
    // intersection data structure containing [floor(row), floor(column), orderingRelation]
319
    // intersection data structure containing [floor(row), floor(column), orderingRelation]
320
    getIntersectionLabels(code0Horz, code0Vert, NbitsHorz, NbitsVert, intersections0);
320
    getIntersectionLabels(code0Horz, code0Vert, NbitsHorz, NbitsVert, intersections0);
321
    getIntersectionLabels(code1Horz, code1Vert, NbitsHorz, NbitsVert, intersections1);
321
    getIntersectionLabels(code1Horz, code1Vert, NbitsHorz, NbitsVert, intersections1);
322
 
322
 
323
    // match intersections
323
    // match intersections
324
    std::vector<cv::Vec3i> matches0, matches1;
324
    std::vector< cv::Vec3d > matches0, matches1;
325
    int i=0, j=0;
325
    int i=0, j=0;
326
 
326
 
327
    while(i<intersections0.size() && j<intersections1.size()){
327
    while(i<intersections0.size() && j<intersections1.size()){
328
        if(intersections0[i][2] == intersections1[j][2]){
328
        if(intersections0[i][2] == intersections1[j][2]){
329
            matches0.push_back(intersections0[i]);
329
            matches0.push_back(intersections0[i]);
330
            matches1.push_back(intersections1[j]);
330
            matches1.push_back(intersections1[j]);
331
            i += 1;
331
            i += 1;
332
            j += 1;
332
            j += 1;
333
        } else if(intersections0[i][2] < intersections1[j][2]){
333
        } else if(intersections0[i][2] < intersections1[j][2]){
334
            i += 1;
334
            i += 1;
335
        } else if(intersections0[i][2] > intersections1[j][2]){
335
        } else if(intersections0[i][2] > intersections1[j][2]){
336
            j += 1;
336
            j += 1;
337
        }
337
        }
338
    }
338
    }
339
 
339
 
-
 
340
    int nMatches = matches0.size();
-
 
341
 
-
 
342
    if(nMatches < 1){
-
 
343
        Q.resize(0);
-
 
344
        color.resize(0);
-
 
345
 
-
 
346
        return;
-
 
347
    }
-
 
348
 
340
    std::vector<cv::Vec2f> q0, q1;
349
    std::vector<cv::Vec2f> q0(nMatches), q1(nMatches);
-
 
350
 
-
 
351
    for(int i=0; i<nMatches; i++){
-
 
352
        q0[i] = cv::Vec2f(matches0[i][1], matches0[i][0]);
-
 
353
        q1[i] = cv::Vec2f(matches1[i][1], matches1[i][0]);
-
 
354
    }
341
 
355
 
342
    // TODO: subpixel refinement in both horizontal and vertical
356
    // TODO: subpixel refinement in both horizontal and vertical
343
//    // subpixel refinement finds the intersection of linear interpolants in the positive/negative pattern
357
//    // subpixel refinement finds the intersection of linear interpolants in the positive/negative pattern
344
//    for(int i=0; i<matchedEdges0.size(); i++){
358
//    for(int i=0; i<matchedEdges0.size(); i++){
345
 
359
 
346
//        int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
360
//        int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
347
 
361
 
348
//        // refine for camera 0
362
//        // refine for camera 0
349
//        float c0 = matchedEdges0[i][0];
363
//        float c0 = matchedEdges0[i][0];
350
//        float c1 = c0+1;
364
//        float c1 = c0+1;
351
 
365
 
352
//        float pos0 = frames0Rect[2*level+2].at<char>(row, c0);
366
//        float pos0 = frames0Rect[2*level+2].at<char>(row, c0);
353
//        float pos1 = frames0Rect[2*level+2].at<char>(row, c1);
367
//        float pos1 = frames0Rect[2*level+2].at<char>(row, c1);
354
//        float neg0 = frames0Rect[2*level+3].at<char>(row, c0);
368
//        float neg0 = frames0Rect[2*level+3].at<char>(row, c0);
355
//        float neg1 = frames0Rect[2*level+3].at<char>(row, c1);
369
//        float neg1 = frames0Rect[2*level+3].at<char>(row, c1);
356
 
370
 
357
//        float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
371
//        float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
358
//        q0Rect.push_back(cv::Point2f(col, row));
372
//        q0Rect.push_back(cv::Point2f(col, row));
359
 
373
 
360
//        // refine for camera 1
374
//        // refine for camera 1
361
//        c0 = matchedEdges1[i][0];
375
//        c0 = matchedEdges1[i][0];
362
//        c1 = c0+1;
376
//        c1 = c0+1;
363
 
377
 
364
//        pos0 = frames1Rect[2*level+2].at<char>(row, c0);
378
//        pos0 = frames1Rect[2*level+2].at<char>(row, c0);
365
//        pos1 = frames1Rect[2*level+2].at<char>(row, c1);
379
//        pos1 = frames1Rect[2*level+2].at<char>(row, c1);
366
//        neg0 = frames1Rect[2*level+3].at<char>(row, c0);
380
//        neg0 = frames1Rect[2*level+3].at<char>(row, c0);
367
//        neg1 = frames1Rect[2*level+3].at<char>(row, c1);
381
//        neg1 = frames1Rect[2*level+3].at<char>(row, c1);
368
 
382
 
369
//        col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
383
//        col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
370
//        q1Rect.push_back(cv::Point2f(col, row));
384
//        q1Rect.push_back(cv::Point2f(col, row));
371
 
385
 
372
//    }
386
//    }
373
 
387
 
374
 
388
 
375
    int nMatches = q0.size();
-
 
376
 
-
 
377
    if(nMatches < 1){
-
 
378
        Q.resize(0);
-
 
379
        color.resize(0);
-
 
380
 
389
 
381
        return;
-
 
382
    }
-
 
383
 
390
 
384
    // retrieve color information (at integer coordinates)
391
    // retrieve color information (at integer coordinates)
385
    color.resize(nMatches);
392
    color.resize(nMatches);
386
    for(int i=0; i<nMatches; i++){
393
    for(int i=0; i<nMatches; i++){
387
 
394
 
388
        cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
395
//        cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
389
        cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
396
//        cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
390
//        cv::Vec3b c0 = getColorSubpix(color0Rect, q0Rect[i]);
397
        cv::Vec3b c0 = getColorSubpix(color0, q0[i]);
391
//        cv::Vec3b c1 = getColorSubpix(color1Rect, q0Rect[i]);
398
        cv::Vec3b c1 = getColorSubpix(color1, q1[i]);
392
 
399
 
393
        color[i] = 0.5*c0 + 0.5*c1;
400
        color[i] = 0.5*c0 + 0.5*c1;
394
    }
401
    }
395
 
402
 
396
    // triangulate points
403
    // triangulate points
397
    cv::Mat P0(3, 4, CV_32F, cv::Scalar(0.0));
404
    cv::Mat P0(3, 4, CV_32F, cv::Scalar(0.0));
398
    cv::Mat(calibration.K0).copyTo(P0.colRange(0, 3));
405
    cv::Mat(calibration.K0).copyTo(P0.colRange(0, 3));
399
 
406
 
400
    cv::Mat P1(3, 4, CV_32F), temp(3,4,CV_32F);
407
    cv::Mat P1(3, 4, CV_32F), temp(3,4,CV_32F);
401
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
408
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
402
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
409
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
403
    P1 = cv::Mat(calibration.K1) * temp;
410
    P1 = cv::Mat(calibration.K1) * temp;
404
 
411
 
405
    cv::correctMatches(calibration.F, q0, q1, q0, q1);
412
    cv::correctMatches(calibration.F, q0, q1, q0, q1);
406
 
413
 
407
    cv::Mat QMatHomogenous, QMat;
414
    cv::Mat QMatHomogenous, QMat;
408
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
415
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
409
 
416
 
410
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
417
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
411
    cvtools::matToPoints3f(QMat, Q);
418
    cvtools::matToPoints3f(QMat, Q);
412
 
419
 
413
}
420
}
414
 
421