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 cv2img = 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 cv2img = 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-topcv2.circle(img, tuple(tag.corners[1].astype(int)), 4, (255, 0, 255), 2) # right-topcv2.circle(img, tuple(tag.corners[2].astype(int)), 4, (255, 0, 255), 2) # right-bottomcv2.circle(img, tuple(tag.corners[3].astype(int)), 4, (255, 0, 255), 2) # left-bottomcv2.imshow("out_image", img) cv2.imwrite('image.png', img) cv2.waitKey()使用line繪制邊框
import cv2 import pupil_apriltags as apriltag # windowsimage = 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 apriltagpi@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 cv2cap = 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# 檢測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()打開樹莓派4B攝像頭進行檢測使用類進行封裝
import cv2 import apriltagclass ApriltagDetect:def __init__(self):self.target_id = 0self.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-topcv2.circle(frame, tuple(tag.corners[1].astype(int)), 4, (0, 0, 255), 2) # right-topcv2.circle(frame, tuple(tag.corners[2].astype(int)), 4, (0, 0, 255), 2) # right-bottomcv2.circle(frame, tuple(tag.corners[3].astype(int)), 4, (0, 0, 255), 2) # left-bottomapriltag_width = abs(tag.corners[0][0] - tag.corners[1][0]) / 2target_x = apriltag_width / 2if __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'):breakcap.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-pythonpi@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-opencvpi@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) …
總結
以上是生活随笔為你收集整理的OpenCV AprilTags 识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ROS 分布式通信
- 下一篇: 树莓派 4B安装ubuntu18.04与