|
1 | 1 | """Module to segment images based on a segmentation template and a set of labels."""
|
| 2 | + |
2 | 3 | import cv2 as cv
|
3 | 4 | import numpy as np
|
4 | 5 | import json
|
@@ -31,7 +32,9 @@ def crop_zeros(image: np.ndarray) -> np.ndarray:
|
31 | 32 | ] # inclusive
|
32 | 33 |
|
33 | 34 |
|
34 |
| -def segment_by_mask_then_crop(raw_image: np.ndarray, segmentation_template: np.ndarray, labels: list[dict[str, str]], debug: bool) -> dict[str, np.ndarray]: |
| 35 | +def segment_by_mask_then_crop( |
| 36 | + raw_image: np.ndarray, segmentation_template: np.ndarray, labels: list[dict[str, str]], debug: bool |
| 37 | +) -> dict[str, np.ndarray]: |
35 | 38 | """Segments a raw image based on a color mask in the segmentation template, and then crops the resulting regions to remove zero (black) areas.
|
36 | 39 |
|
37 | 40 | Args:
|
@@ -75,7 +78,9 @@ def segment_by_mask_then_crop(raw_image: np.ndarray, segmentation_template: np.n
|
75 | 78 | return segments
|
76 | 79 |
|
77 | 80 |
|
78 |
| -def segment_by_color_bounding_box(raw_image: np.ndarray, segmentation_template: np.ndarray, labels: list[dict[str, str]], debug: bool) -> dict[str, np.ndarray]: |
| 81 | +def segment_by_color_bounding_box( |
| 82 | + raw_image: np.ndarray, segmentation_template: np.ndarray, labels: list[dict[str, str]], debug: bool |
| 83 | +) -> dict[str, np.ndarray]: |
79 | 84 | """Segments a raw image by detecting colored boundary boxes in the segmentation template.
|
80 | 85 |
|
81 | 86 | Args:
|
@@ -119,6 +124,7 @@ class ImageSegmenter:
|
119 | 124 | `segment_by_mask_then_crop` or `segment_by_color_bounding_box`.
|
120 | 125 | debug (bool): If `True`, saves debug images and prints additional information.
|
121 | 126 | """
|
| 127 | + |
122 | 128 | def __init__(
|
123 | 129 | self,
|
124 | 130 | segmentation_function=segment_by_mask_then_crop,
|
@@ -152,7 +158,9 @@ def segment(
|
152 | 158 | """
|
153 | 159 | return self.segmentation_function(raw_image, segmentation_template, labels, self.debug)
|
154 | 160 |
|
155 |
| - def load_and_segment(self, raw_image_path: str, segmentation_template_path: str, labels_path: str) -> dict[str, np.ndarray]: |
| 161 | + def load_and_segment( |
| 162 | + self, raw_image_path: str, segmentation_template_path: str, labels_path: str |
| 163 | + ) -> dict[str, np.ndarray]: |
156 | 164 | """Loads image files and labels from specified paths, and then segments the image.
|
157 | 165 |
|
158 | 166 | Args:
|
|
0 commit comments