Aruco label/QR code generation opencv-python

Recently, when I was working on Aruco, I found that life and death failed when generating tags. Later, I found out that it was a version problem.

Because when I run the pip install opencv-contrib-python command directly, I found that the directly installed version is opencv-contrib-python==4.7.0.72

The codes for generating tags under version 4.6 and version 4.7 are provided below

1, opencv-contrib-python==4.6 version of the code

2. opencv-contrib-python==4.7 version code

3. Algorithm summary comparison

When the version is different, the corresponding function needs to be replaced

Functions of version 4.6

Functions in version 4.7

1

cv2.aruco.GridBoard_create

cv2.aruco.GridBoard

2

cv2.aruco.Dictionary_get

cv2.aruco.getPredefinedDictionary

3

cv2.aruco_GridBoard.draw

cv2.aruco.Board.generateImage

1, opencv-contrib-python==4.6 version of the code

import cv2
import numpy as np

markersX = 1           #X轴上标记的数量
markersY = 1           #EY轴上标记的数量   本例生成2行6列的格子
markerLength = 100#标记的长度,单位是像素
markerSeparation = 20#每个标记之间的间隔,单位像素
margins = markerSeparation #标记与边界之间的间隔
borderBits = 10 #标记的边界所占的bit位数
showImage = True


width = markersX * (markerLength + markerSeparation) - markerSeparation + 2 * margins
height =markersY * (markerLength + markerSeparation) - markerSeparation + 2 * margins


dictionary = cv2.aruco.Dictionary_get( cv2.aruco.DICT_6X6_250)
board = cv2.aruco.GridBoard_create(markersX, markersY, float(markerLength),float(markerSeparation), dictionary)
print(cv2.aruco_GridBoard.getGridSize(board))
#根据aruco的宽度自动生成合适的图片尺寸
img= cv2.aruco_GridBoard.draw(board,(width,height),1)
cv2.imwrite('frame.png', img)

The result of running the above code is as follows 

2, opencv-contrib-python==4.7 version of the code

The code below is modified from the code above

import cv2
import numpy as np


markersX = 1           #X轴上标记的数量
markersY = 1            #EY轴上标记的数量   本例生成1x1的棋盘
markerLength = 100#标记的长度,单位是像素
markerSeparation = 20#每个标记之间的间隔,单位像素
margins = markerSeparation #标记与边界之间的间隔
borderBits = 10 #标记的边界所占的bit位数
showImage = True


width = markersX * (markerLength + markerSeparation) - markerSeparation + 2 * margins
height =markersY * (markerLength + markerSeparation) - markerSeparation + 2 * margins


dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_7X7_250)

#生成CharucoBoard
#board = cv2.aruco.CharucoBoard((3, 3), 0.015, 0.011, dictionary)
#生成aruco的格子,或者生成1个aruco,(1, 1)表示生成了1行1列
board = cv2.aruco.GridBoard((1, 1), 0.015, 0.011, dictionary)
#board = cv2.aruco.GridBoard((markersX, markersY), float(markerLength),float(markerSeparation), dictionary)
print(cv2.aruco.Board.getIds(board))


#	outSize[, img[, marginSize[, borderBits]
#img = cv2.aruco.Board.generateImage( board,(300,400), 0, 1)
img = cv2.aruco.Board.generateImage( board,(width,height), 0, 1)
cv2.imwrite('frame.png', img)

The result of running the above code is shown in the figure below

The above code also generates CharucoBoard code, which can be generated on demand by removing the comments

Pay attention to distinguish between three formats, see the figure below

 3. Algorithm summary comparison

When the version is different, the corresponding function needs to be replaced

Functions of version 4.6

Functions in version 4.7

1

cv2.aruco.GridBoard_create

cv2.aruco.GridBoard

2

cv2.aruco.Dictionary_get

cv2.aruco.getPredefinedDictionary

3

cv2.aruco_GridBoard.draw

cv2.aruco.Board.generateImage

 In addition, special attention should be paid to the

In version 4.6, opencv-contrib-python needs to be installed

In version 4.7, the aruco module has been put in the main library, so you only need to install opencv-python, and you don’t need to quote aruco separately when referencing, just refer to cv2

Copyright Statement: Please do not reproduce without permission

Guess you like

Origin blog.csdn.net/sunnyrainflower/article/details/131110680