Use aruco to estimate the world position of the camera (1)

1. Introduction to ArUco

ArUco is an open source miniature reality augmentation library, which has been integrated in OpenCV3.0 and above. It is not only used for reality augmentation, but also used to realize some machine vision applications. Boston Dynamics in the above picture is also This method was used for visual positioning of Atlas.

Aruco markers are binary square fiducial markers that can be used for camera pose estimation. Its main advantages are that detection is simple, fast, and robust. An ArUco marker is a square marker consisting of a wide black border and an internal binary matrix that determines its identifier (id). The black border of the aruco marker facilitates its fast detection in images, and the internal binary code is used to identify the marker and provide error detection and correction. The size of the aruco mark determines the size of the internal matrix, for example, a mark with a size of 4x4 consists of 16 binary numbers.

In layman's terms, the aruco mark is actually a kind of code, which is similar to the QR code in our daily life, but due to the different coding methods, the way of storing information, capacity, etc. are different, so in There are also differences at the application level. Since a single aruco mark can provide sufficient correspondence, for example, there are four distinct corners and internal binary codes, so aruco marks are widely used to increase the amount of information when mapping from a two-dimensional world to a three-dimensional world. The projection relationship between the three-dimensional world and the three-dimensional world, so as to realize pose estimation, camera correction and other applications.

The ArUco module in OpenCV includes the creation and detection of aruco markers, as well as related APIs for using aruco markers for applications such as pose estimation and camera correction, and also provides marker boards, etc.

2. Creation and detection of ArUco markers

When creating an aruco mark, you first need to specify a dictionary. This dictionary indicates the size, encoding, etc. of the created aruco mark. We use API: getPredefinedDictionary() to declare the dictionary we use.

In OpenCV, a variety of predefined dictionaries are provided. We can check which predefined dictionaries are available through PREDEFINED_DICTIONARY_NAME. And the dictionary name indicates the number and size of the aruco marks of the dictionary, for example, DICT_7X7_50 represents a dictionary containing 50 kinds of 7x7 bit marks.

Online aruco marker generator: https://chev.me/arucogen/

Or https://www.2weima.com/aruco.html

Created using python

import cv2 as cv
import numpy as np

# 创建
dictionary = cv.aruco.getPredefinedDictionary(dict=cv.aruco.DICT_6X6_250)
#dictionary = cv2.aruco.Dictionary_get(cv2.aruco.DICT_6X6_250)
img = cv.aruco.drawMarker(dictionary, id=23, sidePixels=200, borderBits=1)
# id         指定该Marker在字典中的索引ID,该例中合法的ID为[0, 249]
# sidePixels 指定输出的Marker图像的尺寸,单位是像素,该例中为(200,200)
#            如果使用DICT_6X6_250,则编码区域被划分为6X6个等大小的模块
#            参数borderBits=1,所以整个标志块区域被划分为8X8个等大小的模块
#            模块的尺寸必须大于一个像素。因此该例中,此参数最小值为8
# borderBits 指定编码区域到标志块区域的距离。单位是编码模块。取值必须大于等于1
print(img.shape)
cv.imshow("Aruco Marker", img)
cv.waitKey()
cv2.imwrite("marker"+str(id)+".png", markerImage)

create and detect

# 创建图像
img_detected = np.ones((800, 600), dtype=np.uint8) * 255
img_detected[50:250, 50:250] = cv.aruco.drawMarker(dictionary, id=23, sidePixels=200, borderBits=1)
img_detected[350:400, 350:400] = cv.aruco.drawMarker(dictionary, id=18, sidePixels=50, borderBits=1)
img_detected[450:500, 450:500] = cv.aruco.drawMarker(dictionary, id=33, sidePixels=50, borderBits=1)
cv.imshow("img_detected", img_detected)
cv.waitKey()

corners, ids, rejectedImgPoints = cv.aruco.detectMarkers(image=img_detected,
                                                         dictionary=dictionary,
                                                         parameters=None)
img_color = cv.cvtColor(img_detected, cv.COLOR_GRAY2BGR)
cv.aruco.drawDetectedMarkers(img_color, corners, ids)
cv.imshow("DectectedMarker", img_color)
cv.waitKey()

Guess you like

Origin blog.csdn.net/Cretheego/article/details/129653721