ArUco Markers for Augmented Reality

# Load the pre-defined ArUco Marker dictionary that has 250 6x6 marker patterns.
import cv2
dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)

# Generate markers with IDs: 23, 25, 30, and 33.
marker_image1 = cv2.aruco.drawMarker(dictionary, 23, 200)
marker_image2 = cv2.aruco.drawMarker(dictionary, 25, 200)
marker_image3 = cv2.aruco.drawMarker(dictionary, 30, 200)
marker_image4 = cv2.aruco.drawMarker(dictionary, 33, 200)

# Display the markers.
plt.figure(figsize = [18, 10])
plt.subplot(1,4,1); plt.imshow(marker_image1); plt.title("Marker ID: 23"); plt.axis('off')
plt.subplot(1,4,2); plt.imshow(marker_image2); plt.title("Marker ID: 25"); plt.axis('off')
plt.subplot(1,4,3); plt.imshow(marker_image3); plt.title("Marker ID: 30"); plt.axis('off')
plt.subplot(1,4,4); plt.imshow(marker_image4); plt.title("Marker ID: 33"); plt.axis('off')

# Save the generated markers.
cv2.imwrite("marker_23.png", marker_image1)
cv2.imwrite("marker_25.png", marker_image2)
cv2.imwrite("marker_30.png", marker_image3)
cv2.imwrite("marker_33.png", marker_image4)

# Read input image with the markers.
frame = cv2.imread('marker_23_printed.png')

# Detect the markers in the destination image.
corners, ids, rejected = cv2.aruco.detectMarkers(frame, dictionary)

cv2.aruco.drawDetectedMarkers(frame, corners, ids)
plt.figure(figsize = [10,10])
plt.axis('off')
plt.imshow(frame[:,:,::-1])

# Upper-left corner of ROI.
index = np.squeeze(np.where(ids == 23))
ref_pt1 = np.squeeze(corners[index[0]])[0]

References