Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. def add_board(image, predefined_dict_name, start_id):
  2.     camera_matrix = np.array([[1000, 0, 1000],
  3.                              [0, 1000, 100],
  4.                              [0, 0, 1]], dtype=np.float32)
  5.     dist_coeffs = np.array([0, 0, 0, 0, 0], dtype=np.float32)
  6.  
  7.     dictionary = aruco.getPredefinedDictionary(predefined_dict_name)
  8.     board = aruco.GridBoard_create(2, 2, 0.06, 0.02, dictionary, start_id)
  9.  
  10.     corners, ids, _ = aruco.detectMarkers(image, dictionary)
  11.  
  12.     if ids is not None and len(ids) > 0:
  13.         rvecs, tvecs, _ = aruco.estimatePoseSingleMarkers(corners, 0.03, camera_matrix, dist_coeffs)
  14.  
  15.         # image = aruco.drawDetectedMarkers(image, corners, ids)
  16.         # for i in range(len(ids)):
  17.         #     aruco.drawAxis(image, camera_matrix, dist_coeffs, rvecs[i], tvecs[i], 0.02)
  18.  
  19.         valid, rvec, tvec = aruco.estimatePoseBoard(corners, ids, board, camera_matrix, dist_coeffs)
  20.  
  21.         if valid > 0:
  22.             rmat = cv2.Rodrigues(rvec)[0]
  23.  
  24.             euler_angles = euler_angles_from_r_matrix(rmat)
  25.             # print(euler_angles)
  26.             # print(euler_angles)
  27.             # z_vec = np.array([[0, 0, 1]], dtype=np.float32)
  28.             # angle_cosine = np.dot(tvec.reshape((3,)), z_vec.reshape((3,))) / (np.linalg.norm(tvec))
  29.             # print(angle_cosine)
  30.  
  31.             # https://stackoverflow.com/questions/2403886/camera-translation-vector-relation-to-rotation-matrix
  32.             # print(np.matmul(rmat.T, tvec))
  33.  
  34.             aruco.drawAxis(image, camera_matrix, dist_coeffs, rvec, tvec, 0.15)
  35.  
  36.             board_name = 'noName'
  37.  
  38.             if start_id == 0:
  39.                 board_name = 'Source'
  40.             if start_id == 4:
  41.                 board_name = 'Destination'
  42.  
  43.             print(board_name)
  44.             # print()
  45.  
  46.             length = 0.17
  47.  
  48.             axis_points = np.array([[0, 0, 0],
  49.                                     [length, 0, 0],
  50.                                     [0, length, 0],
  51.                                     [0, 0, length]], dtype=np.float32)
  52.  
  53.             image_points = np.array([], dtype=np.float32)
  54.  
  55.             image_points, _ = cv2.projectPoints(axis_points, rvec, tvec, camera_matrix, dist_coeffs, image_points)
  56.  
  57.             origin = (int(image_points[0][0][0]) - 100, image_points[0][0][1])
  58.  
  59.             cv2.putText(image, board_name, org=origin, fontFace=1,
  60.                         fontScale=3.0, color=(255, 255, 255), thickness=4)
  61.  
  62.             colors = ((0, 0, 255), (0, 255, 0), (255, 0, 0))
  63.             texts = ('x', 'y', 'z')
  64.  
  65.             coors_norm = np.squeeze(np.copy(image_points))
  66.             coors_norm[:, 0] = coors_norm[:, 0] / image.shape[0]
  67.             coors_norm[:, 1] = coors_norm[:, 1] / image.shape[1]
  68.  
  69.             ys = coors_norm[2, :]
  70.             axis_origin = coors_norm[0]
  71.  
  72.             threshold = 0.01
  73.  
  74.             # if ys[1] > (axis_origin[1] + threshold):
  75.             #     if ys[0] < (axis_origin[0] - threshold):
  76.             #         print('go right')
  77.             #     if ys[0] > (axis_origin[0] + threshold):
  78.             #         print('go left')
  79.             #     else:
  80.             #         print('go ahead')
  81.             # else:
  82.             #     pass
  83.  
  84.             if ys[0] < (axis_origin[0] - threshold):
  85.                 print('go right')
  86.             elif ys[0] > (axis_origin[0] + threshold):
  87.                 print('go left')
  88.             else:
  89.                 if ys[1] < axis_origin[1]:
  90.                     print('go right')
  91.                 else:
  92.                     print('go ahead')
  93.  
  94.             for i, (text, color) in enumerate(zip(texts, colors)):
  95.                 # print(color)
  96.                 coor = (image_points[i + 1][0][0], image_points[i + 1][0][1])
  97.  
  98.                 cv2.putText(image, text, org=coor, fontFace=1,
  99.                             fontScale=3.0, color=color, thickness=4)
  100.  
  101.     return image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement