Opencv .at方法没有按预期工作?

时间:2016-11-07 22:05:01

标签: opencv mat

尝试执行一些简单的转换,让我的相机姿势处于更易于管理的坐标中,并且我一直遇到一个问题:

    vector< Mat> newpoints; 

    Mat oldpoint =  Mat::zeros(4,1, CV_32F);

    oldpoint.at<float>(3,0) = 1.0;

    Mat translation = Mat::zeros(4, 4, CV_32F);
    Mat rotation = Mat::zeros(4, 4, CV_32F);
    Mat tmp_rot = Mat::zeros(3,3,CV_32F);
    Mat trans;
    Mat rota;

cout << "rearranging coords" << endl;

for(int i = 0; i < tvecs.size(); i++){
    trans = tvecs[i];
    rota = rvecs[i];

    translation.at<float>(0,0) = 1.0;
    translation.at<float>(1,1) = 1.0;
    translation.at<float>(2,2) = 1.0;
    translation.at<float>(3,3) = 1.0;

    translation.at<float>(0,3) = trans.at<float>(0);
    translation.at<float>(1,3) = trans.at<float>(1);
    translation.at<float>(2,3) = trans.at<float>(2);

    Rodrigues(rota, tmp_rot);

    rotation.at<float>(0,0) = tmp_rot.at<float>(0,0);
    rotation.at<float>(0,1) = tmp_rot.at<float>(0,1);
    rotation.at<float>(0,2) = tmp_rot.at<float>(0,2);
    rotation.at<float>(1,0) = tmp_rot.at<float>(1,0);
    rotation.at<float>(1,1) = tmp_rot.at<float>(1,1);
    rotation.at<float>(1,2) = tmp_rot.at<float>(1,2);
    rotation.at<float>(2,0) = tmp_rot.at<float>(2,0);
    rotation.at<float>(2,1) = tmp_rot.at<float>(2,1);
    rotation.at<float>(2,2) = tmp_rot.at<float>(2,2);
    rotation.at<float>(3,3) = 1.0;
}

当我使用右侧的.at时,for内的值赋值没有任何意义。它们与原始矩阵中的值完全不同。

1 个答案:

答案 0 :(得分:1)

像Micha指出的那样,有一种类型不匹配。

修正:

    tvecs[i].convertTo(trans, CV_32F);
    rvecs[i].convertTo(rota, CV_32F);
    //trans = tvecs[i];
    //rota = rvecs[i];