|
8 | 8 |
|
9 | 9 |
|
10 | 10 | class MCCardDetector(BaseCardDetector): |
| 11 | + """MCCardDetector implements card detection using OpenCV's ColorChecker detector (cv2.mcc). |
| 12 | +
|
| 13 | + This class detects ColorChecker cards and their color patches in an image using OpenCV's |
| 14 | + built-in MCC ([Macbeth ColorChecker](https://docs.opencv.org/4.11.0/dd/d19/group__mcc.html)) detection module. |
| 15 | + It provides methods to detect cards, extract patch coordinates, and visualize detections. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + use_gpu : bool, optional |
| 20 | + Whether to use GPU for detection (not used in OpenCV MCC), by default False. |
| 21 | + conf_th : float, optional |
| 22 | + Confidence threshold for detection filtering, by default 0.15. |
| 23 | + """ |
| 24 | + |
11 | 25 | def __init__(self, use_gpu: bool = False, conf_th: float = 0.15) -> None: |
| 26 | + """ |
| 27 | + Initialize the MCCardDetector. |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + use_gpu : bool, optional |
| 32 | + Whether to use GPU for detection (not used in OpenCV MCC), by default False. |
| 33 | + conf_th : float, optional |
| 34 | + Confidence threshold for detection filtering, by default 0.15. |
| 35 | + """ |
12 | 36 | self.use_gpu = use_gpu |
13 | 37 | self.conf_th = conf_th |
14 | 38 |
|
15 | | - def detect(self, image: np.ndarray, conf: float = 0.15) -> DetectionResult: |
| 39 | + def detect(self, image: np.ndarray, conf: float | None = None) -> DetectionResult: |
| 40 | + """ |
| 41 | + Detect ColorChecker cards and their color patches in the input image. |
| 42 | +
|
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + image : np.ndarray |
| 46 | + Input image (BGR) in which to detect ColorChecker cards. |
| 47 | + conf : float, optional |
| 48 | + Confidence threshold for detection filtering, by default 0.15. |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + DetectionResult |
| 53 | + A dataclass containing detected segments (card and patch polygons), |
| 54 | + confidence scores, and class IDs. Boxes are not used in this case (None). |
| 55 | + """ |
| 56 | + |
| 57 | + if conf is None: |
| 58 | + conf = self.conf_th |
| 59 | + |
16 | 60 | ls_segments = [] |
17 | 61 | ls_class_ids = [] |
18 | 62 | ls_scores = [] |
@@ -63,6 +107,21 @@ def detect(self, image: np.ndarray, conf: float = 0.15) -> DetectionResult: |
63 | 107 | ) |
64 | 108 |
|
65 | 109 | def _extract_chart_coordinates(self, image_copy: np.ndarray, charts_rgb: np.ndarray) -> list[list[tuple[int, int]]]: |
| 110 | + """ |
| 111 | + Extracts the coordinates of color patches from the detected ColorChecker. |
| 112 | +
|
| 113 | + Parameters |
| 114 | + ---------- |
| 115 | + image_copy : np.ndarray |
| 116 | + Copy of the input image for visualization (patch points drawn). |
| 117 | + charts_rgb : np.ndarray |
| 118 | + Array of shape (N, 2) containing the (x, y) coordinates of patch corners. |
| 119 | +
|
| 120 | + Returns |
| 121 | + ------- |
| 122 | + list of list of tuple of int |
| 123 | + List of patches, each patch is a list of 4 (x, y) tuples. |
| 124 | + """ |
66 | 125 | random_color = (np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) |
67 | 126 | ls_chart_coord = [] |
68 | 127 | ls_group_patch = [] |
@@ -94,6 +153,18 @@ def _extract_chart_coordinates(self, image_copy: np.ndarray, charts_rgb: np.ndar |
94 | 153 | return ls_chart_coord |
95 | 154 |
|
96 | 155 | def _draw_box_color_path(self, image_copy: np.ndarray, i: int, group_patch: list[tuple[int, int]]) -> None: |
| 156 | + """ |
| 157 | + Draws a polygon and label for a color patch on the image. |
| 158 | +
|
| 159 | + Parameters |
| 160 | + ---------- |
| 161 | + image_copy : np.ndarray |
| 162 | + Image on which to draw the patch. |
| 163 | + i : int |
| 164 | + Index or label for the patch. |
| 165 | + group_patch : list of tuple of int |
| 166 | + List of 4 (x, y) tuples representing the patch corners. |
| 167 | + """ |
97 | 168 | random_color = (np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) |
98 | 169 | # draw patch |
99 | 170 | cv2.polylines(image_copy, [np.int32(group_patch)], True, random_color, 2) |
|
0 commit comments