generate_arucoboard module

This script can be used to generate a Aruco marker board. This board is used in the camera pose estimation.

Note

Printing instructions:

  • Make sure Scale to fit is selected.
  • Verify the size of the markers after the board is printed. If size does not match change the settings below or in the detection algorithm.

Source code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
# Main python packages
import os
import pickle
import cv2
from cv2 import aruco
import datetime

# Panda_autograsp modules, msgs and srvs
from panda_autograsp import Logger

# Get required paths
SAVE_DIR_PATH = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))

# Create script logger
script_logger = Logger.get_logger("generate_arucoboard.py")

#################################################
# Script settings ###############################
#################################################

MARKERS_X = 4
MARKERS_Y = 6
MARKER_LENGTH = 0.032  # [M]
MARKER_SEPARATION = 0.009  # [M]
ARUCO_DICT_TYPE = aruco.DICT_6X6_50  # [ROWSxCOLUMNS_LIBRARY_SIZE]
IM_OUT_SIZE = (1100, 1681)  # Pixel size == Pattern size
MARGIN_SIZE = 0

#################################################
# Main script ###################################
#################################################
if __name__ == "__main__":

    # set root logger format
    root_logger = Logger.get_logger(
        log_file=os.path.abspath(
            os.path.join(
                os.path.dirname(os.path.realpath(__file__)),
                "..",
                "logs/generate_arucoboard.log",
            )
        )
    )

    # Welcome message
    print(
        "== Generate arucoboard script ==\n"
        "This script can be used to generate "
        "a Aruco marker board. This board is used "
        "in the camera pose estimation.\n"
    )

    # Save config settings
    config_dict = {
        "MARKERS_X": MARKERS_X,
        "MARKERS_Y": MARKERS_Y,
        "MARKER_LENGTH": 0.032,
        "MARKER_SEPARATION": 0.009,
        "ARUCO_DICT_TYPE": ARUCO_DICT_TYPE,
        "IM_OUT_SIZE": IM_OUT_SIZE,
        "MARGIN_SIZE": MARGIN_SIZE,
    }
    config_save_str = os.path.abspath(
        os.path.join(SAVE_DIR_PATH, "../cfg/_cfg/aruco_config.dict")
    )
    with open(config_save_str, "wb") as config_dict_file:
        pickle.dump(config_dict, config_dict_file)

    # Create aruco gridboard
    ARUCO_DICT = aruco.Dictionary_get(ARUCO_DICT_TYPE)
    gridboard = aruco.GridBoard_create(
        markersX=MARKERS_X,
        markersY=MARKERS_Y,
        markerLength=MARKER_LENGTH,
        markerSeparation=MARKER_SEPARATION,
        dictionary=ARUCO_DICT,
    )

    # Create an image from the gridboard
    gridboard_img = gridboard.draw(
        outSize=IM_OUT_SIZE, marginSize=MARGIN_SIZE, borderBits=1
    )

    # Display and save gridboard image
    cv2.namedWindow("aruco-board")
    cv2.imshow("aruco-board", gridboard_img)
    while True:
        k = cv2.waitKey(100)

        # Break if someone presses q or esc
        if (k == 27) or (k == ord("q")):
            print("Closing window")
            break

        # Break if window is closed
        if cv2.getWindowProperty("aruco-board", cv2.WND_PROP_VISIBLE) < 1:
            break
    cv2.destroyAllWindows()

    # Save generated Arucoboard
    time_str = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    img_save_dir = os.path.abspath(os.path.join(SAVE_DIR_PATH, ("../data/calib")))
    img_save_pth = os.path.abspath(
        os.path.join(img_save_dir, "arucoboard" + "_" + time_str + ".jpg")
    )
    if not os.path.exists(img_save_dir):
        os.makedirs(img_save_dir)
    cv2.imwrite(img_save_pth, gridboard_img)
    print("Aruco board saved to %s" % img_save_pth)