// Estimate face absolute orientations
vector<float> CRecognitionAlgs::CalcAbsoluteOrientations(
    const VO_Shape& iShape2D,
    const VO_Shape& iShape3D,
    VO_Shape& oShape2D)
{
    assert (iShape2D.GetNbOfPoints() == iShape3D.GetNbOfPoints() );
    unsigned int NbOfPoints = iShape3D.GetNbOfPoints();
    Point3f pt3d;
    Point2f pt2d;
    float height1 = iShape2D.GetHeight();
    float height2 = iShape3D.GetHeight();
    VO_Shape tempShape2D = iShape2D;
    tempShape2D.Scale(height2/height1);

    //Create the model points
    std::vector<CvPoint3D32f> modelPoints;
    for(unsigned int i = 0; i < NbOfPoints; ++i)
    {
        pt3d = iShape3D.GetA3DPoint(i);
        modelPoints.push_back(cvPoint3D32f(pt3d.x, pt3d.y, pt3d.z));
    }

    //Create the image points
    std::vector<CvPoint2D32f> srcImagePoints;
    for(unsigned int i = 0; i < NbOfPoints; ++i)
    {
        pt2d = tempShape2D.GetA2DPoint(i);
        srcImagePoints.push_back(cvPoint2D32f(pt2d.x, pt2d.y));
    }

    //Create the POSIT object with the model points
    CvPOSITObject *positObject = cvCreatePOSITObject( &modelPoints[0], NbOfPoints );

    //Estimate the pose
    CvMatr32f rotation_matrix = new float[9];
    CvVect32f translation_vector = new float[3];
    CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 100, 1.0e-4f);
    cvPOSIT( positObject, &srcImagePoints[0], FOCAL_LENGTH, criteria, rotation_matrix, translation_vector );

    //rotation_matrix to Euler angles, refer to VO_Shape::GetRotation
    float sin_beta  = -rotation_matrix[0 * 3 + 2];
    float tan_alpha = rotation_matrix[1 * 3 + 2] / rotation_matrix[2 * 3 + 2];
    float tan_gamma = rotation_matrix[0 * 3 + 1] / rotation_matrix[0 * 3 + 0];

    //Project the model points with the estimated pose
    oShape2D = tempShape2D;
    for ( unsigned int i=0; i < NbOfPoints; ++i )
    {
        pt3d.x = rotation_matrix[0] * modelPoints[i].x +
            rotation_matrix[1] * modelPoints[i].y +
            rotation_matrix[2] * modelPoints[i].z +
            translation_vector[0];
        pt3d.y = rotation_matrix[3] * modelPoints[i].x +
            rotation_matrix[4] * modelPoints[i].y +
            rotation_matrix[5] * modelPoints[i].z +
            translation_vector[1];
        pt3d.z = rotation_matrix[6] * modelPoints[i].x +
            rotation_matrix[7] * modelPoints[i].y +
            rotation_matrix[8] * modelPoints[i].z +
            translation_vector[2];
        if ( pt3d.z != 0 )
        {
            pt2d.x = FOCAL_LENGTH * pt3d.x / pt3d.z;
            pt2d.y = FOCAL_LENGTH * pt3d.y / pt3d.z;
        }
        oShape2D.SetA2DPoint(pt2d, i);
    }

    //return Euler angles
    vector<float> pos(3);
    pos[0] = atan(tan_alpha);    // yaw
    pos[1] = asin(sin_beta);     // pitch
    pos[2] = atan(tan_gamma);    // roll
    return pos;
}
Example #2
0
void CV_POSITTest::run( int start_from )
{
    int code = CvTS::OK;

    /* fixed parameters output */
    /*float rot[3][3]={  0.49010f,  0.85057f, 0.19063f,
                      -0.56948f,  0.14671f, 0.80880f,
                       0.65997f, -0.50495f, 0.55629f };

    float trans[3] = { 0.0f, 0.0f, 40.02637f };
    */

    /* Some variables */
    int i, counter;

    CvTermCriteria criteria;
    CvPoint3D32f* obj_points;
    CvPoint2D32f* img_points;
    CvPOSITObject* object;

    float angleX, angleY, angleZ;
    CvRNG* rng = ts->get_rng();
    int progress = 0;

    CvMat* true_rotationX = cvCreateMat( 3, 3, CV_32F );
    CvMat* true_rotationY = cvCreateMat( 3, 3, CV_32F );
    CvMat* true_rotationZ = cvCreateMat( 3, 3, CV_32F );
    CvMat* tmp_matrix = cvCreateMat( 3, 3, CV_32F );
    CvMat* true_rotation = cvCreateMat( 3, 3, CV_32F );
    CvMat* rotation = cvCreateMat( 3, 3, CV_32F );
    CvMat* translation = cvCreateMat( 3, 1, CV_32F );
    CvMat* true_translation = cvCreateMat( 3, 1, CV_32F );

    const float flFocalLength = 760.f;
    const float flEpsilon = 0.1f;

    /* Initilization */
    criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
    criteria.epsilon = flEpsilon;
    criteria.max_iter = 10000;

    /* Allocating source arrays; */
    obj_points = (CvPoint3D32f*)cvAlloc( 8 * sizeof(CvPoint3D32f) );
    img_points = (CvPoint2D32f*)cvAlloc( 8 * sizeof(CvPoint2D32f) );

    /* Fill points arrays with values */

    /* cube model with edge size 10 */
    obj_points[0].x = 0;  obj_points[0].y = 0;  obj_points[0].z = 0;
    obj_points[1].x = 10; obj_points[1].y = 0;  obj_points[1].z = 0;
    obj_points[2].x = 10; obj_points[2].y = 10; obj_points[2].z = 0;
    obj_points[3].x = 0;  obj_points[3].y = 10; obj_points[3].z = 0;
    obj_points[4].x = 0;  obj_points[4].y = 0;  obj_points[4].z = 10;
    obj_points[5].x = 10; obj_points[5].y = 0;  obj_points[5].z = 10;
    obj_points[6].x = 10; obj_points[6].y = 10; obj_points[6].z = 10;
    obj_points[7].x = 0;  obj_points[7].y = 10; obj_points[7].z = 10;

    /* Loop for test some random object positions */
    for( counter = start_from; counter < test_case_count; counter++ )
    {
        ts->update_context( this, counter, true );
        progress = update_progress( progress, counter, test_case_count, 0 );
        
        /* set all rotation matrix to zero */
        cvZero( true_rotationX );
        cvZero( true_rotationY );
        cvZero( true_rotationZ );
        
        /* fill random rotation matrix */
        angleX = (float)(cvTsRandReal(rng)*2*CV_PI);
        angleY = (float)(cvTsRandReal(rng)*2*CV_PI);
        angleZ = (float)(cvTsRandReal(rng)*2*CV_PI);

        true_rotationX->data.fl[0 *3+ 0] = 1;
        true_rotationX->data.fl[1 *3+ 1] = (float)cos(angleX);
        true_rotationX->data.fl[2 *3+ 2] = true_rotationX->data.fl[1 *3+ 1];
        true_rotationX->data.fl[1 *3+ 2] = -(float)sin(angleX);
        true_rotationX->data.fl[2 *3+ 1] = -true_rotationX->data.fl[1 *3+ 2];

        true_rotationY->data.fl[1 *3+ 1] = 1;
        true_rotationY->data.fl[0 *3+ 0] = (float)cos(angleY);
        true_rotationY->data.fl[2 *3+ 2] = true_rotationY->data.fl[0 *3+ 0];
        true_rotationY->data.fl[0 *3+ 2] = -(float)sin(angleY);
        true_rotationY->data.fl[2 *3+ 0] = -true_rotationY->data.fl[0 *3+ 2];

        true_rotationZ->data.fl[2 *3+ 2] = 1;
        true_rotationZ->data.fl[0 *3+ 0] = (float)cos(angleZ);
        true_rotationZ->data.fl[1 *3+ 1] = true_rotationZ->data.fl[0 *3+ 0];
        true_rotationZ->data.fl[0 *3+ 1] = -(float)sin(angleZ);
        true_rotationZ->data.fl[1 *3+ 0] = -true_rotationZ->data.fl[0 *3+ 1];

        cvMatMul( true_rotationX, true_rotationY, tmp_matrix);
        cvMatMul( tmp_matrix, true_rotationZ, true_rotation);

        /* fill translation vector */
        true_translation->data.fl[2] = (float)(cvRandReal(rng)*(2*flFocalLength-40) + 40);
        true_translation->data.fl[0] = (float)((cvRandReal(rng)*2-1)*true_translation->data.fl[2]);
        true_translation->data.fl[1] = (float)((cvRandReal(rng)*2-1)*true_translation->data.fl[2]);

        /* calculate perspective projection */
        for ( i = 0; i < 8; i++ )
        {
            float vec[3];
            CvMat Vec = cvMat( 3, 1, CV_MAT32F, vec );
            CvMat Obj_point = cvMat( 3, 1, CV_MAT32F, &obj_points[i].x );

            cvMatMul( true_rotation, &Obj_point, &Vec );

            vec[0] += true_translation->data.fl[0];
            vec[1] += true_translation->data.fl[1];
            vec[2] += true_translation->data.fl[2];

            img_points[i].x = flFocalLength * vec[0] / vec[2];
            img_points[i].y = flFocalLength * vec[1] / vec[2];
        }

        /*img_points[0].x = 0 ; img_points[0].y =   0;
        img_points[1].x = 80; img_points[1].y = -93;
        img_points[2].x = 245;img_points[2].y =  -77;
        img_points[3].x = 185;img_points[3].y =  32;
        img_points[4].x = 32; img_points[4].y = 135;
        img_points[5].x = 99; img_points[5].y = 35;
        img_points[6].x = 247; img_points[6].y = 62;
        img_points[7].x = 195; img_points[7].y = 179;
        */

        object = cvCreatePOSITObject( obj_points, 8 );
        cvPOSIT( object, img_points, flFocalLength, criteria,
                 rotation->data.fl, translation->data.fl );
        cvReleasePOSITObject( &object );

        code = cvTsCmpEps2( ts, rotation, true_rotation, flEpsilon, false, "rotation matrix" );
        if( code < 0 )
            goto _exit_;

        code = cvTsCmpEps2( ts, translation, true_translation, flEpsilon, false, "translation vector" );
        if( code < 0 )
            goto _exit_;
    }

_exit_:

    cvFree( &obj_points );
    cvFree( &img_points );

    cvReleaseMat( &true_rotationX );
    cvReleaseMat( &true_rotationY );
    cvReleaseMat( &true_rotationZ );
    cvReleaseMat( &tmp_matrix );
    cvReleaseMat( &true_rotation );
    cvReleaseMat( &rotation );
    cvReleaseMat( &translation );
    cvReleaseMat( &true_translation );

    if( code < 0 )
        ts->set_failed_test_info( code );
}