Opencv虚拟相机旋转/平移鸟瞰图

时间:2011-07-07 06:49:19

标签: opencv rotation translation homography

我有一个经过校准的相机,我完全知道内在和外在数据。此外,相机的高度也是已知的。现在我想虚拟旋转相机以获得鸟瞰图,这样我就可以构建具有三个旋转角度和平移的Homography矩阵。

我知道可以通过Homography将2个点从一个图像转换为另一个图像

x = K *(R-t * n / d)K ^ -1 * x'

我现在想知道一些事情: 如果我想在ccs中恢复图像坐标,我必须将它与K ^ -1相乘,对吧?作为图像坐标,我使用(x',y',1)?

然后我需要构建一个旋转矩阵来旋转ccs ......但是我应该使用哪种约定?我怎么知道如何设置我的WCS?

接下来是正常和距离。是否只是将三个点放在地上并计算它们的正常值?并且是相机高度的距离?

此外,我想知道如何改变虚拟鸟瞰摄像机的高度,这样我可以说我想看到3米高的地平面。如何在翻译和单应矩阵中使用单位“米”?

到目前为止,如果有人可以启发并帮助我,那就太好了。并且请不要建议用“getperspective”生成鸟类视图,我已经尝试过但这种方式不适合我。

塞纳

2 个答案:

答案 0 :(得分:14)

这是我建议的代码(这是我的代码之一),在我看来它解答了很多你的问题, 如果你想要距离,我会精确地确定它在Z矩阵中,即(4,3)系数。

希望它能帮到你......

Mat source=imread("Whatyouwant.jpg");
int alpha_=90., beta_=90., gamma_=90.;
int f_ = 500, dist_ = 500;

Mat destination;

string wndname1 = getFormatWindowName("Source: ");
string wndname2 = getFormatWindowName("WarpPerspective: ");
string tbarname1 = "Alpha";
string tbarname2 = "Beta";
string tbarname3 = "Gamma";
string tbarname4 = "f";
string tbarname5 = "Distance";
namedWindow(wndname1, 1);
namedWindow(wndname2, 1);
createTrackbar(tbarname1, wndname2, &alpha_, 180);
createTrackbar(tbarname2, wndname2, &beta_, 180);
createTrackbar(tbarname3, wndname2, &gamma_, 180);
createTrackbar(tbarname4, wndname2, &f_, 2000);
createTrackbar(tbarname5, wndname2, &dist_, 2000);

imshow(wndname1, source);
while(true) {
    double f, dist;
    double alpha, beta, gamma;
    alpha = ((double)alpha_ - 90.)*PI/180;
    beta = ((double)beta_ - 90.)*PI/180;
    gamma = ((double)gamma_ - 90.)*PI/180;
    f = (double) f_;
    dist = (double) dist_;

    Size taille = source.size();
    double w = (double)taille.width, h = (double)taille.height;

    // Projection 2D -> 3D matrix
    Mat A1 = (Mat_<double>(4,3) <<
        1, 0, -w/2,
        0, 1, -h/2,
        0, 0,    0,
        0, 0,    1);

    // Rotation matrices around the X,Y,Z axis
    Mat RX = (Mat_<double>(4, 4) <<
        1,          0,           0, 0,
        0, cos(alpha), -sin(alpha), 0,
        0, sin(alpha),  cos(alpha), 0,
        0,          0,           0, 1);

    Mat RY = (Mat_<double>(4, 4) <<
        cos(beta), 0, -sin(beta), 0,
                0, 1,          0, 0,
        sin(beta), 0,  cos(beta), 0,
                0, 0,          0, 1);

    Mat RZ = (Mat_<double>(4, 4) <<
        cos(gamma), -sin(gamma), 0, 0,
        sin(gamma),  cos(gamma), 0, 0,
        0,          0,           1, 0,
        0,          0,           0, 1);

    // Composed rotation matrix with (RX,RY,RZ)
    Mat R = RX * RY * RZ;

    // Translation matrix on the Z axis change dist will change the height
    Mat T = (Mat_<double>(4, 4) <<
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, dist,
        0, 0, 0, 1);

    // Camera Intrisecs matrix 3D -> 2D
    Mat A2 = (Mat_<double>(3,4) <<
        f, 0, w/2, 0,
        0, f, h/2, 0,
        0, 0,   1, 0);

    // Final and overall transformation matrix
    Mat transfo = A2 * (T * (R * A1));

    // Apply matrix transformation
    warpPerspective(source, destination, transfo, taille, INTER_CUBIC | WARP_INVERSE_MAP);

    imshow(wndname2, destination);
    waitKey(30);
}

答案 1 :(得分:0)

此代码适用于我,但我不知道为什么更换Roll和Pitch角度。当我改变&#34; alpha&#34;时,图像会在音高上扭曲,当我改变&#34; beta&#34;翘曲的图像。所以,我改变了我的旋转矩阵,如下所示。

此外,RY有信号错误。您可以访问:http://en.wikipedia.org/wiki/Rotation_matrix

我使用的旋转矩阵:

Mat RX = (Mat_<double>(4, 4) <<
    1,          0,           0, 0,
    0, cos(beta), -sin(beta), 0,
    0, sin(beta),  cos(beta), 0,
    0,          0,           0, 1);

Mat RY = (Mat_<double>(4, 4) <<
     cos(alpha), 0,  sin(alpha), 0,
              0, 1,           0, 0,
    -sin(alpha), 0,  cos(alpha), 0,
              0, 0,           0, 1);

Mat RZ = (Mat_<double>(4, 4) <<
    cos(gamma), -sin(gamma), 0, 0,
    sin(gamma),  cos(gamma), 0, 0,
    0,          0,           1, 0,
    0,          0,           0, 1);

此致