使用openCv的Aruco标记,获得3d角坐标?

ant*_*nti 4 c++ opencv aruco

我正在使用opencv 3.2检测打印的Aruco标记:

aruco::estimatePoseSingleMarkers(corners, markerLength, camMatrix, distCoeffs, rvecs,tvecs);
Run Code Online (Sandbox Code Playgroud)

这将返回标记的平移和旋转矢量.我需要的是标记每个角落的3d坐标.

因为我知道标记长度,我可以做类似的事情

corner1 = tvecs[0] - markerlength /2;
corner2 = tvecs[0] + markerlength /2;
Run Code Online (Sandbox Code Playgroud)

....

但是有更好的方法吗?还是现有的功能?总结一下,我有:

在2d广场的中心的3d点.

那个方格两边的长度.

方形的旋转值.

如何找到角落的三维坐标?

Qua*_*ang 13

首先,我们假设我们只有一个标记side = 2 * half_side.

在此输入图像描述

其次,aruco::detectMarker返回相机在标记世界中的相对位置.因此,我假设您正在寻找相机世界中角落的坐标.

然后,在标记的空间中:

     [ half_side ]      [     0     ]
E  = [     0     ], F = [ half_side ]
     [     0     ]      [     0     ]
Run Code Online (Sandbox Code Playgroud)

其中O正方形的中心具有坐标tvec(在相机的世界中),并且标记的旋转垫由rot_mat计算cv::Rodrigues(rvec,rot_mat).

现在,使用针孔相机模型,P凸轮世界中的点与标记世界的坐标之间的关系是:

[P_x_cam]             [P_x_marker]
[P_y_cam] = rot_mat * [P_y_marker] + tvec
[P_z_cam]             [P_z_marker]    
Run Code Online (Sandbox Code Playgroud)

例如,中心O,这是[0,0,0]在标志的世界,是tvec在凸轮的世界.

所以,Ecam世界的坐标是:

[E_x_cam]             [half_side]
|E_y_cam| = rot_mat * |    0    | + tvec
[E_z_cam]             [    0    ] 
Run Code Online (Sandbox Code Playgroud)

奇迹般地,它是rot_mat第一列乘以half_size和的总和tvec.类似地,的座标FIS rot_mat的第二列乘以half_sizetvec.

现在,例如,可以计算角落

C - O = (E - O) + (F - O), B - O = (E - O) - (F - O)
Run Code Online (Sandbox Code Playgroud)

其中E-O恰好rot_mat的第一列乘以half_size.

考虑到所有这些,我们可以组成这个功能:

vector<Point3f> getCornersInCameraWorld(double side, Vec3d rvec, Vec3d tvec){

     double half_side = side/2;


     // compute rot_mat
     Mat rot_mat;
     Rodrigues(rvec, rot_mat);

     // transpose of rot_mat for easy columns extraction
     Mat rot_mat_t = rot_mat.t();

     // the two E-O and F-O vectors
     double * tmp = rot_mat_t.ptr<double>(0);
     Point3f camWorldE(tmp[0]*half_side,
                       tmp[1]*half_side,
                       tmp[2]*half_side);

     tmp = rot_mat_t.ptr<double>(1);
     Point3f camWorldF(tmp[0]*half_side,
                       tmp[1]*half_side,
                       tmp[2]*half_side);

     // convert tvec to point
     Point3f tvec_3f(tvec[0], tvec[1], tvec[2]);

     // return vector:
     vector<Point3f> ret(4,tvec_3f);

     ret[0] +=  camWorldE + camWorldF;
     ret[1] += -camWorldE + camWorldF;
     ret[2] += -camWorldE - camWorldF;
     ret[3] +=  camWorldE - camWorldF;

     return ret;
}
Run Code Online (Sandbox Code Playgroud)

注1:我讨厌SO没有MathJax

注2:必须有一些我不知道的更快的实现.

  • 这是一个令人惊奇的答案。完美工作的代码和易于理解的解释。+100。:) 非常感谢。 (3认同)

Mar*_*rco 5

我使用从 cv2.aruco.estimatePoseSingleMarkers() 返回的 rvec 和 tvec 为上述标记角旋转编写了一个 python 实现。感谢@Quang Hoang 的详细解释。

import numpy as np

# rotate a markers corners by rvec and translate by tvec if given
# input is the size of a marker.
# In the markerworld the 4 markercorners are at (x,y) = (+- markersize/2, +- markersize/2)
# returns the rotated and translated corners and the rotation matrix
def rotate_marker_corners(rvec, markersize, tvec = None):

    mhalf = markersize / 2.0
    # convert rot vector to rot matrix both do: markerworld -> cam-world
    mrv, jacobian = cv2.Rodrigues(rvec)

    #in markerworld the corners are all in the xy-plane so z is zero at first
    X = mhalf * mrv[:,0] #rotate the x = mhalf
    Y = mhalf * mrv[:,1] #rotate the y = mhalf
    minusX = X * (-1)
    minusY = Y * (-1)

    # calculate 4 corners of the marker in camworld. corners are enumerated clockwise
    markercorners = []
    markercorners.append(np.add(minusX, Y)) #was upper left in markerworld
    markercorners.append(np.add(X, Y)) #was upper right in markerworld
    markercorners.append(np.add( X, minusY)) #was lower right in markerworld
    markercorners.append(np.add(minusX, minusY)) #was lower left in markerworld
    # if tvec given, move all by tvec
    if tvec is not None:
        C = tvec #center of marker in camworld
        for i, mc in enumerate(markercorners):
            makercorners[i] = np.add(C,mc) #add tvec to each corner
    #print('Vec X, Y, C, dot(X,Y)', X,Y,C, np.dot(X,Y)) # just for debug
    markercorners = np.array(markercorners,dtype=np.float32) # type needed when used as input to cv2
    return markercorners, mrv

'''
Copyright 2019 Marco Noll, Garmin International Inc. Licensed under the Apache 
License, Version 2.0 (the "License"); you may not use this file except in compliance 
with the License. You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software distributed 
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the specific 
language governing permissions and limitations under the License.
'''
Run Code Online (Sandbox Code Playgroud)