Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def add_board(image, predefined_dict_name, start_id):
- camera_matrix = np.array([[1000, 0, 1000],
- [0, 1000, 100],
- [0, 0, 1]], dtype=np.float32)
- dist_coeffs = np.array([0, 0, 0, 0, 0], dtype=np.float32)
- dictionary = aruco.getPredefinedDictionary(predefined_dict_name)
- board = aruco.GridBoard_create(2, 2, 0.06, 0.02, dictionary, start_id)
- corners, ids, _ = aruco.detectMarkers(image, dictionary)
- if ids is not None and len(ids) > 0:
- rvecs, tvecs, _ = aruco.estimatePoseSingleMarkers(corners, 0.03, camera_matrix, dist_coeffs)
- # image = aruco.drawDetectedMarkers(image, corners, ids)
- # for i in range(len(ids)):
- # aruco.drawAxis(image, camera_matrix, dist_coeffs, rvecs[i], tvecs[i], 0.02)
- valid, rvec, tvec = aruco.estimatePoseBoard(corners, ids, board, camera_matrix, dist_coeffs)
- if valid > 0:
- rmat = cv2.Rodrigues(rvec)[0]
- euler_angles = euler_angles_from_r_matrix(rmat)
- # print(euler_angles)
- # print(euler_angles)
- # z_vec = np.array([[0, 0, 1]], dtype=np.float32)
- # angle_cosine = np.dot(tvec.reshape((3,)), z_vec.reshape((3,))) / (np.linalg.norm(tvec))
- # print(angle_cosine)
- # https://stackoverflow.com/questions/2403886/camera-translation-vector-relation-to-rotation-matrix
- # print(np.matmul(rmat.T, tvec))
- aruco.drawAxis(image, camera_matrix, dist_coeffs, rvec, tvec, 0.15)
- board_name = 'noName'
- if start_id == 0:
- board_name = 'Source'
- if start_id == 4:
- board_name = 'Destination'
- print(board_name)
- # print()
- length = 0.17
- axis_points = np.array([[0, 0, 0],
- [length, 0, 0],
- [0, length, 0],
- [0, 0, length]], dtype=np.float32)
- image_points = np.array([], dtype=np.float32)
- image_points, _ = cv2.projectPoints(axis_points, rvec, tvec, camera_matrix, dist_coeffs, image_points)
- origin = (int(image_points[0][0][0]) - 100, image_points[0][0][1])
- cv2.putText(image, board_name, org=origin, fontFace=1,
- fontScale=3.0, color=(255, 255, 255), thickness=4)
- colors = ((0, 0, 255), (0, 255, 0), (255, 0, 0))
- texts = ('x', 'y', 'z')
- coors_norm = np.squeeze(np.copy(image_points))
- coors_norm[:, 0] = coors_norm[:, 0] / image.shape[0]
- coors_norm[:, 1] = coors_norm[:, 1] / image.shape[1]
- ys = coors_norm[2, :]
- axis_origin = coors_norm[0]
- threshold = 0.01
- # if ys[1] > (axis_origin[1] + threshold):
- # if ys[0] < (axis_origin[0] - threshold):
- # print('go right')
- # if ys[0] > (axis_origin[0] + threshold):
- # print('go left')
- # else:
- # print('go ahead')
- # else:
- # pass
- if ys[0] < (axis_origin[0] - threshold):
- print('go right')
- elif ys[0] > (axis_origin[0] + threshold):
- print('go left')
- else:
- if ys[1] < axis_origin[1]:
- print('go right')
- else:
- print('go ahead')
- for i, (text, color) in enumerate(zip(texts, colors)):
- # print(color)
- coor = (image_points[i + 1][0][0], image_points[i + 1][0][1])
- cv2.putText(image, text, org=coor, fontFace=1,
- fontScale=3.0, color=color, thickness=4)
- return image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement