在本文中,我们将研究使用 Python 和 OpenCV 检测和估计 ArUco 标记的方向。
首先,我们将从生成 ArUco 标记开始。你可以使用特定网站一个一个地创建单个标签,也可以使用我的程序生成整个页面的标记。
一旦你创建了一些标记,我将向你展示另一个检测这些标记的程序。该程序还可以确定这些 ArUco 标记的位姿轴(位姿代表位置和姿态)。
我在这里编写的代码是用 Python 编写的,但是对于更高级的检测和姿态估计应用程序,你可能希望使用 C++ 来实现它们。
生成 ArUco 标记
如果你想一个一个地创建 ArUco 标记,那么你可以使用 Oleg Kalachev 的这个网站:https://chev.me/arucogen/。该网站提供了一个简单的界面,如下所示:
选择 ArUco 字典、标记的 id 和大小。然后下载标记并打印出来。
由于想用 ArUco 标记打印整个页面,所以这个网站对这个没有帮助。
现在,OpenCV 提供了两个名为cv.aruco.GridBoard.create()
和cv.aruco.GridBoard.draw()
的函数——创建并随后绘制一整页的 ArUco 标记。
你可以在此处阅读有关这些功能的更多信息:https://docs.opencv.org/4.x/de/d05/classcv_1_1aruco_1_1GridBoard.html
然而,在尝试这些功能时,对结果不满意。打印时 ArUco 标记的大小不正确,一些标记未正确放置在页面边界内,并且无法添加标记的 id。
因此,决定编写自己的程序来生成 ArUco 标记页面。你可以在下面找到它:
import argparse
import cv2
import sys
import numpy as np
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", required=True, help="path to output image containing ArUCo tag")
ap.add_argument("-i", "--id", type=int, required=True, help="ID of first ArUCo tag to generate")
ap.add_argument("-t", "--type", type=str, default="DICT_ARUCO_ORIGINAL", help="type of ArUCo tag to generate")
ap.add_argument("-d", "--dpi", type=str, default="72", help="the DPI of the output print")
ap.add_argument("-s", "--size", type=int, default=50, help="the size in mm of the ArUco tag")
ap.add_argument("-m", "--margin", type=int, default=5, help="the size in mm of the margins between the ArUco tags")
ap.add_argument("-x", "--x", type=int, default=3, help="number of ArUco tags in the X direction")
ap.add_argument("-y", "--y", type=int, default=4, help="number of ArUco tags in the Y direction")
ap.add_argument("--write-id", default=True, action=argparse.BooleanOptionalAction, help="write the id of the tag or not")
args = vars(ap.parse_args())
ARUCO_DICT = {
"DICT_4X4_50": cv2.aruco.DICT_4X4_50,
"DICT_4X4_100": cv2.aruco.DICT_4X4_100,
"DICT_4X4_250": cv2.aruco.DICT_4X4_250,
"DICT_4X4_1000": cv2.aruco.DICT_4X4_1000,
"DICT_5X5_50": cv2.aruco.DICT_5X5_50,
"DICT_5X5_100