基于Python下的Apriltag检测
Posted 卓晴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Python下的Apriltag检测相关的知识,希望对你有一定的参考价值。
简 介: 在Windows下下载Aprilttags检测工具包,对于图片中的Apriltag检测进行了初步的实验。
关键词
: Apriltag
§01 Apriltag检测
在 第十七届智能车竞赛智能视觉组比赛 中,利用了 Apriltag 作为场地内移动平台的定位基准。下面通过实验验证Apriltag基准下,车模定位的精度和稳定性。
1.1 软件安装
通过Python平台进行实验。在 Windows下安装Apriltag软件包。下面的安装过程参见了: Apriltag使用之一:python下的安装与检测
!pip install apriltag
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting apriltag
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/f2/e5/957bb26d575141bc25af7214c14e315c9ebb9a12ccf7c8d09ed40b59bb29/apriltag-0.0.16.tar.gz (201kB)
|████████████████████████████████| 204kB 6.9MB/s eta 0:00:01
Building wheels for collected packages: apriltag
Building wheel for apriltag (setup.py) ... done
Created wheel for apriltag: filename=apriltag-0.0.16-cp37-cp37m-linux_x86_64.whl size=452175 sha256=d96bf635950b28a95ebfd0de20dcc4167520b5ebe291ca35a946a3f2269464ce
Stored in directory: /home/aistudio/.cache/pip/wheels/7c/93/d0/f5128991a88d5609b5a87ab639a488d63f664cf5f862f26232
Successfully built apriltag
Installing collected packages: apriltag
Successfully installed apriltag-0.0.16
1.2 基本测试
1.2.1 准备April图片
从 MATLAB readAprilTag 下来测试Apriltag图片。根据下载网站介绍,这张图上的Apriltag的系列属于“tag36h11”。
▲ 图1.2.1 测试Apriltag图片
(1)读取显示
import sys,os,math,time
import matplotlib.pyplot as plt
from numpy import *
import apriltag
import cv2
filename = '/home/aistudio/work/apriltag/211222003539.JPG'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.figure(figsize=(10,10))
plt.imshow(gray, cmap=plt.cm.gray)
▲ 图1.2.2 读取的Apriltag
1.2.2 检测Apriltag
(1)运行程序检查输出结果
at_detector = apriltag.Detector(apriltag.DetectorOptions(families='tag36h11 tag25h9'))
tags = at_detector.detect(gray)
print("tags: ".format(tags))
tags: [Detection(tag_family=b'tag36h11', tag_id=25, hamming=0, goodness=0.0, decision_margin=91.3055648803711, homography=array([[-7.87509998e-01, 2.41417461e-01, 4.23456319e+00],
[-4.48012182e-02, -4.85332325e-01, 2.98685475e+00],
[ 8.01207863e-05, 6.64032624e-04, 2.06647960e-02]]), center=array([204.91676722, 144.53831297]), corners=array([[239.98501587, 176.54994202],
[159.63618469, 170.67903137],
[172.28639221, 114.75206757],
[247.70874023, 119.83428192]])), Detection(tag_family=b'tag36h11', tag_id=40, hamming=0, goodness=0.0, decision_margin=91.85741424560547, homography=array([[ 7.39238560e-01, -2.05494924e-01, -7.01276897e+00],
[ 2.40134622e-02, 5.15044464e-01, -4.06511210e+00],
[-1.06282177e-04, -5.43717318e-04, -1.97884482e-02]]), center=array([354.3870095 , 205.42854438]), corners=array([[394.31161499, 240.57174683],
[313.57714844, 235.44726562],
[317.00183105, 172.52064514],
[393.43164062, 176.70828247]])), Detection(tag_family=b'tag36h11', tag_id=58, hamming=0, goodness=0.0, decision_margin=89.47430419921875, homography=array([[-8.01388138e-01, 2.31142479e-01, 3.31658082e+00],
[-9.15290364e-02, -4.61505819e-01, 4.47529673e+00],
[-1.52557638e-04, 6.30739876e-04, 1.77670950e-02]]), center=array([186.66984256, 251.88680187]), corners=array([[224.81613159, 290.84140015],
[134.48406982, 285.28799438],
[150.52307129, 214.97409058],
[234.44848633, 221.30636597]])), Detection(tag_family=b'tag36h11', tag_id=72, hamming=0, goodness=0.0, decision_margin=92.50230407714844, homography=array([[ 7.20749823e-01, -1.81448329e-01, -9.34844133e+00],
[ 1.67731094e-02, 4.91868533e-01, -4.85718429e+00],
[-7.98075722e-05, -5.38616031e-04, -1.79806136e-02]]), center=array([519.91781505, 270.13451264]), corners=array([[569.49859619, 309.05236816],
[482.0418396 , 304.32250977],
[473.63418579, 233.80471802],
[555.90893555, 237.64784241]])), Detection(tag_family=b'tag36h11', tag_id=94, hamming=0, goodness=0.0, decision_margin=88.40879821777344, homography=array([[-6.86081205e-01, 2.00609106e-01, 1.06077797e+01],
[-2.76750082e-03, -5.01083216e-01, 3.42209841e+00],
[ 2.16688961e-04, 5.85781006e-04, 2.10566186e-02]]), center=array([503.7741315 , 162.51889615]), corners=array([[547.70269775, 193.83432007],
[469.90100098, 189.50617981],
[463.07089233, 133.50271606],
[536.48022461, 136.46141052]]))]
(2)tags的格式
print("type(tags): ".format(type(tags)), "len(tags): ".format(len(tags)))
type(tags): <class 'list'>
len(tags): 5
tag.tag_family: b'tag36h11'
tag.tag_id: 25
tag.homography: [[-7.87509998e-01 2.41417461e-01 4.23456319e+00]
[-4.48012182e-02 -4.85332325e-01 2.98685475e+00]
[ 8.01207863e-05 6.64032624e-04 2.06647960e-02]]
tag.goodness: 0.0
tag.hamming: 0
tag.decision_margin: 91.3055648803711
1.2.3 标记Apriltag
for tag in tags:
print(tuple(tag.corners[0].astype(int)))
for i in range(4):
cv2.circle(img, tuple(tag.corners[i].astype(int)), 4, (255, 0, 0), 2)
cv2.circle(img, tuple(tag.center.astype(int)), 4, (2, 180, 200), 4)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.show()
▲ 图1.2.3 检测到的5个Apriltag的位置
而对tag36h11和tag25h9码进行检测则不会出现。原因应该是tag16h5码过于简单,容错率比较低,当图像具有丰富的变化时就很容易出现误检测。所以尽量使用tag36h11和tag25h9码。
§02 更多测试
2.1 官网图片检测
在 AprilTag 官网首页有两个图片,其中包含了一些Apriltag的图片:
▲ 图2.1.1 带有移动机器人的草地
▲ 图2.1.2 不同系列的Apriltag
下面利用Apriltag Detector检测上述图片中的数据。
2.1.1 下载图片
filename = 'https://img-blog.csdnimg.cn/img_convert/63876837fa343c6e100a030690c5aa6c.png'
outfile = '/home/aistudio/work/apriltag/ap1.png'
if not os.path.isfile(outfile):
wget.download(filename, outfile)
img = cv2.imread(outfile)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.figure(figsize=(10,10))
plt.imshow(gray, cmap=plt.cm.gray)
▲ 图2.1.3 下载转换成灰度图片
2.1.2 检测与标记
at_detector = apriltag.Detector(apriltag.DetectorOptions(families='tag36h11'))
tags = at_detector.detect(gray)
标记的程序与前面相同。
▲ 图2.1.4 检测出7个Apriltag
对于第二张图,只检测出第一个“tag36h11”。
▲ 图2.1.5 只检测出第一个tag36h11
※ 检测总结 ※
以上是关于基于Python下的Apriltag检测的主要内容,如果未能解决你的问题,请参考以下文章