我使用以下OpenCV代码估算方形标记的姿势,并在图像上绘制标记的3个轴。但是标记的Z轴有时会旋转180度,如下图所示。如何使z轴稳定?
// Marker world coordinates
vector<Point3f> objecPoints;
objecPoints.push_back(Point3f(0, 0, 0));
objecPoints.push_back(Point3f(0, 2.4, 0));
objecPoints.push_back(Point3f(2.4, 2.4, 0));
objecPoints.push_back(Point3f(2.4, 0.0, 0));
// 2D image coordinates of 4 marker corners. They are arranged in the same order for each frame
vector<Point2f> marker2DPoints;
// Calculate Rotation and Translation
cv::Mat Rvec;
cv::Mat_<float> Tvec;
cv::Mat raux, taux;
cv::solvePnP(objecPoints, marker2DPoints, camMatrix, distCoeff, raux, taux);
// Draw marker pose on the image
vector<Point3f> axisPoints3D;
axisPoints3D.push_back(Point3f(0, 0, 0));
axisPoints3D.push_back(Point3f(2.4, 0, 0));
axisPoints3D.push_back(Point3f(0, 2.4, 0));
axisPoints3D.push_back(Point3f(0, 0, 2.4));
vector<Point2f> axisPoints2D;
// Take the camMatrix and distCoeff from camera calibration results
projectPoints(axisPoints3D, Rvec, Tvec, camMatrix, distCoeff, axisPoints2D);
line(srcImg, axisPoints2D[0], axisPoints2D[1], CV_RGB(0, 0, 255), 1, CV_AA);
line(srcImg, axisPoints2D[0], axisPoints2D[2], CV_RGB(0, 255, 0), 1, CV_AA);
line(srcImg, axisPoints2D[0], axisPoints2D[3], CV_RGB(255, 0, 0), 1, CV_AA);
最佳答案
作为评论,这可能会更好,但是我对此没有足够的声誉。我认为这可能是由于solvePnP获取标签坐标的顺序而发生的。此外,由于solvePnP只是试图将3D平面上的4个点(在这种情况下)与图像中的4个2D点进行匹配,因此有多种解决方案。标签可以绕其上轴旋转,也可以上下翻转。从提供的点不知道solvePnP是向上的方向。
我有一个预感,solvePnP对于这个问题有点太笼统了,因为稳定的标签检测算法应该能够以稳定的顺序将角点输入到姿势估计代码中。
编辑:角的顺序很重要,由solvePnP给出的解决方案取决于此。也许生成角点的算法没有按一致的顺序提供角?请分享标签的输出