gpt4 book ai didi

python - 如何在 ArUco 标记上制作 3D 圆柱体/立方体?

转载 作者:太空宇宙 更新时间:2023-11-03 21:32:58 26 4
gpt4 key购买 nike

我正在开发一个小项目,其中我必须从图像中检测 aruco 标记,然后制作覆盖所有白色框的 3d 圆柱体或立方体。

在下面的代码中,它们是一个 detector_marker() 函数,其中我必须返回 Aruco_list,但我不知道该怎么做。如果有任何教程可以让我学习,您可以推荐我。


def getCameraMatrix():
with np.load('System.npz') as X:
camera_matrix, dist_coeff, _, _ = [X[i] for i in ('mtx','dist','rvecs','tvecs')]
return camera_matrix, dist_coeff

"""
Function Name : sin()
Input: angle (in degrees)
Output: value of sine of angle specified
Purpose: Returns the sine of angle specified in degrees
"""
def sin(angle):
return math.sin(math.radians(angle))

"""
Function Name : cos()
Input: angle (in degrees)
Output: value of cosine of angle specified
Purpose: Returns the cosine of angle specified in degrees
"""
def cos(angle):
return math.cos(math.radians(angle))



################################################################################


"""
Function Name : detect_markers()
Input: img (numpy array), camera_matrix, dist_coeff
Output: aruco list in the form [(aruco_id_1, centre_1, rvec_1, tvec_1),(aruco_id_2,
centre_2, rvec_2, tvec_2), ()....]
Purpose: This function takes the image in form of a numpy array, camera_matrix and
distortion matrix as input and detects ArUco markers in the image. For each
ArUco marker detected in image, paramters such as ID, centre coord, rvec
and tvec are calculated and stored in a list in a prescribed format. The list
is returned as output for the function
"""
def detect_markers(img, camera_matrix, dist_coeff):
markerLength = 100
aruco_list = []
######################## INSERT CODE HERE ########################


##################################################################
return aruco_list

"""
Function Name : drawAxis()
Input: img (numpy array), aruco_list, aruco_id, camera_matrix, dist_coeff
Output: img (numpy array)
Purpose: This function takes the above specified outputs and draws 3 mutually
perpendicular axes on the specified aruco marker in the image and
returns the modified image.
"""
def drawAxis(img, aruco_list, aruco_id, camera_matrix, dist_coeff):
for x in aruco_list:
if aruco_id == x[0]:
rvec, tvec = x[2], x[3]
markerLength = 100
m = markerLength/2
pts = np.float32([[-m,m,0],[m,m,0],[-m,-m,0],[-m,m,m]])
pt_dict = {}
imgpts, _ = cv2.projectPoints(pts, rvec, tvec, camera_matrix, dist_coeff)
for i in range(len(pts)):
pt_dict[tuple(pts[i])] = tuple(imgpts[i].ravel())
src = pt_dict[tuple(pts[0])]; dst1 = pt_dict[tuple(pts[1])];
dst2 = pt_dict[tuple(pts[2])]; dst3 = pt_dict[tuple(pts[3])];

img = cv2.line(img, src, dst1, (0,255,0), 4)
img = cv2.line(img, src, dst2, (255,0,0), 4)
img = cv2.line(img, src, dst3, (0,0,255), 4)
return img

"""
Function Name : drawCube()
Input: img (numpy array), aruco_list, aruco_id, camera_matrix, dist_coeff
Output: img (numpy array)
Purpose: This function takes the above specified outputs and draws a cube
on the specified aruco marker in the image and returns the modified
image.
"""
def drawCube(img, ar_list, ar_id, camera_matrix, dist_coeff):
for x in ar_list:
if ar_id == x[0]:
rvec, tvec = x[2], x[3]
markerLength = 100
m = markerLength/2
######################## INSERT CODE HERE ########################


##################################################################
return img

"""
Function Name : drawCylinder()
Input: img (numpy array), aruco_list, aruco_id, camera_matrix, dist_coeff
Output: img (numpy array)
Purpose: This function takes the above specified outputs and draws a cylinder
on the specified aruco marker in the image and returns the modified
image.
"""
def drawCylinder(img, ar_list, ar_id, camera_matrix, dist_coeff):
for x in ar_list:
if ar_id == x[0]:
rvec, tvec = x[2], x[3]
markerLength = 100
radius = markerLength/2; height = markerLength*1.5
######################## INSERT CODE HERE ########################


##################################################################
return img

"""
MAIN CODE
This main code reads images from the test cases folder and converts them into
numpy array format using cv2.imread. Then it draws axis, cubes or cylinders on
the ArUco markers detected in the images.
"""


if __name__=="__main__":
cam, dist = getCameraMatrix()
img = cv2.imread("..\\TestCases\\image_1.jpg")
aruco_list = detect_markers(img, cam, dist)
for i in aruco_list:
img = drawAxis(img, aruco_list, i[0], cam, dist)
## img = drawCube(img, aruco_list, i[0], cam, dist)
## img = drawCylinder(img, aruco_list, i[0], cam, dist)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

我对这些东西很陌生,所以这对我来说似乎有点困难。请帮帮我。

下面是我必须执行上述任务的 ArUco 代码

最佳答案

要检测 aruco 标记,您需要图像或框架、用于在您的情况下创建标记的 aruco 字典或 aruco 原始版本或此处的 5x5 更多信息:https://docs.opencv.org/trunk/d5/d0b/classcv_1_1aruco_1_1Dictionary.html

然后你可以使用这个函数来检测aruco标记:

aruco_dict = aruco.Dictionary_get(aruco.YOUR ARUCO DICTIONARY HERE)
parameters = aruco.DetectorParameters_create()

corners, ids, rejectedImgPoints = cv2.aruco.detectMarkers(img, aruco_dict, parameters=parameters)

id 是检测到的 arucos 的列表,角是那里的位置。

关于python - 如何在 ArUco 标记上制作 3D 圆柱体/立方体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53424332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com