I'm working on a small project in which I have to detect aruco marker from the image then make 3d cylinder or cube covering all white boxes.
In below code their is a detect_marker() function in which I have to return Aruco_list which I don't know how to do. If there is any tutorial from were I can learn this you can suggest me.
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()
I'm very new to these stuffs so it's seems to a bit difficult for me. Please help me out.
Below is the ArUco code on which I have to perform the above task