我正在做一个小项目,我必须从图像中检测出aruco标记,然后制作覆盖所有白色盒子的3d圆柱或立方体。
在下面的代码中,它们是一个detect_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()
我对这些东西很陌生,所以对我来说似乎有点困难。请帮帮我。
答案 0 :(得分:0)
要检测需要图像或框架的aruco标记,用于创建标记的aruco词典(视情况而定),aruco原始图像或5x5的其他信息,请点击此处:https://docs.opencv.org/trunk/d5/d0b/classcv_1_1aruco_1_1Dictionary.html < / p>
然后您可以使用此功能来检测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)
ids是检测到的arucos的列表,并且角落处有位置。