Skip to content

Chessboard

ChessboardCorrespondenceFinder(n_rows, n_columns, square_size, flags, fast_check=True, refine=True, window_size=(5, 5), criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))

Bases: CorrespondenceFinderBase

Chessboard pattern correspondence finder.

Initialize a chessboard correspondence finder.

Parameters:

Name Type Description Default
n_rows int

Number of rows in the chessboard pattern.

required
n_columns int

Number of columns in the chessboard pattern.

required
square_size float

Size of a square in the chessboard pattern. Returned object points will be in units of this size.

required
flags int

Flags to pass to cv2.findChessboardCorners.

required
fast_check bool

If True, perform a fast check to see if a chessboard is present in the image. This just adds cv2.CALIB_CB_FAST_CHECK to the flags.

True
refine bool

If True, perform a refinement step on the corner locations.

True
window_size Tuple[int, int]

Size of the window to use for refinement.

(5, 5)
criteria tuple

Criteria to use for refinement.

(TERM_CRITERIA_EPS | TERM_CRITERIA_MAX_ITER, 30, 0.001)
Source code in src/compas_camcal/correspondence/chessboard.py
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
def __init__(
    self,
    n_rows: int,
    n_columns: int,
    square_size: float,
    flags: int,
    fast_check: bool = True,
    refine: bool = True,
    window_size: Tuple[int, int] = (5, 5),
    criteria: tuple = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001),
) -> None:
    """
    Initialize a chessboard correspondence finder.

    Args:
        n_rows: Number of rows in the chessboard pattern.
        n_columns: Number of columns in the chessboard pattern.
        square_size: Size of a square in the chessboard pattern. Returned object points will be in units of this size.
        flags: Flags to pass to cv2.findChessboardCorners.
        fast_check: If True, perform a fast check to see if a chessboard is present in the image. This just adds cv2.CALIB_CB_FAST_CHECK to the flags.
        refine: If True, perform a refinement step on the corner locations.
        window_size: Size of the window to use for refinement.
        criteria: Criteria to use for refinement.
    """
    super().__init__()

    # Chessboard pattern properties
    self._n_rows = n_rows
    self._n_columns = n_columns
    self._square_size = square_size

    # Cached object points for the chessboard pattern, recomputed if any of the above properties change
    self._object_points = None
    self._update_object_points()

    # Flags for cv2.findChessboardCorners
    self.flags = flags

    # Whether to use fast check for cv2.findChessboardCorners
    self.fast_check = fast_check

    # Whether to refine corner locations
    self.refine = refine

    # Window size and termination critera for cv2.cornerSubPix
    self.window_size = window_size
    self.criteria = criteria

n_columns property writable

Number of columns in the chessboard pattern.

n_rows property writable

Number of rows in the chessboard pattern.

pattern_size property writable

Size of the chessboard pattern.

compute_object_points(n_rows, n_columns, square_size) staticmethod

Compute object points for a chessboard pattern.

Source code in src/compas_camcal/correspondence/chessboard.py
63
64
65
66
67
68
69
70
@staticmethod
def compute_object_points(n_rows: int, n_columns: int, square_size: float) -> np.ndarray:
    """
    Compute object points for a chessboard pattern.
    """
    object_points = np.zeros((n_rows * n_columns, 3), dtype=np.float32)
    object_points[:, :2] = np.mgrid[0:n_rows, 0:n_columns].T.reshape(-1, 2) * square_size
    return object_points

find(image)

Find correspondences in an image.

Parameters:

Name Type Description Default
image ndarray

Image to find correspondences in.

required

Returns:

Type Description
Optional[Correspondence]

Correspondence object if found, None otherwise.

Source code in src/compas_camcal/correspondence/chessboard.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def find(self, image: np.ndarray) -> Optional[Correspondence]:
    """
    Find correspondences in an image.

    Args:
        image: Image to find correspondences in.

    Returns:
        Correspondence object if found, None otherwise.
    """
    if image.ndim == 3:  # If image is a color image, convert to grayscale
        self.logger.debug("Detected 3-channel image, converting to grayscale")
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    self.logger.info("Finding chessboard corners")
    ok, corners = cv2.findChessboardCorners(
        image,
        self.pattern_size,
        flags=self.flags | (cv2.CALIB_CB_FAST_CHECK if self.fast_check else 0),
    )

    if not ok:  # No chessboard found
        self.logger.warning("No chessboard corners found")
        return None

    if self.refine:  # Refine corner locations
        self.logger.info("Refining corner locations")
        corners = cv2.cornerSubPix(image, corners, self.window_size, (-1, -1), self.criteria)

    return Correspondence(self._object_points, corners[:, 0, :])