Skip to content

CalibrationPattern

CalibrationPattern describes the physical ChArUco board you printed and uses it to detect corners in images. Create one instance per board design and share it across your calibration and detection code.


Example

import cv2
from charucal import CalibrationPattern

pattern = CalibrationPattern(
    width=10,
    height=8,
    square_length=0.115,   # meters — measure your printed board
    marker_length=0.086,   # meters
    aruco_dict=cv2.aruco.DICT_4X4_100,
)

# Detect corners in a single image
grid = pattern.detect(image)   # returns a CalibrationGrid

Choosing the right ArUco dictionary

The dictionary must match the one used when you generated the board.

Dictionary Markers Recommended for
DICT_4X4_50 50 Small boards, few cameras
DICT_4X4_100 100 General use (default)
DICT_5X5_100 100 Larger boards, more robust detection
DICT_6X6_250 250 Very large boards

Pass the constant directly (cv2.aruco.DICT_4X4_100) or its integer value. The CLI also accepts the short string form (4X4_100).


Board validation

Sometimes something that isn't part of the board gets detected as a marker, like a road sign or a pattern on the ground. To filter these out, detect works out where each marker should be based on the layout of the board, and ignores any marker that is too far from that spot. If too many markers have to be ignored, the board is rejected.

pattern = CalibrationPattern(
    width=10,
    height=8,
    square_length=0.115,
    marker_length=0.086,
    aruco_dict=cv2.aruco.DICT_4X4_100,
    max_marker_error=0.005,
    min_inlier_ratio=0.8,
    check_markers=False,
)

max_marker_error sets how far off a marker may be, as a fraction of the image diagonal. The default of 0.005 comes down to about 11 pixels in a 1080p image. If markers near the edges of a wide-angle lens are being ignored, try raising it.

min_inlier_ratio is the share of markers that has to fit. With the default of 0.8, the board is rejected when more than one in five markers is off.

check_markers also turns on OpenCV's own check. It is a lot stricter and often rejects boards that are seen at a steep angle, which is why it is off by default.


Reference

CalibrationPattern

CalibrationPattern(
    width: int,
    height: int,
    square_length: float,
    marker_length: float,
    aruco_dict: int,
    *,
    max_marker_error: float = 0.005,
    min_inlier_ratio: float = 0.8,
    check_markers: bool = False,
)

Represents a calibration pattern.

Initialize the calibration pattern.

Parameters:

Name Type Description Default
width int

The width of the calibration pattern.

required
height int

The height of the calibration pattern.

required
square_length float

The length of the squares on the calibration pattern.

required
marker_length float

The length of the markers on the calibration pattern.

required
aruco_dict int

The dictionary for the calibration pattern.

required
max_marker_error float

How far off a marker may be, as a fraction of the image diagonal.

0.005
min_inlier_ratio float

The share of the detected markers that has to match the board.

0.8
check_markers bool

Whether to also use OpenCV's own marker check.

.. warning:: Enabling check_markers can cause valid markers to be rejected, especially when the board is seen at a steep angle. It is recommended to leave it disabled unless you have a specific reason to enable it.

False

Methods:

Name Description
detect

Detect the ChArUco board in an image, ignoring markers that don't match the board layout.

get_shape

Get the shape of the ChArUco board.

to_grid

Get the grid of the ChArUco board.

Attributes:

Name Type Description
width int

The width of the calibration pattern.

height int

The height of the calibration pattern.

max_marker_error float

How far off a marker may be, as a fraction of the image diagonal.

min_inlier_ratio float

The share of the detected markers that has to match the board.

check_markers bool

Whether OpenCV's own marker check is used as well.

board CharucoBoard

The calibration board.

detector CharucoDetector

The calibration pattern detector.

Source code in src/charucal/pattern.py
def __init__(
    self,
    width: int,
    height: int,
    square_length: float,
    marker_length: float,
    aruco_dict: int,
    *,
    max_marker_error: float = 0.005,
    min_inlier_ratio: float = 0.8,
    check_markers: bool = False,
) -> None:
    """Initialize the calibration pattern.

    :param width: The width of the calibration pattern.
    :param height: The height of the calibration pattern.
    :param square_length: The length of the squares on the calibration pattern.
    :param marker_length: The length of the markers on the calibration pattern.
    :param aruco_dict: The dictionary for the calibration pattern.
    :param max_marker_error: How far off a marker may be, as a fraction of the image diagonal.
    :param min_inlier_ratio: The share of the detected markers that has to match the board.
    :param check_markers: Whether to also use OpenCV's own marker check.

    .. warning::
        Enabling ``check_markers`` can cause valid markers to be rejected, especially when the board is seen
        at a steep angle. It is recommended to leave it disabled unless you have a specific reason to enable it.

    """
    if max_marker_error <= 0.0:
        raise ValueError(f"Expected max_marker_error to be positive, got {max_marker_error}.")

    if not 0.0 < min_inlier_ratio <= 1.0:
        raise ValueError(f"Expected min_inlier_ratio to be between 0.0 and 1.0, got {min_inlier_ratio}.")

    self.width = width
    self.height = height
    self.max_marker_error = max_marker_error
    self.min_inlier_ratio = min_inlier_ratio
    self.check_markers = check_markers

    dictionary = cv2.aruco.getPredefinedDictionary(aruco_dict)
    self.board = cv2.aruco.CharucoBoard(self.get_shape(), square_length, marker_length, dictionary)

    charuco_params = cv2.aruco.CharucoParameters()
    charuco_params.checkMarkers = check_markers
    self.detector = cv2.aruco.CharucoDetector(self.board, charuco_params, cv2.aruco.DetectorParameters())

    marker_ids = self.board.getIds()
    marker_points = self.board.getObjPoints()
    self._marker_points = {int(i): points[:, :2] for i, points in zip(marker_ids, marker_points)}

width instance-attribute

width: int = width

The width of the calibration pattern.

height instance-attribute

height: int = height

The height of the calibration pattern.

max_marker_error instance-attribute

max_marker_error: float = max_marker_error

How far off a marker may be, as a fraction of the image diagonal.

min_inlier_ratio instance-attribute

min_inlier_ratio: float = min_inlier_ratio

The share of the detected markers that has to match the board.

check_markers instance-attribute

check_markers: bool = check_markers

Whether OpenCV's own marker check is used as well.

board instance-attribute

board: CharucoBoard = CharucoBoard(
    get_shape(), square_length, marker_length, dictionary
)

The calibration board.

detector instance-attribute

detector: CharucoDetector = CharucoDetector(
    board, charuco_params, DetectorParameters()
)

The calibration pattern detector.

detect

detect(image: NDArray[uint8]) -> CalibrationGrid

Detect the ChArUco board in an image, ignoring markers that don't match the board layout.

Parameters:

Name Type Description Default
image NDArray[uint8]

The image to detect the ChArUco board in.

required

Returns:

Type Description
CalibrationGrid

The corners and ids of the detected ChArUco board.

Source code in src/charucal/pattern.py
def detect(self, image: npt.NDArray[np.uint8]) -> CalibrationGrid:
    """Detect the ChArUco board in an image, ignoring markers that don't match the board layout.

    :param image: The image to detect the ChArUco board in.
    :return: The corners and ids of the detected ChArUco board.
    """
    _, _, marker_corners, marker_ids = self.detector.detectBoard(image)
    if marker_ids is None or len(marker_ids) == 0:
        raise ValueError("Failed to detect ChArUco board.")

    diagonal = math.hypot(image.shape[0], image.shape[1])
    inlier_corners, inlier_ids = self._filter_markers(marker_corners, marker_ids, diagonal)

    charuco_corners, charuco_ids, _, _ = self.detector.detectBoard(
        image, markerCorners=inlier_corners, markerIds=inlier_ids
    )

    if charuco_corners is None or charuco_ids is None or len(charuco_ids) == 0:
        raise ValueError("Failed to detect ChArUco board.")

    return CalibrationGrid.from_corners(self.get_shape(), charuco_corners, charuco_ids)

get_shape

get_shape() -> tuple[int, int]

Get the shape of the ChArUco board.

Returns:

Type Description
tuple[int, int]

The shape of the ChArUco board.

Source code in src/charucal/pattern.py
def get_shape(self) -> tuple[int, int]:
    """Get the shape of the ChArUco board.

    :return: The shape of the ChArUco board.
    """
    return self.width, self.height

to_grid

to_grid() -> CalibrationGrid

Get the grid of the ChArUco board.

Returns:

Type Description
CalibrationGrid

The grid of the ChArUco board.

Source code in src/charucal/pattern.py
def to_grid(self) -> CalibrationGrid:
    """Get the grid of the ChArUco board.

    :return: The grid of the ChArUco board.
    """
    shape = self.get_shape()
    length = self.board.getSquareLength()

    return CalibrationGrid.from_shape(shape, length)