Subversion Repositories seema-scanner

Rev

Rev 49 | Rev 74 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jakw 1
#include "cvtools.h"
2
 
3
#ifdef _WIN32
4
#include <cstdint>
5
#endif
6
 
7
#include <stdio.h>
8
 
9
namespace cvtools{
10
 
49 jakw 11
// Lightly modified OpenCV function which accepts a line width argument
50 jakw 12
void drawChessboardCorners(cv::InputOutputArray _image, cv::Size patternSize, cv::InputArray _corners, bool patternWasFound, int line_width){
13
    cv::Mat corners = _corners.getMat();
49 jakw 14
    if( corners.empty() )
15
        return;
50 jakw 16
    cv::Mat image = _image.getMat(); CvMat c_image = _image.getMat();
49 jakw 17
    int nelems = corners.checkVector(2, CV_32F, true);
18
    CV_Assert(nelems >= 0);
19
    cvDrawChessboardCorners( &c_image, patternSize, (CvPoint2D32f*)corners.data,
20
                             nelems, patternWasFound, line_width);
21
}
22
 
50 jakw 23
void cvDrawChessboardCorners(CvArr* _image, CvSize pattern_size, CvPoint2D32f* corners, int count, int found, int line_width){
49 jakw 24
    const int shift = 0;
50 jakw 25
    const int radius = 12;
49 jakw 26
    const int r = radius*(1 << shift);
27
    int i;
28
    CvMat stub, *image;
29
    double scale = 1;
30
    int type, cn, line_type;
31
 
32
    image = cvGetMat( _image, &stub );
33
 
34
    type = CV_MAT_TYPE(image->type);
35
    cn = CV_MAT_CN(type);
36
    if( cn != 1 && cn != 3 && cn != 4 )
37
        CV_Error( CV_StsUnsupportedFormat, "Number of channels must be 1, 3 or 4" );
38
 
39
    switch( CV_MAT_DEPTH(image->type) )
40
    {
41
    case CV_8U:
42
        scale = 1;
43
        break;
44
    case CV_16U:
45
        scale = 256;
46
        break;
47
    case CV_32F:
48
        scale = 1./255;
49
        break;
50
    default:
51
        CV_Error( CV_StsUnsupportedFormat,
52
            "Only 8-bit, 16-bit or floating-point 32-bit images are supported" );
53
    }
54
 
55
    line_type = type == CV_8UC1 || type == CV_8UC3 ? CV_AA : 8;
56
 
57
    if( !found )
58
    {
59
        CvScalar color = {{0,0,255}};
60
        if( cn == 1 )
61
            color = cvScalarAll(200);
62
        color.val[0] *= scale;
63
        color.val[1] *= scale;
64
        color.val[2] *= scale;
65
        color.val[3] *= scale;
66
 
67
        for( i = 0; i < count; i++ )
68
        {
69
            CvPoint pt;
70
            pt.x = cvRound(corners[i].x*(1 << shift));
71
            pt.y = cvRound(corners[i].y*(1 << shift));
72
            cvLine( image, cvPoint( pt.x - r, pt.y - r ),
50 jakw 73
                    cvPoint( pt.x + r, pt.y + r ), color, line_width, line_type, shift );
49 jakw 74
            cvLine( image, cvPoint( pt.x - r, pt.y + r),
50 jakw 75
                    cvPoint( pt.x + r, pt.y - r), color, line_width, line_type, shift );
76
            cvCircle( image, pt, r+(1<<shift), color, line_width, line_type, shift );
49 jakw 77
        }
78
    }
79
    else
80
    {
81
        int x, y;
82
        CvPoint prev_pt = {0, 0};
83
        const int line_max = 7;
84
        static const CvScalar line_colors[line_max] =
85
        {
86
            {{0,0,255}},
87
            {{0,128,255}},
88
            {{0,200,200}},
89
            {{0,255,0}},
90
            {{200,200,0}},
91
            {{255,0,0}},
92
            {{255,0,255}}
93
        };
94
 
95
        for( y = 0, i = 0; y < pattern_size.height; y++ )
96
        {
97
            CvScalar color = line_colors[y % line_max];
98
            if( cn == 1 )
99
                color = cvScalarAll(200);
100
            color.val[0] *= scale;
101
            color.val[1] *= scale;
102
            color.val[2] *= scale;
103
            color.val[3] *= scale;
104
 
105
            for( x = 0; x < pattern_size.width; x++, i++ )
106
            {
107
                CvPoint pt;
108
                pt.x = cvRound(corners[i].x*(1 << shift));
109
                pt.y = cvRound(corners[i].y*(1 << shift));
110
 
111
                if( i != 0 )
112
                    cvLine( image, prev_pt, pt, color, 1, line_type, shift );
113
 
114
                cvLine( image, cvPoint(pt.x - r, pt.y - r),
50 jakw 115
                        cvPoint(pt.x + r, pt.y + r), color, line_width, line_type, shift );
49 jakw 116
                cvLine( image, cvPoint(pt.x - r, pt.y + r),
50 jakw 117
                        cvPoint(pt.x + r, pt.y - r), color, line_width, line_type, shift );
118
                cvCircle( image, pt, r+(1<<shift), color, line_width, line_type, shift );
49 jakw 119
                prev_pt = pt;
120
            }
121
        }
122
    }
123
}
124
 
42 jakw 125
// Convert a 3xN matrix to a vector of Point3fs.
126
void matToPoints3f(const cv::Mat &mat, std::vector<cv::Point3f> &points){
127
 
128
    unsigned int nPoints = mat.cols;
129
    points.resize(nPoints);
130
 
131
    for(unsigned int i=0; i<nPoints; i++)
132
        points[i] = cv::Point3f(mat.at<float>(0, i), mat.at<float>(1, i), mat.at<float>(2, i));
133
}
134
 
135
// Convert a (Dim+1)xN matrix of homogenous points to a DimxN matrix of points in non-homogenous coordinates.
136
void convertMatFromHomogeneous(cv::Mat &src, cv::Mat &dst){
137
    unsigned int N = src.cols;
138
    unsigned int Dim = src.rows-1;
139
    dst.create(Dim, N, src.type());
140
    for(unsigned int i=0; i<N; i++){
141
        for(unsigned int j=0; j<Dim; j++)
142
            dst.at<float>(j,i) = src.at<float>(j,i)/src.at<float>(Dim,i);
143
    }
144
 
145
}
146
 
34 jakw 147
// Function to solve the hand-eye (or eye-in-hand) calibration problem.
148
// Finds [Omega | tau], to minimize ||[R_mark | t_mark][Omega | tau] - [Omega | tau][R | t]||^2
149
// Algorithm according to Tsai, Lenz, A new technique for fully autonomous and efficient 3d robotics hand-eye calibration
150
// DTU, 2014, Jakob Wilm
151
void handEyeCalibrationTsai(const std::vector<cv::Matx33f> R, const std::vector<cv::Vec3f> t, const std::vector<cv::Matx33f> R_mark, const std::vector<cv::Vec3f> t_mark, cv::Matx33f &Omega, cv::Vec3f &tau){
42 jakw 152
 
34 jakw 153
    int N = R.size();
154
    assert(N == R_mark.size());
155
    assert(N == t.size());
156
    assert(N == t_mark.size());
157
 
158
    // construct equations for rotation
159
    cv::Mat A(3*N, 3, CV_32F);
160
    cv::Mat b(3*N, 1, CV_32F);
161
    for(int i=0; i<N; i++){
162
        // angle axis representations
163
        cv::Vec3f rot;
164
        cv::Vec3f rot_mark;
165
        cv::Rodrigues(R[i], rot);
166
        cv::Rodrigues(R_mark[i], rot_mark);
167
 
168
        cv::Vec3f P = 2.0*sin(cv::norm(rot)/2.0)*cv::normalize(rot);
36 jakw 169
//std::cout << "P: " << std::endl << P << std::endl;
34 jakw 170
        cv::Vec3f P_mark = 2.0*sin(cv::norm(rot_mark)/2.0)*cv::normalize(rot_mark);
36 jakw 171
//std::cout << "P_mark: " << std::endl << P_mark << std::endl;
34 jakw 172
        cv::Vec3f sum = P+P_mark;
173
        cv::Mat crossProduct = (cv::Mat_<float>(3,3) << 0.0, -sum(2), sum(1), sum(2), 0.0, -sum(0), -sum(1), sum(0), 0.0);
36 jakw 174
//std::cout << "crossProduct: " << std::endl << crossProduct << std::endl;
34 jakw 175
        crossProduct.copyTo(A.rowRange(i*3, i*3+3));
176
 
177
        cv::Mat(P-P_mark).copyTo(b.rowRange(i*3, i*3+3));
178
    }
179
 
180
    // solve for rotation
36 jakw 181
    cv::Vec3f P_prime;
182
    cv::solve(A, b, P_prime, cv::DECOMP_SVD);
183
    cv::Vec3f P = 2.0*P_prime/(cv::sqrt(1.0 + cv::norm(P_prime)*cv::norm(P_prime)));
34 jakw 184
    float nP = cv::norm(P);
185
    cv::Mat crossProduct = (cv::Mat_<float>(3,3) << 0.0, -P(2), P(1), P(2), 0.0, -P(0), -P(1), P(0), 0.0);
186
    cv::Mat OmegaMat = (1.0-nP*nP/2.0)*cv::Mat::eye(3,3,CV_32F) + 0.5*(cv::Mat(P)*cv::Mat(P).t() + cv::sqrt(4.0 - nP*nP)*crossProduct);
187
    Omega = cv::Matx33f(OmegaMat);
188
 
189
    // construct equations for translation
190
    A.setTo(0.0);
191
    b.setTo(0.0);
192
    for(int i=0; i<N; i++){
193
 
36 jakw 194
        cv::Mat diff = cv::Mat(R_mark[i]) - cv::Mat::eye(3, 3, CV_32F);
34 jakw 195
        diff.copyTo(A.rowRange(i*3, i*3+3));
196
 
36 jakw 197
        cv::Mat diff_mark = cv::Mat(Omega*t[i] - t_mark[i]);
34 jakw 198
        diff_mark.copyTo(b.rowRange(i*3, i*3+3));
199
    }
200
 
201
    // solve for translation
36 jakw 202
    cv::solve(A, b, tau, cv::DECOMP_SVD);
34 jakw 203
}
204
 
205
// Function to fit two sets of corresponding pose data.
206
// Finds [Omega | tau], to minimize ||[R_mark | t_mark] - [Omega | tau][R | t]||^2
31 jakw 207
// Algorithm and notation according to Mili Shah, Comparing two sets of corresponding six degree of freedom data, CVIU 2011.
208
// DTU, 2013, Oline V. Olesen, Jakob Wilm
209
void fitSixDofData(const std::vector<cv::Matx33f> R, const std::vector<cv::Vec3f> t, const std::vector<cv::Matx33f> R_mark, const std::vector<cv::Vec3f> t_mark, cv::Matx33f &Omega, cv::Vec3f &tau){
34 jakw 210
 
31 jakw 211
    int N = R.size();
212
    assert(N == R_mark.size());
213
    assert(N == t.size());
214
    assert(N == t_mark.size());
215
 
216
    // Mean translations
217
    cv::Vec3f t_mean;
218
    cv::Vec3f t_mark_mean;
219
    for(int i=0; i<N; i++){
220
        t_mean += 1.0/N * t[i];
221
        t_mark_mean += 1.0/N * t_mark[i];
222
    }
223
 
224
    // Data with mean adjusted translations
225
    cv::Mat X_bar(3, 4*N, CV_32F);
226
    cv::Mat X_mark_bar(3, 4*N, CV_32F);
227
    for(int i=0; i<N; i++){
33 jakw 228
        cv::Mat(R[i]).copyTo(X_bar.colRange(i*4,i*4+3));
229
        cv::Mat(t[i] - t_mean).copyTo(X_bar.col(i*4+3));
230
        cv::Mat(R_mark[i]).copyTo(X_mark_bar.colRange(i*4,i*4+3));
231
        cv::Mat(t_mark[i] - t_mark_mean).copyTo(X_mark_bar.col(i*4+3));
31 jakw 232
    }
33 jakw 233
    //std::cout << X_bar << std::endl;
31 jakw 234
    // SVD
33 jakw 235
    cv::Mat W, U, VT;
31 jakw 236
    cv::SVDecomp(X_bar*X_mark_bar.t(), W, U, VT);
237
 
238
    cv::Matx33f D = cv::Matx33f::eye();
239
    if(cv::determinant(VT*U) < 0)
240
        D(3,3) = -1;
241
 
242
    // Best rotation
33 jakw 243
    Omega = cv::Matx33f(cv::Mat(VT.t()))*D*cv::Matx33f(cv::Mat(U.t()));
31 jakw 244
 
245
    // Best translation
246
    tau = t_mark_mean - Omega*t_mean;
247
 
248
}
249
 
1 jakw 250
// Forward distortion of points. The inverse of the undistortion in cv::initUndistortRectifyMap().
251
// Inspired by Pascal Thomet, http://code.opencv.org/issues/1387#note-11
252
// Convention for distortion parameters: http://www.vision.caltech.edu/bouguetj/calib_doc/htmls/parameters.html
253
void initDistortMap(const cv::Matx33f cameraMatrix, const cv::Vec<float, 5> distCoeffs, const cv::Size size, cv::Mat &map1, cv::Mat &map2){
254
 
255
    float fx = cameraMatrix(0,0);
256
    float fy = cameraMatrix(1,1);
257
    float ux = cameraMatrix(0,2);
258
    float uy = cameraMatrix(1,2);
259
 
260
    float k1 = distCoeffs[0];
261
    float k2 = distCoeffs[1];
262
    float p1 = distCoeffs[2];
263
    float p2 = distCoeffs[3];
264
    float k3 = distCoeffs[4];
265
 
266
    map1.create(size, CV_32F);
267
    map2.create(size, CV_32F);
268
 
269
    for(int col = 0; col < size.width; col++){
270
        for(int row = 0; row < size.height; row++){
271
 
272
            // move origo to principal point and convert using focal length
273
            float x = (col-ux)/fx;
274
            float y = (row-uy)/fy;
275
 
276
            float xCorrected, yCorrected;
277
 
278
            //Step 1 : correct distortion
279
            float r2 = x*x + y*y;
280
            //radial
281
            xCorrected = x * (1. + k1*r2 + k2*r2*r2 + k3*r2*r2*r2);
282
            yCorrected = y * (1. + k1*r2 + k2*r2*r2 + k3*r2*r2*r2);
283
            //tangential
284
            xCorrected = xCorrected + (2.*p1*x*y + p2*(r2+2.*x*x));
285
            yCorrected = yCorrected + (p1*(r2+2.*y*y) + 2.*p2*x*y);
286
 
287
            //convert back to pixel coordinates
288
            float col_displaced = xCorrected * fx + ux;
289
            float row_displaced = yCorrected * fy + uy;
290
 
291
            // correct the vector in the opposite direction
292
            map1.at<float>(row,col) = col+(col-col_displaced);
293
            map2.at<float>(row,col) = row +(row-row_displaced);
294
        }
295
    }
296
}
297
 
298
// Downsample a texture which was created in virtual column/row space for a diamond pixel array projector
299
cv::Mat diamondDownsample(cv::Mat &pattern){
300
 
301
    cv::Mat pattern_diamond(pattern.rows,pattern.cols/2,CV_8UC3);
302
 
303
    for(unsigned int col = 0; col < pattern_diamond.cols; col++){
304
        for(unsigned int row = 0; row < pattern_diamond.rows; row++){
305
 
306
            pattern_diamond.at<cv::Vec3b>(row,col)=pattern.at<cv::Vec3b>(row,col*2+row%2);
307
        }
308
    }
309
 
310
    return pattern_diamond;
311
 
312
}
313
 
314
 
315
void mouseCallback(int evt, int x, int y, int flags, void* param){
316
    cv::Mat *im = (cv::Mat*) param;
317
    if (evt == CV_EVENT_LBUTTONDOWN) {
318
        if(im->type() == CV_8UC3){
319
            printf("%d %d: %d, %d, %d\n",
320
                   x, y,
321
                   (int)(*im).at<cv::Vec3b>(y, x)[0],
322
                    (int)(*im).at<cv::Vec3b>(y, x)[1],
323
                    (int)(*im).at<cv::Vec3b>(y, x)[2]);
324
        } else if (im->type() == CV_32F) {
325
            printf("%d %d: %f\n",
326
                   x, y,
327
                   im->at<float>(y, x));
328
        }
329
    }
330
}
331
 
332
void imshow(const char *windowName, cv::Mat im, unsigned int x, unsigned int y){
333
 
334
    // Imshow
335
    if(!cvGetWindowHandle(windowName)){
336
        int windowFlags = CV_GUI_EXPANDED | CV_WINDOW_KEEPRATIO;
337
        cv::namedWindow(windowName, windowFlags);
338
        cv::moveWindow(windowName, x, y);
339
    }
340
    cv::imshow(windowName, im);
341
}
342
 
343
void imagesc(const char *windowName, cv::Mat im){
344
 
345
    // Imshow with scaled image
346
 
347
 
348
}
349
 
350
cv::Mat histimage(cv::Mat histogram){
351
 
352
    cv::Mat histImage(512, 640, CV_8UC3, cv::Scalar(0));
353
 
354
    // Normalize the result to [ 2, histImage.rows-2 ]
355
    cv::normalize(histogram, histogram, 2, histImage.rows-2, cv::NORM_MINMAX, -1, cv::Mat());
356
 
357
    float bin_w = (float)histImage.cols/(float)histogram.rows;
358
 
359
    // Draw main histogram
360
    for(int i = 1; i < histogram.rows-10; i++){
361
        cv::line(histImage, cv::Point( bin_w*(i-1), histImage.rows - cvRound(histogram.at<float>(i-1)) ),
362
                 cv::Point( bin_w*(i), histImage.rows - cvRound(histogram.at<float>(i)) ),
363
                 cv::Scalar(255, 255, 255), 2, 4);
364
    }
365
 
366
    // Draw red max
367
    for(int i = histogram.rows-10; i < histogram.rows; i++){
368
        cv::line(histImage, cv::Point( bin_w*(i-1), histImage.rows - cvRound(histogram.at<float>(i-1)) ),
369
                 cv::Point( bin_w*(i), histImage.rows - cvRound(histogram.at<float>(i)) ),
370
                 cv::Scalar(0, 0, 255), 2, 4);
371
    }
372
 
373
    return histImage;
374
}
375
 
376
void hist(const char *windowName, cv::Mat histogram, unsigned int x, unsigned int y){
377
 
378
    // Display
379
    imshow(windowName, histimage(histogram), x, y);
380
    cv::Point(1,2);
381
}
382
 
383
 
384
void writeMat(cv::Mat const& mat, const char* filename, const char* varName, bool bgr2rgb){
385
    /*!
386
         *  \author Philip G. Lee <rocketman768@gmail.com>
387
         *  Write \b mat into \b filename
388
         *  in uncompressed .mat format (Level 5 MATLAB) for Matlab.
389
         *  The variable name in matlab will be \b varName. If
390
         *  \b bgr2rgb is true and there are 3 channels, swaps 1st and 3rd
391
         *  channels in the output. This is needed because OpenCV matrices
392
         *  are bgr, while Matlab is rgb. This has been tested to work with
393
         *  3-channel single-precision floating point matrices, and I hope
394
         *  it works on other types/channels, but not exactly sure.
395
         *  Documentation at <http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf>
396
         */
397
    int textLen = 116;
398
    char* text;
399
    int subsysOffsetLen = 8;
400
    char* subsysOffset;
401
    int verLen = 2;
402
    char* ver;
403
    char flags;
404
    int bytes;
405
    int padBytes;
406
    int bytesPerElement;
407
    int i,j,k,k2;
408
    bool doBgrSwap;
409
    char mxClass;
410
    int32_t miClass;
411
    uchar const* rowPtr;
412
    uint32_t tmp32;
413
    float tmp;
414
    FILE* fp;
415
 
416
    // Matlab constants.
417
    const uint16_t MI = 0x4d49; // Contains "MI" in ascii.
418
    const int32_t miINT8 = 1;
419
    const int32_t miUINT8 = 2;
420
    const int32_t miINT16 = 3;
421
    const int32_t miUINT16 = 4;
422
    const int32_t miINT32 = 5;
423
    const int32_t miUINT32 = 6;
424
    const int32_t miSINGLE = 7;
425
    const int32_t miDOUBLE = 9;
426
    const int32_t miMATRIX = 14;
427
    const char mxDOUBLE_CLASS = 6;
428
    const char mxSINGLE_CLASS = 7;
429
    const char mxINT8_CLASS = 8;
430
    const char mxUINT8_CLASS = 9;
431
    const char mxINT16_CLASS = 10;
432
    const char mxUINT16_CLASS = 11;
433
    const char mxINT32_CLASS = 12;
434
    const char mxUINT32_CLASS = 13;
435
    const uint64_t zero = 0; // Used for padding.
436
 
437
    fp = fopen( filename, "wb" );
438
 
439
    if( fp == 0 )
440
        return;
441
 
442
    const int rows = mat.rows;
443
    const int cols = mat.cols;
444
    const int chans = mat.channels();
445
 
446
    doBgrSwap = (chans==3) && bgr2rgb;
447
 
448
    // I hope this mapping is right :-/
449
    switch( mat.depth() ){
450
    case CV_8U:
451
        mxClass = mxUINT8_CLASS;
452
        miClass = miUINT8;
453
        bytesPerElement = 1;
454
        break;
455
    case CV_8S:
456
        mxClass = mxINT8_CLASS;
457
        miClass = miINT8;
458
        bytesPerElement = 1;
459
        break;
460
    case CV_16U:
461
        mxClass = mxUINT16_CLASS;
462
        miClass = miUINT16;
463
        bytesPerElement = 2;
464
        break;
465
    case CV_16S:
466
        mxClass = mxINT16_CLASS;
467
        miClass = miINT16;
468
        bytesPerElement = 2;
469
        break;
470
    case CV_32S:
471
        mxClass = mxINT32_CLASS;
472
        miClass = miINT32;
473
        bytesPerElement = 4;
474
        break;
475
    case CV_32F:
476
        mxClass = mxSINGLE_CLASS;
477
        miClass = miSINGLE;
478
        bytesPerElement = 4;
479
        break;
480
    case CV_64F:
481
        mxClass = mxDOUBLE_CLASS;
482
        miClass = miDOUBLE;
483
        bytesPerElement = 8;
484
        break;
485
    default:
486
        return;
487
    }
488
 
489
    //==================Mat-file header (128 bytes, page 1-5)==================
490
    text = new char[textLen]; // Human-readable text.
491
    memset( text, ' ', textLen );
492
    text[textLen-1] = '\0';
493
    const char* t = "MATLAB 5.0 MAT-file, Platform: PCWIN";
494
    memcpy( text, t, strlen(t) );
495
 
496
    subsysOffset = new char[subsysOffsetLen]; // Zeros for us.
497
    memset( subsysOffset, 0x00, subsysOffsetLen );
498
    ver = new char[verLen];
499
    ver[0] = 0x00;
500
    ver[1] = 0x01;
501
 
502
    fwrite( text, 1, textLen, fp );
503
    fwrite( subsysOffset, 1, subsysOffsetLen, fp );
504
    fwrite( ver, 1, verLen, fp );
505
    // Endian indicator. MI will show up as "MI" on big-endian
506
    // systems and "IM" on little-endian systems.
507
    fwrite( &MI, 2, 1, fp );
508
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
509
 
510
    //===================Data element tag (8 bytes, page 1-8)==================
511
    bytes = 16 + 24 + (8 + strlen(varName) + (8-(strlen(varName)%8))%8)
512
            + (8 + rows*cols*chans*bytesPerElement);
513
    fwrite( &miMATRIX, 4, 1, fp ); // Data type.
514
    fwrite( &bytes, 4, 1, fp); // Data size in bytes.
515
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
516
 
517
    //====================Array flags (16 bytes, page 1-15)====================
518
    bytes = 8;
519
    fwrite( &miUINT32, 4, 1, fp );
520
    fwrite( &bytes, 4, 1, fp );
521
    flags = 0x00; // Complex, logical, and global flags all off.
522
 
523
    tmp32 = 0;
524
    tmp32 = (flags << 8 ) | (mxClass);
525
    fwrite( &tmp32, 4, 1, fp );
526
 
527
    fwrite( &zero, 4, 1, fp ); // Padding to 64-bit boundary.
528
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
529
 
530
    //===============Dimensions subelement (24 bytes, page 1-17)===============
531
    bytes = 12;
532
    fwrite( &miINT32, 4, 1, fp );
533
    fwrite( &bytes, 4, 1, fp );
534
 
535
    fwrite( &rows, 4, 1, fp );
536
    fwrite( &cols, 4, 1, fp );
537
    fwrite( &chans, 4, 1, fp );
538
    fwrite( &zero, 4, 1, fp ); // Padding to 64-bit boundary.
539
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
540
 
541
    //==Array name (8 + strlen(varName) + (8-(strlen(varName)%8))%8 bytes, page 1-17)==
542
    bytes = strlen(varName);
543
 
544
    fwrite( &miINT8, 4, 1, fp );
545
    fwrite( &bytes, 4, 1, fp );
546
    fwrite( varName, 1, bytes, fp );
547
 
548
    // Pad to nearest 64-bit boundary.
549
    padBytes = (8-(bytes%8))%8;
550
    fwrite( &zero, 1, padBytes, fp );
551
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
552
 
553
    //====Matrix data (rows*cols*chans*bytesPerElement+8 bytes, page 1-20)=====
554
    bytes = rows*cols*chans*bytesPerElement;
555
    fwrite( &miClass, 4, 1, fp );
556
    fwrite( &bytes, 4, 1, fp );
557
 
558
    for( k = 0; k < chans; ++k )
559
    {
560
        if( doBgrSwap )
561
        {
562
            k2 = (k==0)? 2 : ((k==2)? 0 : 1);
563
        }
564
        else
565
            k2 = k;
566
 
567
        for( j = 0; j < cols; ++j )
568
        {
569
            for( i = 0; i < rows; ++i )
570
            {
571
                rowPtr = mat.data + mat.step*i;
572
                fwrite( rowPtr + (chans*j + k2)*bytesPerElement, bytesPerElement, 1, fp );
573
            }
574
        }
575
    }
576
 
577
    // Pad to 64-bit boundary.
578
    padBytes = (8-(bytes%8))%8;
579
    fwrite( &zero, 1, padBytes, fp );
580
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
581
 
582
    fclose(fp);
583
    delete[] text;
584
    delete[] subsysOffset;
585
    delete[] ver;
586
}
587
 
588
 
589
 
590
 
591
 
592
}