April tag安装
April tag应用
静态图片识别
识别图像中tag个数,对其四个角点位置记录。对其后做单应性矩阵计算提供四个点。
#!/usr/bin/env python
# coding: UTF-8
# import apriltag
import pupil_apriltags as apriltag # for windows
import cv2
import numpy as np
import sys
def tag_H(img):
cols, rows, ch = img.shape
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[3].astype(int)), 2, (255, 0, 0), 2) # left-top
cv2.circle(img, tuple(tag.corners[2].astype(int)), 2, (255, 0, 0), 2) # right-top
cv2.circle(img, tuple(tag.corners[1].astype(int)), 2, (255, 0, 0), 2) # right-bottom
cv2.circle(img, tuple(tag.corners[0].astype(int)), 2, (255, 0, 0), 2) # left-bottom
可以输出tag.corners[0],tag.corners[1],tag.corners[2],tag.corners[3]坐标。
动态中识别
持续读取来自摄像头的图像,检测其中的apriltag,并进行动态的显示。
#!/usr/bin/env python
# coding: UTF-8
import apriltag
#import pupil_apriltags as apriltag # for windows
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
at_detector = apriltag.Detector(apriltag.DetectorOptions(families='tag36h11 tag25h9') )
# at_detector = apriltag.Detector(families='tag36h11 tag25h9') #for windows
i=0
while(1):
# 获得图像
ret, frame = cap.read()
# 检测按键
k=cv2.waitKey(1)
if k==27:
break
elif k==ord('s'):
cv2.imwrite('E:/OpenCV_pic/'+str(i)+'.jpg', frame)
i+=1
# 检测apriltag
gray = 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-top
cv2.circle(frame, tuple(tag.corners[1].astype(int)), 4, (255, 0, 0), 2) # right-top
cv2.circle(frame, tuple(tag.corners[2].astype(int)), 4, (255, 0, 0), 2) # right-bottom
cv2.circle(frame, tuple(tag.corners[3].astype(int)), 4, (255, 0, 0), 2) # left-bottom
# 显示检测结果
cv2.imshow('capture', frame)
cap.release()
cv2.destroyAllWindows()
————————————————
版权声明:本文为CSDN博主「arczee」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shenyan0712/article/details/102678534
利用tag四个角点计算单应性矩阵
pts1=np.float32([tag.corners[0],tag.corners[1],tag.corners[2],tag.corners[3]]) #变换前四个点
ax=tag.corners[3][0]
ay=tag.corners[3][1] #tag固定左上点,使tag图像变换
pts2=np.float32([[ax,ay+tag_w],[ax+tag_w,ay+tag_w],[ax+tag_w,ay],[ax,ay]]) #变换后四个点
M3 = cv2.getPerspectiveTransform(pts1,pts2) #求四点映射的变换矩阵
dst3 = cv2.warpPerspective(img,M3,(rows,cols)) #四点映射的变换函数,dst为变换后的图像
print(int(ax),int(ay))
# crop = dst3[int(ay)-20-212:int(ay)-20,int(ax)-53:int(ax)+160] #213*213为表盘在像素中大小,53,20为tag中left-top距离表盘left-bottom距离
crop = dst3[int(ay)-300:int(ay),int(ax)-100:int(ax)+200]
cv2.imshow('crop',crop)
对tag四个角点,图像在变换前为tag.coners,变换后的坐标计算。
通过奇异值分解(SVD)求解透视变换单应性矩阵.
# 算模板中tag与表盘像素差
coners=np.transpose(tag.corners)
coners=np.row_stack((coners, [1,1,1,1])) #H*[X,Y,1]
c=np.dot(M3, coners)
c_normed = c / c.min(axis=0)
coners=np.transpose(c_normed)