Subversion Repositories seema-scanner

Rev

Rev 193 | Rev 207 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 193 Rev 195
Line 74... Line 74...
74
 
74
 
75
cv::Mat AlgorithmPhaseShiftEmbedded::getEncodingPattern(unsigned int depth){
75
cv::Mat AlgorithmPhaseShiftEmbedded::getEncodingPattern(unsigned int depth){
76
    return patterns[depth];
76
    return patterns[depth];
77
}
77
}
78
 
78
 
79
static void decodeEmbeddedPS(const std::vector<cv::Mat> &frames, cv::Mat &up, cv::Mat &upRange, float screenCols){
79
static void decodeEmbeddedPS(const std::vector<cv::Mat> &frames, cv::Mat &up, cv::Mat &upRange){
80
 
80
 
81
    const int N = frames.size();
81
    const int N = frames.size();
82
 
82
 
83
    // Construct shift matrix
83
    // Construct shift matrix
84
    cv::Mat A(N, 1 + 2*M, CV_32F);
84
    cv::Mat A(N, 1 + 2*M, CV_32F);
-
 
85
    A.setTo(0.0);
85
    A.col(0).setTo(1.0);
86
    A.col(0).setTo(1.0);
86
 
87
 
87
    int rowBegin = 0;
88
    int rowBegin = 0;
88
    for(int m=0; m<M; m++){
89
    for(int m=0; m<M; m++){
89
 
90
 
Line 100... Line 101...
100
 
101
 
101
        // Copy into the A matrix
102
        // Copy into the A matrix
102
        Am.copyTo(A.rowRange(rowBegin, rowBegin+nSteps).colRange(1+2*m, 1+2*(m+1)));
103
        Am.copyTo(A.rowRange(rowBegin, rowBegin+nSteps).colRange(1+2*m, 1+2*(m+1)));
103
        rowBegin += nSteps;
104
        rowBegin += nSteps;
104
    }
105
    }
105
    //std::cout << A << std::endl << std::endl;
106
//    std::cout << A << std::endl << std::endl;
-
 
107
 
-
 
108
    // Invert A
-
 
109
    cv::Mat Ainv;
-
 
110
    cv::invert(A, Ainv, cv::DECOMP_SVD);
106
 
111
 
107
    int frameRows = frames[0].rows;
112
    int frameRows = frames[0].rows;
108
    int frameCols = frames[0].cols;
113
    int frameCols = frames[0].cols;
109
 
114
 
110
    // DC-offset
115
    // DC-offset
Line 124... Line 129...
124
            for(int i=0; i<N; i++)
129
            for(int i=0; i<N; i++)
125
                r.at<float>(i) = frames[i].at<uchar>(row, col);
130
                r.at<float>(i) = frames[i].at<uchar>(row, col);
126
 
131
 
127
            // Solve
132
            // Solve
128
            cv::Mat u; //[o, a cos1, a sin1, a cos2, a sin2, ...]
133
            cv::Mat u; //[o, a cos1, a sin1, a cos2, a sin2, ...]
129
            cv::solve(A, r, u, cv::DECOMP_SVD);
134
            //cv::solve(A, r, u, cv::DECOMP_SVD);
-
 
135
            u = Ainv*r;
130
 
136
 
131
            for(int m=0; m<M; m++)
137
            for(int m=0; m<M; m++)
132
                phim[m].at<float>(row, col) = std::atan2(u.at<float>(m*2+1), u.at<float>(m*2+2));
138
                phim[m].at<float>(row, col) = std::atan2(u.at<float>(m*2+1), u.at<float>(m*2+2));
133
 
139
 
134
            O.at<float>(row, col) = u.at<float>(0);
140
            O.at<float>(row, col) = u.at<float>(0);
Line 136... Line 142...
136
 
142
 
137
    }
143
    }
138
 
144
 
139
    #if 0
145
    #if 0
140
        for(int i=0; i<N; i++)
146
        for(int i=0; i<N; i++)
141
            cvtools::writeMat(frames[i], QString("frames_%1.mat").arg(i).toStdString().c_str());
147
            cv::imwrite(QString("frames_%1.png").arg(i).toStdString().c_str(), frames[i]);
142
        cvtools::writeMat(O, "O.mat");
148
        cvtools::writeMat(O, "O.mat");
143
        for(int m=0; m<M; m++)
149
        for(int m=0; m<M; m++)
144
            cvtools::writeMat(phim[m], QString("phim_%1.mat").arg(m).toStdString().c_str());
150
            cvtools::writeMat(phim[m], QString("phim_%1.mat").arg(m).toStdString().c_str());
145
    #endif
151
    #endif
146
 
152
 
Line 165... Line 171...
165
        Fm[m] = 1.0;
171
        Fm[m] = 1.0;
166
        for(int i=0; i<=m; i++)
172
        for(int i=0; i<=m; i++)
167
            Fm[m] *= 1.0/Tm[i];
173
            Fm[m] *= 1.0/Tm[i];
168
    }
174
    }
169
 
175
 
-
 
176
    // Compute pattern frequencies
-
 
177
    float fm[M];
-
 
178
    for(int m=0; m<M; m++)
-
 
179
        fm[m] = Fm[0];
-
 
180
    for(int m=1; m<M; m++)
-
 
181
        fm[m] += Fm[m];
-
 
182
 
170
    // Unwrap phase cue sequence
183
    // Unwrap phase cue sequence
171
    cv::Mat upCue = Phim[M-1];
184
    cv::Mat upCue = Phim[M-1];
172
    for(int m=M-2; m>0; m--){
185
    for(int m=M-2; m>0; m--){
173
        upCue = unwrapWithCue(Phim[m], upCue, screenCols*Fm[m]);
186
        upCue = unwrapWithCue(Phim[m], upCue, Fm[m]/Fm[m+1]);
174
        #if 1
187
        #if 0
175
                cvtools::writeMat(upCue, "upCue.mat", "upCue");
188
                cvtools::writeMat(upCue, "upCue.mat", "upCue");
176
        #endif
189
        #endif
177
    }
190
    }
178
 
191
 
179
    // Unwrap high frequency patterns
192
    // Unwrap high frequency patterns
180
    std::vector<cv::Mat> upm(M);
193
    std::vector<cv::Mat> upm(M);
181
    for(int m=0; m<M; m++){
194
    for(int m=0; m<M; m++){
182
        upm[m] = unwrapWithCue(phim[m], upCue, 1.0/Fm[1]);
195
        upm[m] = unwrapWithCue(phim[m], upCue, fm[m]/Fm[M-1]);
183
    }
196
    }
184
 
197
 
185
    #if 1
198
    #if 1
186
        for(int m=0; m<M; m++)
199
        for(int m=0; m<M; m++)
187
            cvtools::writeMat(upm[m], QString("upm_%1.mat").arg(m).toStdString().c_str());
200
            cvtools::writeMat(upm[m], QString("upm_%1.mat").arg(m).toStdString().c_str());
188
    #endif
201
    #endif
189
 
202
 
190
    // Determine range of phases (for outlier detection)
203
    // Determine range of phases (for outlier detection)
191
    cv::Mat upMin = upm[0];
204
    cv::Mat upMin = upm[0].clone();
192
    cv::Mat upMax = upm[0];
205
    cv::Mat upMax = upm[0].clone();
193
    for(int m=1; m<M; m++){
206
    for(int m=1; m<M; m++){
194
        upMin = cv::min(upMin, upm[m]);
207
        upMin = cv::min(upm[m], upMin);
195
        upMax = cv::max(upMax, upm[m]);
208
        upMax = cv::max(upm[m], upMax);
196
    }
209
    }
-
 
210
 
-
 
211
    #if 1
-
 
212
        cvtools::writeMat(upMin, "upMin.mat");
-
 
213
        cvtools::writeMat(upMax, "upMax.mat");
-
 
214
    #endif
-
 
215
 
197
    upRange = upMax-upMin;
216
    upRange = upMax-upMin;
198
 
217
 
199
    // Return average of phase maps
218
    // Return average of phase maps
200
    up = upm[0];
219
    up = upm[0].clone();
201
    for(int m=1; m<M; m++)
220
    for(int m=1; m<M; m++)
202
        up += upm[m];
221
        up += upm[m];
203
    up /= M;
222
    up /= M;
204
}
223
}
205
 
224
 
Line 241... Line 260...
241
 
260
 
242
    // Decode camera 0
261
    // Decode camera 0
243
    std::vector<cv::Mat> frames0Patterns(frames0Rect.begin()+2, frames0Rect.end());
262
    std::vector<cv::Mat> frames0Patterns(frames0Rect.begin()+2, frames0Rect.end());
244
 
263
 
245
    cv::Mat up0, up0Range;
264
    cv::Mat up0, up0Range;
246
    decodeEmbeddedPS(frames0Patterns, up0, up0Range, screenCols);
265
    decodeEmbeddedPS(frames0Patterns, up0, up0Range);
247
    up0 *= screenCols;
266
    up0 *= screenCols;
248
 
267
 
249
    #ifdef QT_DEBUG
268
    #ifdef QT_DEBUG
250
        cvtools::writeMat(up0, "up0.mat", "up0");
269
        cvtools::writeMat(up0, "up0.mat", "up0");
251
        cvtools::writeMat(up0Range, "up0Range.mat", "up0Range");
270
        cvtools::writeMat(up0Range, "up0Range.mat", "up0Range");
Line 253... Line 272...
253
 
272
 
254
    // Decode camera 1
273
    // Decode camera 1
255
    std::vector<cv::Mat> frames1Patterns(frames1Rect.begin()+2, frames1Rect.end());
274
    std::vector<cv::Mat> frames1Patterns(frames1Rect.begin()+2, frames1Rect.end());
256
 
275
 
257
    cv::Mat up1, up1Range;
276
    cv::Mat up1, up1Range;
258
    decodeEmbeddedPS(frames1Patterns, up1, up1Range, screenCols);
277
    decodeEmbeddedPS(frames1Patterns, up1, up1Range);
259
    up1 *= screenCols;
278
    up1 *= screenCols;
260
 
279
 
261
    #ifdef QT_DEBUG
280
    #ifdef QT_DEBUG
262
        cvtools::writeMat(up1, "up1.mat", "up1");
281
        cvtools::writeMat(up1, "up1.mat", "up1");
263
    #endif
282
    #endif