IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OpenCV Discussion :

Utilisation de cvStereoCalibrate


Sujet :

OpenCV

  1. #1
    Membre habitué
    Inscrit en
    Décembre 2008
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 8
    Par défaut Utilisation de cvStereoCalibrate
    bonjour
    j'aimerai savoir quelque sont les paramétres à rentrer sur la fonction cvstereocalibrate:
    void cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1,
    const CvMat* _imagePoints2, const CvMat* _npoints,
    CvMat* _cameraMatrix1, CvMat* _distCoeffs1,
    CvMat* _cameraMatrix2, CvMat* _distCoeffs2,
    CvSize imageSize, CvMat* _R, CvMat* _T,
    CvMat* _E, CvMat* _F,
    CvTermCriteria termCrit, int flags )

    j'ai essayé de me documenter sur les explications fourni par opencv doc mais j'ai pas trouvé et ça une semaine que je suis blocqué.
    merci davance

  2. #2
    Membre confirmé Avatar de xtra.creativity
    Développeur informatique
    Inscrit en
    Mars 2007
    Messages
    108
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2007
    Messages : 108
    Par défaut
    Il suffit de regarder l'exemple donne par opencv pour comprendre.......


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
     
    /* This is sample from the OpenCV book. The copyright notice is below */
     
    /* *************** License:**************************
       Oct. 3, 2008
       Right to use this code in any way you want without warrenty, support or any guarentee of it working.
     
       BOOK: It would be nice if you cited it:
       Learning OpenCV: Computer Vision with the OpenCV Library
         by Gary Bradski and Adrian Kaehler
         Published by O'Reilly Media, October 3, 2008
     
       AVAILABLE AT: 
         http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134
         Or: http://oreilly.com/catalog/9780596516130/
         ISBN-10: 0596516134 or: ISBN-13: 978-0596516130    
     
       OTHER OPENCV SITES:
       * The source code is on sourceforge at:
         http://sourceforge.net/projects/opencvlibrary/
       * The OpenCV wiki page (As of Oct 1, 2008 this is down for changing over servers, but should come back):
         http://opencvlibrary.sourceforge.net/
       * An active user group is at:
         http://tech.groups.yahoo.com/group/OpenCV/
       * The minutes of weekly OpenCV development meetings are at:
         http://pr.willowgarage.com/wiki/OpenCV
       ************************************************** */
     
    #include "cv.h"
    #include "cxmisc.h"
    #include "highgui.h"
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <stdio.h>
    #include <ctype.h>
     
    using namespace std;
     
    //
    // Given a list of chessboard images, the number of corners (nx, ny)
    // on the chessboards, and a flag: useCalibrated for calibrated (0) or
    // uncalibrated (1: use cvStereoCalibrate(), 2: compute fundamental
    // matrix separately) stereo. Calibrate the cameras and display the
    // rectified results along with the computed disparity images.
    //
    static void
    StereoCalib(const char* imageList, int nx, int ny, int useUncalibrated)
    {
        int displayCorners = 0;
        int showUndistorted = 1;
        bool isVerticalStereo = false;//OpenCV can handle left-right
                                          //or up-down camera arrangements
        const int maxScale = 1;
        const float squareSize = 1.f; //Set this to your actual square size
        FILE* f = fopen(imageList, "rt");
        int i, j, lr, nframes, n = nx*ny, N = 0;
        vector<string> imageNames[2];
        vector<CvPoint3D32f> objectPoints;
        vector<CvPoint2D32f> points[2];
        vector<int> npoints;
        vector<uchar> active[2];
        vector<CvPoint2D32f> temp(n);
        CvSize imageSize = {0,0};
        // ARRAY AND VECTOR STORAGE:
        double M1[3][3], M2[3][3], D1[5], D2[5];
        double R[3][3], T[3], E[3][3], F[3][3];
        CvMat _M1 = cvMat(3, 3, CV_64F, M1 );
        CvMat _M2 = cvMat(3, 3, CV_64F, M2 );
        CvMat _D1 = cvMat(1, 5, CV_64F, D1 );
        CvMat _D2 = cvMat(1, 5, CV_64F, D2 );
        CvMat _R = cvMat(3, 3, CV_64F, R );
        CvMat _T = cvMat(3, 1, CV_64F, T );
        CvMat _E = cvMat(3, 3, CV_64F, E );
        CvMat _F = cvMat(3, 3, CV_64F, F );
        if( displayCorners )
            cvNamedWindow( "corners", 1 );
    // READ IN THE LIST OF CHESSBOARDS:
        if( !f )
        {
            fprintf(stderr, "can not open file %s\n", imageList );
            return;
        }
        for(i=0;;i++)
        {
            char buf[1024];
            int count = 0, result=0;
            lr = i % 2;
            vector<CvPoint2D32f>& pts = points[lr];
            if( !fgets( buf, sizeof(buf)-3, f ))
                break;
            size_t len = strlen(buf);
            while( len > 0 && isspace(buf[len-1]))
                buf[--len] = '\0';
            if( buf[0] == '#')
                continue;
            IplImage* img = cvLoadImage( buf, 0 );
            if( !img )
                break;
            imageSize = cvGetSize(img);
            imageNames[lr].push_back(buf);
        //FIND CHESSBOARDS AND CORNERS THEREIN:
            for( int s = 1; s <= maxScale; s++ )
            {
                IplImage* timg = img;
                if( s > 1 )
                {
                    timg = cvCreateImage(cvSize(img->width*s,img->height*s),
                        img->depth, img->nChannels );
                    cvResize( img, timg, CV_INTER_CUBIC );
                }
                result = cvFindChessboardCorners( timg, cvSize(nx, ny),
                    &temp[0], &count,
                    CV_CALIB_CB_ADAPTIVE_THRESH |
                    CV_CALIB_CB_NORMALIZE_IMAGE);
                if( timg != img )
                    cvReleaseImage( &timg );
                if( result || s == maxScale )
                    for( j = 0; j < count; j++ )
                {
                    temp[j].x /= s;
                    temp[j].y /= s;
                }
                if( result )
                    break;
            }
            if( displayCorners )
            {
                printf("%s\n", buf);
                IplImage* cimg = cvCreateImage( imageSize, 8, 3 );
                cvCvtColor( img, cimg, CV_GRAY2BGR );
                cvDrawChessboardCorners( cimg, cvSize(nx, ny), &temp[0],
                    count, result );
                cvShowImage( "corners", cimg );
                cvReleaseImage( &cimg );
                int c = cvWaitKey(1000);
                if( c == 27 || c == 'q' || c == 'Q' ) //Allow ESC to quit
                    exit(-1);
            }
            else
                putchar('.');
            N = pts.size();
            pts.resize(N + n, cvPoint2D32f(0,0));
            active[lr].push_back((uchar)result);
        //assert( result != 0 );
            if( result )
            {
             //Calibration will suffer without subpixel interpolation
                cvFindCornerSubPix( img, &temp[0], count,
                    cvSize(11, 11), cvSize(-1,-1),
                    cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,
                    30, 0.01) );
                copy( temp.begin(), temp.end(), pts.begin() + N );
            }
            cvReleaseImage( &img );
        }
        fclose(f);
        printf("\n");
    // HARVEST CHESSBOARD 3D OBJECT POINT LIST:
        nframes = active[0].size();//Number of good chessboads found
        objectPoints.resize(nframes*n);
        for( i = 0; i < ny; i++ )
            for( j = 0; j < nx; j++ )
            objectPoints[i*nx + j] =
            cvPoint3D32f(i*squareSize, j*squareSize, 0);
        for( i = 1; i < nframes; i++ )
            copy( objectPoints.begin(), objectPoints.begin() + n,
            objectPoints.begin() + i*n );
        npoints.resize(nframes,n);
        N = nframes*n;
        CvMat _objectPoints = cvMat(1, N, CV_32FC3, &objectPoints[0] );
        CvMat _imagePoints1 = cvMat(1, N, CV_32FC2, &points[0][0] );
        CvMat _imagePoints2 = cvMat(1, N, CV_32FC2, &points[1][0] );
        CvMat _npoints = cvMat(1, npoints.size(), CV_32S, &npoints[0] );
        cvSetIdentity(&_M1);
        cvSetIdentity(&_M2);
        cvZero(&_D1);
        cvZero(&_D2);
     
    // CALIBRATE THE STEREO CAMERAS
        printf("Running stereo calibration ...");
        fflush(stdout);
        cvStereoCalibrate( &_objectPoints, &_imagePoints1,
            &_imagePoints2, &_npoints,
            &_M1, &_D1, &_M2, &_D2,
            imageSize, &_R, &_T, &_E, &_F,
            cvTermCriteria(CV_TERMCRIT_ITER+
            CV_TERMCRIT_EPS, 100, 1e-5),
            CV_CALIB_FIX_ASPECT_RATIO +
            CV_CALIB_ZERO_TANGENT_DIST +
            CV_CALIB_SAME_FOCAL_LENGTH );
        printf(" done\n");
    // CALIBRATION QUALITY CHECK
    // because the output fundamental matrix implicitly
    // includes all the output information,
    // we can check the quality of calibration using the
    // epipolar geometry constraint: m2^t*F*m1=0
        vector<CvPoint3D32f> lines[2];
        points[0].resize(N);
        points[1].resize(N);
        _imagePoints1 = cvMat(1, N, CV_32FC2, &points[0][0] );
        _imagePoints2 = cvMat(1, N, CV_32FC2, &points[1][0] );
        lines[0].resize(N);
        lines[1].resize(N);
        CvMat _L1 = cvMat(1, N, CV_32FC3, &lines[0][0]);
        CvMat _L2 = cvMat(1, N, CV_32FC3, &lines[1][0]);
    //Always work in undistorted space
        cvUndistortPoints( &_imagePoints1, &_imagePoints1,
            &_M1, &_D1, 0, &_M1 );
        cvUndistortPoints( &_imagePoints2, &_imagePoints2,
            &_M2, &_D2, 0, &_M2 );
        cvComputeCorrespondEpilines( &_imagePoints1, 1, &_F, &_L1 );
        cvComputeCorrespondEpilines( &_imagePoints2, 2, &_F, &_L2 );
        double avgErr = 0;
        for( i = 0; i < N; i++ )
        {
            double err = fabs(points[0][i].x*lines[1][i].x +
                points[0][i].y*lines[1][i].y + lines[1][i].z)
                + fabs(points[1][i].x*lines[0][i].x +
                points[1][i].y*lines[0][i].y + lines[0][i].z);
            avgErr += err;
        }
        printf( "avg err = %g\n", avgErr/(nframes*n) );
    //COMPUTE AND DISPLAY RECTIFICATION
        if( showUndistorted )
        {
            CvMat* mx1 = cvCreateMat( imageSize.height,
                imageSize.width, CV_32F );
            CvMat* my1 = cvCreateMat( imageSize.height,
                imageSize.width, CV_32F );
            CvMat* mx2 = cvCreateMat( imageSize.height,
     
                imageSize.width, CV_32F );
            CvMat* my2 = cvCreateMat( imageSize.height,
                imageSize.width, CV_32F );
            CvMat* img1r = cvCreateMat( imageSize.height,
                imageSize.width, CV_8U );
            CvMat* img2r = cvCreateMat( imageSize.height,
                imageSize.width, CV_8U );
            CvMat* disp = cvCreateMat( imageSize.height,
                imageSize.width, CV_16S );
            CvMat* vdisp = cvCreateMat( imageSize.height,
                imageSize.width, CV_8U );
            CvMat* pair;
            double R1[3][3], R2[3][3], P1[3][4], P2[3][4];
            CvMat _R1 = cvMat(3, 3, CV_64F, R1);
            CvMat _R2 = cvMat(3, 3, CV_64F, R2);
    // IF BY CALIBRATED (BOUGUET'S METHOD)
            if( useUncalibrated == 0 )
            {
                CvMat _P1 = cvMat(3, 4, CV_64F, P1);
                CvMat _P2 = cvMat(3, 4, CV_64F, P2);
                cvStereoRectify( &_M1, &_M2, &_D1, &_D2, imageSize,
                    &_R, &_T,
                    &_R1, &_R2, &_P1, &_P2, 0,
                    0/*CV_CALIB_ZERO_DISPARITY*/ );
                isVerticalStereo = fabs(P2[1][3]) > fabs(P2[0][3]);
        //Precompute maps for cvRemap()
                cvInitUndistortRectifyMap(&_M1,&_D1,&_R1,&_P1,mx1,my1);
                cvInitUndistortRectifyMap(&_M2,&_D2,&_R2,&_P2,mx2,my2);
            }
    //OR ELSE HARTLEY'S METHOD
            else if( useUncalibrated == 1 || useUncalibrated == 2 )
         // use intrinsic parameters of each camera, but
         // compute the rectification transformation directly
         // from the fundamental matrix
            {
                double H1[3][3], H2[3][3], iM[3][3];
                CvMat _H1 = cvMat(3, 3, CV_64F, H1);
                CvMat _H2 = cvMat(3, 3, CV_64F, H2);
                CvMat _iM = cvMat(3, 3, CV_64F, iM);
        //Just to show you could have independently used F
                if( useUncalibrated == 2 )
                    cvFindFundamentalMat( &_imagePoints1,
                    &_imagePoints2, &_F);
                cvStereoRectifyUncalibrated( &_imagePoints1,
                    &_imagePoints2, &_F,
                    imageSize,
                    &_H1, &_H2, 3);
                cvInvert(&_M1, &_iM);
                cvMatMul(&_H1, &_M1, &_R1);
                cvMatMul(&_iM, &_R1, &_R1);
                cvInvert(&_M2, &_iM);
                cvMatMul(&_H2, &_M2, &_R2);
                cvMatMul(&_iM, &_R2, &_R2);
        //Precompute map for cvRemap()
                cvInitUndistortRectifyMap(&_M1,&_D1,&_R1,&_M1,mx1,my1);
     
                cvInitUndistortRectifyMap(&_M2,&_D1,&_R2,&_M2,mx2,my2);
            }
            else
                assert(0);
            cvNamedWindow( "rectified", 1 );
    // RECTIFY THE IMAGES AND FIND DISPARITY MAPS
            if( !isVerticalStereo )
                pair = cvCreateMat( imageSize.height, imageSize.width*2,
                CV_8UC3 );
            else
                pair = cvCreateMat( imageSize.height*2, imageSize.width,
                CV_8UC3 );
    //Setup for finding stereo corrrespondences
            CvStereoBMState *BMState = cvCreateStereoBMState();
            assert(BMState != 0);
            BMState->preFilterSize=41;
            BMState->preFilterCap=31;
            BMState->SADWindowSize=41;
            BMState->minDisparity=-64;
            BMState->numberOfDisparities=128;
            BMState->textureThreshold=10;
            BMState->uniquenessRatio=15;
            for( i = 0; i < nframes; i++ )
            {
                IplImage* img1=cvLoadImage(imageNames[0][i].c_str(),0);
                IplImage* img2=cvLoadImage(imageNames[1][i].c_str(),0);
                if( img1 && img2 )
                {
                    CvMat part;
                    cvRemap( img1, img1r, mx1, my1 );
                    cvRemap( img2, img2r, mx2, my2 );
                    if( !isVerticalStereo || useUncalibrated != 0 )
                    {
                  // When the stereo camera is oriented vertically,
                  // useUncalibrated==0 does not transpose the
                  // image, so the epipolar lines in the rectified
                  // images are vertical. Stereo correspondence
                  // function does not support such a case.
                        cvFindStereoCorrespondenceBM( img1r, img2r, disp,
                            BMState);
                        cvNormalize( disp, vdisp, 0, 256, CV_MINMAX );
                        cvNamedWindow( "disparity" );
                        cvShowImage( "disparity", vdisp );
                    }
                    if( !isVerticalStereo )
                    {
                        cvGetCols( pair, &part, 0, imageSize.width );
                        cvCvtColor( img1r, &part, CV_GRAY2BGR );
                        cvGetCols( pair, &part, imageSize.width,
                            imageSize.width*2 );
                        cvCvtColor( img2r, &part, CV_GRAY2BGR );
                        for( j = 0; j < imageSize.height; j += 16 )
                            cvLine( pair, cvPoint(0,j),
                            cvPoint(imageSize.width*2,j),
                            CV_RGB(0,255,0));
                    }
                    else
                    {
                        cvGetRows( pair, &part, 0, imageSize.height );
                        cvCvtColor( img1r, &part, CV_GRAY2BGR );
                        cvGetRows( pair, &part, imageSize.height,
                            imageSize.height*2 );
                        cvCvtColor( img2r, &part, CV_GRAY2BGR );
                        for( j = 0; j < imageSize.width; j += 16 )
                            cvLine( pair, cvPoint(j,0),
                            cvPoint(j,imageSize.height*2),
                            CV_RGB(0,255,0));
                    }
                    cvShowImage( "rectified", pair );
                    if( cvWaitKey() == 27 )
                        break;
                }
                cvReleaseImage( &img1 );
                cvReleaseImage( &img2 );
            }
            cvReleaseStereoBMState(&BMState);
            cvReleaseMat( &mx1 );
            cvReleaseMat( &my1 );
            cvReleaseMat( &mx2 );
            cvReleaseMat( &my2 );
            cvReleaseMat( &img1r );
            cvReleaseMat( &img2r );
            cvReleaseMat( &disp );
        }
    }
     
    int main(void)
    {
        StereoCalib("stereo_calib.txt", 9, 6, 1);
        return 0;
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. utiliser les tag [MFC] [Win32] [.NET] [C++/CLI]
    Par hiko-seijuro dans le forum Visual C++
    Réponses: 8
    Dernier message: 08/06/2005, 15h57
  2. Réponses: 4
    Dernier message: 05/06/2002, 14h35
  3. utilisation du meta type ANY
    Par Anonymous dans le forum CORBA
    Réponses: 1
    Dernier message: 15/04/2002, 12h36
  4. [BCB5] Utilisation des Ressources (.res)
    Par Vince78 dans le forum C++Builder
    Réponses: 2
    Dernier message: 04/04/2002, 16h01
  5. Réponses: 2
    Dernier message: 20/03/2002, 23h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo