根据Apriltag进行角度和距离检测
Posted 卓晴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据Apriltag进行角度和距离检测相关的知识,希望对你有一定的参考价值。
简 介: 根据检测到的Apriltag的图像,可以提取摄像头与Apriltag之间的距离和角度,这些参数可以用于对移动车模的定位。
关键词
: Apriltag,单应矩阵,欧拉角
§00 背景介绍
在 第十七届全国大学生智能车智能视觉组 中,采用 AprilTag 作为车模场地辅助定位。这其中需要设计到 Apriltag检测 、对 不同视角下完成Apriltag方位参数确定 。通过两个,或者两个以上的Apriltag的检测,确定在同一平面上车模作品所在的位置。
▲ 图1 不同系列的Apriltag码
- 官网:https://april.eecs.umich.edu/software/apriltag.html
- git仓库地址:https://github.com/AprilRobotics/apriltag
那么如何利用Apriltag检测方法来获取相机与视觉基准Apriltag之间的方位(距离和夹角)?这需要对Apriltag方法和结果进行讨论。
§01 Apriltag检测结果
安装Python下检测Apriltag软件包的方法:
pip install pupil-apriltags
在Linux
下,则直接安装: apriltag
。
1.1 Apriltag检测结果参数
检测Apriltag的函数。
from pupil_apriltags import Detector
at_detector = Detector(families='tag36h11',
nthreads=1,
quad_decimate=1.0,
quad_sigma=0.0,
refine_edges=1,
decode_sharpening=0.25,
debug=0)
在 旋转的Apriltag码 中,利用apriltag检测到图片中的APRILTAG的参数。
在 APRILTAG详解-PYTHON实现 给出了这些参数的介绍
- tag_family: 所需要的系列;tag25h9, tag36h11等等
- tag_id: tag的ID数码
- hamming:表明检测结果中存在多个bit位错误。如果允许存在一定的错误比特,可能会导致出现错误的正样本概率。
- goodness:
- decision_margin:表征了数据bit为的亮度值与判定平均阈值之间的平均差异。值越大,表示图片质量越好。
- homography:表征了从(-1,1),(1,1),(1,-1),(-1,-1)标准的四方Apriltag码映射到图片中的单应矩阵。
- center: Apriltag图片中心位置;
- corners:Apriltag图片的四个角落的位置;
1.1.1 检测函数参数
Option | Default | Explanation |
---|---|---|
families | ‘tag36h11’ | Tag families, separated with a space |
nthreads | 1 | Number of threads |
quad decimate | 2.0 | Detection of quads can be done on a lower-resolution image, improving speed at a cost of pose accuracy and a slight decrease in detection rate. Decoding the binary payload is still done at full resolution. Set this to 1.0 to use the full resolution. |
quad sigma | 0.0 | What Gaussian blur should be applied to the segmented image. Parameter is the standard deviation in pixels. Very noisy images benefit from non-zero values (e.g. 0.8) |
refine edges | 1 | When non-zero, the edges of the each quad are adjusted to “snap to” strong gradients nearby. This is useful when decimation is employed, as it can increase the quality of the initial quad estimate substantially. Generally recommended to be on (1). Very computationally inexpensive. Option is ignored if quad decimate = 1 |
decode sharpening | 0.25 | How much sharpening should be done to decoded images? This can help decode small tags but may or may not help in odd lighting conditions or low light conditions |
debug | 0 | If 1, will save debug images. Runs very slow |
1.1.2 检测结果含义
Attribute | Explanation |
---|---|
tag family | The family of the tag. |
tag id | The decoded ID of the tag. |
hamming | How many error bits were corrected? Note: accepting large numbers of corrected errors leads to greatly increased false positive rates. NOTE: As of this implementation, the detector cannot detect tags with a Hamming distance greater than 2. |
decision margin | A measure of the quality of the binary decoding process: the average difference between the intensity of a data bit versus the decision threshold. Higher numbers roughly indicate better decodes. This is a reasonable measure of detection accuracy only for very small tags-- not effective for larger tags (where we could have sampled anywhere within a bit cell and still gotten a good detection.) |
homography | The 3x3 homography matrix describing the projection from an “ideal” tag (with corners at (-1,1), (1,1), (1,-1), and (-1, -1)) to pixels in the image. |
center | The center of the detection in image pixel coordinates. |
corners | The corners of the tag in image pixel coordinates. These always wrap counter-clock wise around the tag. |
pose R* | Rotation matrix of the pose estimate. |
pose t* | Translation of the pose estimate. |
pose err* | Object-space error of the estimation. |
1.2 检测示例
下面通过一个实际图片中的Apriltag图片检测,演示对Apriltag的检测。
1.2.1 检测过程
(1)检测图片
这是在 旋转的Apriltag码 拍摄的小型Apriltag定位立方体的照片。模拟在智能视觉组中比赛场地内用于车模位置定位的Apriltag立方体。
▲ 图1.2.1 用于检测的图片
img = cv2.imread(procfile)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.clf()
plt.figure(figsize=(12,12))
plt.axis("off")
plt.imshow(gray, cmap=plt.cm.gray)
(2)检测结果
atd = apriltag.Detector(apriltag.DetectorOptions(families='tag36h11 tag25h9'))
tags = atd.detect(gray)
print(tags)
下面是检测出的结果,可以看到只检测出一个Apriltag的参数。
[Detection(tag_family=b'tag25h9', tag_id=0, hamming=0, goodness=0.0, decision_margin=84.82856750488281, homography=array([[-7.34829223e-01, -2.23934792e-02, -3.21568120e+00],
[-8.83848839e-03, -6.92308636e-01, -2.31210225e+00],
[-1.24067453e-05, -2.62518965e-05, -7.28615154e-03]]), center=array([441.3415206 , 317.32832351]), corners=array([[339.21502686, 222.27757263],
[540.14733887, 223.94987488],
[542.39001465, 411.37576294],
[342.91049194, 410.35256958]]))]
下图给出了检测到的一个Apriltag的关键点(四个角点,一个中心店)的位置。
▲ 图1.2.2 检测出一个Apriltag以及它对应的关键点的位置
for tag in tags:
for c in tag.corners:
print(c)
cv2.circle(img, tuple(c.astype(int)), 4, (255, 0, 0), 2)
cv2.circle(img, tuple(tag.center.astype(int)), 4, (2,180,200), 4)
plt.clf()
plt.figure(figsize=(12,12))
plt.axis("off")
plt.imshow(img)
1.2.2 单应矩阵
从上面的结果中可以看到单应矩阵的数值。
tags[0].homography:
[[-7.34829223e-01 -2.23934792e-02 -3.21568120e+00]
[-8.83848839e-03 -6.92308636e-01 -2.31210225e+00]
[-1.24067453e-05 -2.62518965e-05 -7.28615154e-03]]
利用单应矩阵对tags中的corners进行反变换:
O = H − 1 ⋅ C o r n e r s T O = H^ - 1 \\cdot Corners^T O=H−1⋅CornersT
imgpos = ones([4, 3])*0
imgpos[:,:2] = corner
print("imgpos:\\n".format(imgpos))
invcorner = linalg.inv(homo).dot(imgpos.T)
print("invcorner.T:\\n".format(invcorner.T))
imgpos:
[[339.21502686 222.27757263 0. ]
[540.14733887 223.94987488 0. ]
[542.39001465 411.37576294 0. ]
[342.91049194 410.35256958 0. ]]
invcorner.T:
[[-460.32336174 -321.67884815 1.94283559]
[-735.810068 -322.14963888 2.41362631]
[-734.82441934 -596.17990575 3.39927498]
[-461.3157468 -596.64396008 2.93522065]]
如果将反变换中,Corners的Z分量设置为1,结果如下:
imgpos = ones([4, 3])
imgpos[:,:2] = corner
print("imgpos:\\n".format(imgpos))
invcorner = linalg.inv(homo).dot(imgpos.T)
print("invcorner.T:\\n".format(invcorner.T))
imgpos:
[[339.21502686 222.27757263 1. ]
[540.14733887 223.94987488 1. ]
[542.39001465 411.37576294 1. ]
[342.91049194 410.35256958 1. ]]
invcorner.T:
[[ 137.97874849 137.97874849 -137.97874849]
[-137.50795777 137.50795777 -137.50795777]
[-136.52230911 -136.52230911 -136.52230911]
[ 136.98636343 -136.98636343 -136.98636343]]
可以看到反变换的四个点都位于 [ ( 1 , 1 ) , ( − 1 , 1 ) , ( − 1 , − 1 ) , ( 1 , − 1 ) ] × 137 \\left[ \\left( 1,1 \\right),\\left( - 1,1 \\right),\\left( - 1, - 1 \\right),\\left( 1, - 1 \\right) \\right] \\times 137 两个对于Apriltag图片处理问题讨论