# Estimate pose of each marker and return the values rvec and tvec---(different from those of camera coefficients)rvec, tvec, markerPoints = cv2.aruco.estimatePoseSingleMarkers(corners[i], 0.02, matrix_coefficients, distortion_coefficients)# Draw a square around the markerscv2.aruco.drawDetectedMarkers(frame, corners)# Draw Axiscv2.aruco.drawAxis(frame, matrix_coefficients, distortion_coefficients, rvec, tvec, 0.01)
deffind_qr(img,k,distort): qr = cv2.QRCodeDetector() ret_qr, points = qr.detect(img) img_w_frame = copy.deepcopy(img)if ret_qr: axis_points, rvec, tvec =get_qr_coords(k, distort, points)#BGR color format colors = [(255,0,0), (0,255,0), (0,0,255), (0,0,0)]#check axes points are projected to camera view.iflen(axis_points)>0: axis_points = axis_points.reshape((4,2)) origin = (int(axis_points[0][0]),int(axis_points[0][1]) )for p, c inzip(axis_points[1:], colors[:3]): p = (int(p[0]),int(p[1]))#Sometimes qr detector will make a mistake and projected point will overflow integer value. We skip these cases. if origin[0]>5*img.shape[1]or origin[1]>5*img.shape[1]:breakif p[0]>5*img.shape[1]or p[1]>5*img.shape[1]:break cv2.line(img_w_frame, origin, p, c, 5)return img_w_frame
defget_qr_coords(k,distort,points):## from https://github.com/TemugeB/QR_code_orientation_OpenCV/blob/main/run_qr.py#Selected coordinate points for each corner of QR code. qr_edges = np.array([[0,0,0], [0,1,0], [1,1,0], [1,0,0]], dtype ='float32').reshape((4,1,3))#determine the orientation of QR code coordinate system with respect to camera coorindate system. ret, rvec, tvec = cv2.solvePnP(qr_edges, points, np.reshape(k, (3,3)), np.array(distort))#Define unit xyz axes. These are then projected to camera view using the rotation matrix and translation vector. unitv_points = np.array([[0,0,0], [1,0,0], [0,1,0], [0,0,1]], dtype ='float32').reshape((4,1,3))if ret: points, jac = cv2.projectPoints(unitv_points, rvec, tvec, np.reshape(k, (3,3)), np.array(distort))return points, rvec, tvec#return empty arrays if rotation and translation values not foundelse:return [], [], []