本文主要是介绍Apriltag使用之一:python下的安装与检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安装
在python中有几个实现的apriltag包。在windows下:
pip install pupil-apriltags
在linux下:
pip install apriltag
简单示例
示例一
这个例子中读取一个图像文件并进行检测
#!/usr/bin/env python
# coding: UTF-8
import apriltag
#import pupil_apriltags as apriltag # for windows
import cv2
import numpy as np
import sysimg =cv2.imread("apriltag_image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 创建一个apriltag检测器
at_detector = apriltag.Detector(apriltag.DetectorOptions(families='tag36h11 tag25h9') )
# at_detector = apriltag.Detector(families='tag36h11 tag25h9') #for windows
# 进行apriltag检测,得到检测到的apriltag的列表
tags = at_detector.detect(gray)
print("%d apriltags have been detected."%len(tags))
for tag in tags:cv2.circle(img, tuple(tag.corners[0].astype(int)), 4,(255,0,0), 2) # left-topcv2.circle(img, tuple(tag.corners[1].astype(int)), 4,(255,0,0), 2) # right-topcv2.circle(img, tuple(tag.corners[2].astype(int)), 4,(255,0,0), 2) # right-bottomcv2.circle(img, tuple(tag.corners[3].astype(int)), 4,(255,0,0), 2) # left-bottomcv2.imshow("apriltag_test",img)
cv2.waitKey()
其中获得的apriltag码的四个顶点可以通过homography变换到标准apriltag码的(-1,1),(1,1),(1,-1),(-1,-1)顶点。
示例二
这个示例中,我们持续读取来自摄像头的图像,检测其中的apriltag,并进行动态的显示
#!/usr/bin/env python
# coding: UTF-8
import apriltag
#import pupil_apriltags as apriltag # for windows
import cv2
import numpy as npcap = cv2.VideoCapture(0)
at_detector = apriltag.Detector(apriltag.DetectorOptions(families='tag36h11 tag25h9') )
# at_detector = apriltag.Detector(families='tag36h11 tag25h9') #for windowsi=0
while(1):# 获得图像ret, frame = cap.read()# 检测按键k=cv2.waitKey(1)if k==27:breakelif k==ord('s'):cv2.imwrite('E:/OpenCV_pic/'+str(i)+'.jpg', frame)i+=1# 检测apriltaggray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)tags = at_detector.detect(gray)for tag in tags:cv2.circle(frame, tuple(tag.corners[0].astype(int)), 4, (255, 0, 0), 2) # left-topcv2.circle(frame, tuple(tag.corners[1].astype(int)), 4, (255, 0, 0), 2) # right-topcv2.circle(frame, tuple(tag.corners[2].astype(int)), 4, (255, 0, 0), 2) # right-bottomcv2.circle(frame, tuple(tag.corners[3].astype(int)), 4, (255, 0, 0), 2) # left-bottom# 显示检测结果cv2.imshow('capture', frame)cap.release()
cv2.destroyAllWindows()
当运行这个代码对tag16h5的apriltag码进行检测时,可以看到显示的图像上会有假的apriltag出现:
而对tag36h11和tag25h9码进行检测则不会出现。原因应该是tag16h5码过于简单,容错率比较低,当图像具有丰富的变化时就很容易出现误检测。所以尽量使用tag36h11和tag25h9码。
这篇关于Apriltag使用之一:python下的安装与检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!