python April tag安装与使用(单应性矩阵)

本文介绍了如何在Windows系统中使用Apriltag库进行静态图片中Apriltag的识别,包括计数和角点定位,并演示了如何在动态视频中实时检测。还讲解了如何利用检测到的tag角点计算单应性矩阵,用于图像变换。
摘要由CSDN通过智能技术生成

April tag安装

在win系统,python安装方法。

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为表盘在像素中大小,5320为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)
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值