OpenCV AprilTags 识别

使用环境:

  • Windows10
  • Raspberry Pi 4B
  • Python :3.7.3

Windows下使用 AprilTags

在windows中安装 apriltags 库:

pip install pupil-apriltags

代码部分:

import pupil_apriltags as apriltag     # 在 windows 下引入该库
import cv2

img = cv2.imread('1.jpg', cv2.IMREAD_GRAYSCALE)
detector = apriltag.Detector()
result = detector.detect(img)
print(result)

在这里插入图片描述
识别后得到的信息:
在这里插入图片描述

通过检测到的参数,标记四个角点。

#!/usr/bin/env python
# coding: UTF-8
import pupil_apriltags as apriltag   
import cv2

img = cv2.imread("tag36h11_0.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 创建一个apriltag检测器
detector = apriltag.Detector(families='tag36h11') # windows

# 进行apriltag检测,得到检测到的apriltag的列表
tags = detector.detect(gray)

for tag in tags:
    cv2.circle(img, tuple(tag.corners[0].astype(int)), 4, (255, 0, 255), 2)  # left-top
    cv2.circle(img, tuple(tag.corners[1].astype(int)), 4, (255, 0, 255), 2)  # right-top
    cv2.circle(img, tuple(tag.corners[2].astype(int)), 4, (255, 0, 255), 2)  # right-bottom
    cv2.circle(img, tuple(tag.corners[3].astype(int)), 4, (255, 0, 255), 2)  # left-bottom

cv2.imshow("out_image", img)
cv2.imwrite('image.png', img)
cv2.waitKey()

在这里插入图片描述

使用line绘制边框

import cv2
import pupil_apriltags as apriltag     #  windows

image = cv2.imread('tag36h11_0.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 创建一个apriltag检测器,然后检测AprilTags
options = apriltag.Detector(families='tag36h11')  # windows
results = options.detect(gray)
print(results)

for r in results:
    # 获取4个角点的坐标
    b = (tuple(r.corners[0].astype(int))[0], tuple(r.corners[0].astype(int))[1])
    c = (tuple(r.corners[1].astype(int))[0], tuple(r.corners[1].astype(int))[1])
    d = (tuple(r.corners[2].astype(int))[0], tuple(r.corners[2].astype(int))[1])
    a = (tuple(r.corners[3].astype(int))[0], tuple(r.corners[3].astype(int))[1])

    # 绘制检测到的AprilTag的框
    cv2.line(image, a, b, (255, 0, 255), 2, lineType=cv2.LINE_AA)
    cv2.line(image, b, c, (255, 0, 255), 2, lineType=cv2.LINE_AA)
    cv2.line(image, c, d, (255, 0, 255), 2, lineType=cv2.LINE_AA)
    cv2.line(image, d, a, (255, 0, 255), 2, lineType=cv2.LINE_AA)

    # 绘制 AprilTag 的中心坐标
    (cX, cY) = (int(r.center[0]), int(r.center[1]))
    cv2.circle(image, (cX, cY), 5, (0, 0, 255), -1)

    # 在图像上绘制标文本
    tagFamily = r.tag_family.decode("utf-8")
    cv2.putText(image, tagFamily, (a[0], a[1] - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    cv2.imwrite('image1.png', image)

cv2.imshow("Image", image)
cv2.waitKey(0)

在这里插入图片描述

树莓派4B下使用 AprilTags

在树莓派下安装 AprilTags

pip3 install apriltag

pi@raspberrypi:~ $ pip3 install apriltag
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting apriltag
Downloading https://www.piwheels.org/simple/apriltag/apriltag-0.0.16-cp37-cp37m-linux_armv7l.whl (433kB)
100% |████████████████████████████████| 440kB 212kB/s
Installing collected packages: apriltag
Successfully installed apriltag-0.0.16

打开树莓派4B摄像头进行检测
#!/usr/bin/env python
# coding: UTF-8
import apriltag
import cv2

cap = cv2.VideoCapture(0)
at_detector = apriltag.Detector(apriltag.DetectorOptions(families='tag36h11'))

while(1):
    # 获得图像
    ret, frame = cap.read()
    # 检测按键
    k = cv2.waitKey(1)
    if k==27:
        break
        
    # 检测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()

在这里插入图片描述

打开树莓派4B摄像头进行检测使用类进行封装
import cv2
import apriltag

class ApriltagDetect:
    def __init__(self):
        self.target_id = 0
        self.at_detector = apriltag.Detector(apriltag.DetectorOptions(families='tag36h11'))

    def update_frame(self,frame):
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        tags = self.at_detector.detect(gray)
        for tag in tags:
            print(tag.tag_id)
            cv2.circle(frame, tuple(tag.corners[0].astype(int)), 4, (0, 0, 255), 2) # left-top
            cv2.circle(frame, tuple(tag.corners[1].astype(int)), 4, (0, 0, 255), 2) # right-top
            cv2.circle(frame, tuple(tag.corners[2].astype(int)), 4, (0, 0, 255), 2) # right-bottom
            cv2.circle(frame, tuple(tag.corners[3].astype(int)), 4, (0, 0, 255), 2) # left-bottom

            apriltag_width = abs(tag.corners[0][0] - tag.corners[1][0]) / 2
            target_x = apriltag_width / 2

if __name__ == '__main__':
    cap = cv2.VideoCapture(0)
    cap.set(3,640)
    cap.set(4,480)
    ad = ApriltagDetect()
    while True:
        ret, frame = cap.read()
        frame = cv2.rotate(frame, cv2.ROTATE_180)
        ad.update_frame(frame)
        cv2.imshow("capture", frame)
        if cv2.waitKey(100) & 0xff == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()

踩坑:

安装 opencv-python 库后,无法引用cv2:

pi@raspberrypi:~/Desktop $ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

>>> import cv2
Traceback (most recent call last):
File “”, line 1, in
File “/home/pi/.local/lib/python3.7/site-packages/cv2/init.py”, line 3, in
from .cv2 import *
ImportError: libhdf5_serial.so.103: cannot open shared object file: No such file or directory

解决方法:

在树莓派下安装 opencv-python 库

pip3 install opencv-python

pi@raspberrypi:~/Desktop $ pip3 install opencv-python
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting opencv-python
Downloading https://www.piwheels.org/simple/opencv-python/opencv_python-4.5.3.56-cp37-cp37m-linux_armv7l.whl (10.4MB)
100% |████████████████████████████████| 10.4MB 41kB/s
Requirement already satisfied: numpy>=1.14.5 in /usr/lib/python3/dist-packages (from opencv-python) (1.16.2)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.5.3.56

安装 opencv-contrib-python

pi@raspberrypi:~/Desktop $ pip3 install opencv-contrib-python==4.1.0.25
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting opencv-contrib-python==4.1.0.25
Downloading https://www.piwheels.org/simple/opencv-contrib-python/opencv_contrib_python-4.1.0.25-cp37-cp37m-linux_armv7l.whl (15.7MB)
100% |████████████████████████████████| 15.7MB 27kB/s
Requirement already satisfied: numpy>=1.16.2 in /usr/lib/python3/dist-packages (from opencv-contrib-python==4.1.0.25) (1.16.2)
Installing collected packages: opencv-contrib-python
Successfully installed opencv-contrib-python-4.1.0.25

安装 libatlas-base-dev

pi@raspberrypi:~/Desktop $ sudo apt-get install libatlas-base-dev -y
正在读取软件包列表… 完成
正在分析软件包的依赖关系树
正在读取状态信息… 完成
将会同时安装下列软件:
libatlas3-base
建议安装:
libatlas-doc liblapack-doc
下列【新】软件包将被安装:
libatlas-base-dev libatlas3-base
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 151 个软件包未被升级。
需要下载 5,365 kB 的归档。
解压缩后会消耗 32.1 MB 的额外空间。
获取:1 http://mirrors.sjtug.sjtu.edu.cn/raspbian/raspbian buster/main armhf libatlas3-base armhf 3.10.3-8+rpi1 [2,399 kB]
获取:2 http://mirrors.sjtug.sjtu.edu.cn/raspbian/raspbian buster/main armhf libatlas-base-dev armhf 3.10.3-8+rpi1 [2,966 kB]
已下载 5,365 kB,耗时 10秒 (555 kB/s)
正在选中未选择的软件包 libatlas3-base:armhf。
(正在读取数据库 … 系统当前共安装有 180903 个文件和目录。)
准备解压 …/libatlas3-base_3.10.3-8+rpi1_armhf.deb …
正在解压 libatlas3-base:armhf (3.10.3-8+rpi1) …
正在选中未选择的软件包 libatlas-base-dev:armhf。
准备解压 …/libatlas-base-dev_3.10.3-8+rpi1_armhf.deb …
正在解压 libatlas-base-dev:armhf (3.10.3-8+rpi1) …
正在设置 libatlas3-base:armhf (3.10.3-8+rpi1) …
update-alternatives: 使用 /usr/lib/arm-linux-gnueabihf/atlas/libblas.so.3 来在自动模式中提供 /usr/lib/arm-linux-gnueabihf/libblas.so.3 (libblas.so.3-arm-linux-gnueabihf)
update-alternatives: 使用 /usr/lib/arm-linux-gnueabihf/atlas/liblapack.so.3 来在自动模式中提供 /usr/lib/arm-linux-gnueabihf/liblapack.so.3 (liblapack.so.3-arm-linux-gnueabihf)
正在设置 libatlas-base-dev:armhf (3.10.3-8+rpi1) …
update-alternatives: 使用 /usr/lib/arm-linux-gnueabihf/atlas/libblas.so 来在自动模式中提供 /usr/lib/arm-linux-gnueabihf/libblas.so (libblas.so-arm-linux-gnueabihf)
update-alternatives: 使用 /usr/lib/arm-linux-gnueabihf/atlas/liblapack.so 来在自动模式中提供 /usr/lib/arm-linux-gnueabihf/liblapack.so (liblapack.so-arm-linux-gnueabihf)
正在处理用于 libc-bin (2.28-10+rpi1) 的触发器 …

安装 libjasper-dev

pi@raspberrypi:~/Desktop $ sudo apt-get install libjasper-dev -y
正在读取软件包列表… 完成
正在分析软件包的依赖关系树
正在读取状态信息… 完成
将会同时安装下列软件:
libjasper1
建议安装:
libjasper-runtime
下列【新】软件包将被安装:
libjasper-dev libjasper1
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 151 个软件包未被升级。
需要下载 611 kB 的归档。
解压缩后会消耗 1,207 kB 的额外空间。
获取:1 http://mirrors.sjtug.sjtu.edu.cn/raspbian/raspbian buster/main armhf libjasper1 armhf 1.900.1-debian1-2.4+deb8u1 [110 kB]
获取:2 http://mirrors.bfsu.edu.cn/raspbian/raspbian buster/main armhf libjasper-dev armhf 1.900.1-debian1-2.4+deb8u1 [501 kB]
已下载 611 kB,耗时 3秒 (232 kB/s)
正在选中未选择的软件包 libjasper1:armhf。
(正在读取数据库 … 系统当前共安装有 181101 个文件和目录。)
准备解压 …/libjasper1_1.900.1-debian1-2.4+deb8u1_armhf.deb …
正在解压 libjasper1:armhf (1.900.1-debian1-2.4+deb8u1) …
正在选中未选择的软件包 libjasper-dev。
准备解压 …/libjasper-dev_1.900.1-debian1-2.4+deb8u1_armhf.deb …
正在解压 libjasper-dev (1.900.1-debian1-2.4+deb8u1) …
正在设置 libjasper1:armhf (1.900.1-debian1-2.4+deb8u1) …
正在设置 libjasper-dev (1.900.1-debian1-2.4+deb8u1) …

安装 libqtgui4

pi@raspberrypi:~/Desktop $ sudo apt-get install libqtgui4 -y

安装 python3-pyqt5

sudo apt-get install python3-pyqt5 -y

安装 libqt4-test

pi@raspberrypi:~/Desktop $ sudo apt-get install libqt4-test -y
正在读取软件包列表… 完成
正在分析软件包的依赖关系树
正在读取状态信息… 完成
下列【新】软件包将被安装:
libqt4-test
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 151 个软件包未被升级。
需要下载 98.0 kB 的归档。
解压缩后会消耗 250 kB 的额外空间。
获取:1 http://mirrors.bfsu.edu.cn/raspbian/raspbian buster/main armhf libqt4-test armhf 4:4.8.7+dfsg-18+rpi1+deb10u1 [98.0 kB]
已下载 98.0 kB,耗时 1秒 (71.5 kB/s)
正在选中未选择的软件包 libqt4-test:armhf。
(正在读取数据库 … 系统当前共安装有 181337 个文件和目录。)
准备解压 …/libqt4-test_4%3a4.8.7+dfsg-18+rpi1+deb10u1_armhf.deb …
正在解压 libqt4-test:armhf (4:4.8.7+dfsg-18+rpi1+deb10u1) …
正在设置 libqt4-test:armhf (4:4.8.7+dfsg-18+rpi1+deb10u1) …
正在处理用于 libc-bin (2.28-10+rpi1) 的触发器 …

安装 libhdf5-devt

pi@raspberrypi:~/Desktop $ sudo apt-get install libhdf5-dev -y

安装完以上内容后,才能使用opencv-python

测试 opencv-python能否使用:

pi@raspberrypi:~/Desktop $ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import cv2
>>> cv2.version
‘4.1.0’

显示版本号后,说明opencv-python库可以被使用了。


指令集为:aarch64下,可以尝试使用 apt 安装 python3-opencv

sudo apt install python3-opencv

pi@raspbian:~/桌面$ sudo apt install python3-opencv
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following package was automatically installed and is no longer required:
libre2-5
Use ‘sudo apt autoremove’ to remove it.
The following additional packages will be installed:
autoconf automake autotools-dev gdal-data gfortran gfortran-8 libaec0 libarmadillo9 libarpack2
libcaf-openmpi-3 libcharls2 libcoarrays-dev libcoarrays-openmpi-dev libdap25 libdapclient6v5
libdapserver7v5 libepsilon1 libfreexl1 libfyba0 libgdal20 libgdcm2.8 libgeos-3.7.1 libgeos-c1v5
libgeotiff2 libgfortran-8-dev libgl2ps1.4 libhdf4-0-alt libhdf5-103 libhdf5-openmpi-103 libhwloc-dev
libhwloc-plugins libhwloc5 libibverbs-dev libkmlbase1 libkmlconvenience1 libkmldom1 libkmlengine1



Setting up libopencv-highgui3.2:arm64 (3.2.0+dfsg-6) …
Setting up libopencv-features2d3.2:arm64 (3.2.0+dfsg-6) …
Setting up libopencv-calib3d3.2:arm64 (3.2.0+dfsg-6) …
Setting up libopencv-objdetect3.2:arm64 (3.2.0+dfsg-6) …
Setting up libopencv-stitching3.2:arm64 (3.2.0+dfsg-6) …
Setting up libopencv-videostab3.2:arm64 (3.2.0+dfsg-6) …
Setting up libopencv-contrib3.2:arm64 (3.2.0+dfsg-6) …
Setting up python3-opencv (3.2.0+dfsg-6) …
Processing triggers for libc-bin (2.28-10) …
Processing triggers for man-db (2.8.5-2) …

  • 8
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值