edge_cropper
data.crop.edge_cropper
Crop image patches along the contour of mask (or polygon)
Imported from DefectGeneration repository. generation/data/crop/edge_cropper.py
BaseCropper
Bases: ABC
get_n_patch
abstractmethod
WindowState
Bases: Enum
relation state enum between center point and target point
WindowManager
Manager for cropping window size, relation and window coordinates.
Attributes:
-
width(int) –window width for image patch.
-
half_width(int) –half of window width for image patch.
-
height(int) –window height for image patch.
-
half_height(int) –half of window height for image patch.
-
strict_inner_patch(bool) –whether not allowing outer area of image.
Source code in SaigeToolkit/data/crop/edge_cropper.py
check_window
check_window(point: Iterable[int], center: Iterable[int]) -> WindowState
check whether target point is inside of window, returns WindowState
Parameters:
-
point(Iterable[int]) –target point to be checked
-
center(Iterable[int]) –reference center point
Returns:
-
WindowState(WindowState) –relation state enum
Source code in SaigeToolkit/data/crop/edge_cropper.py
is_in_window
is target point inside of window
Parameters:
-
point(Iterable[int]) –target point to be checked
-
center(Iterable[int]) –reference center point
Returns:
-
bool(bool) –boolean result
Source code in SaigeToolkit/data/crop/edge_cropper.py
is_in_any_window
is target point inside of any of windows
Parameters:
-
point(Iterable[int]) –target point to be checked
-
center(Iterable[int]) –list of reference center points
Returns:
-
bool(bool) –boolean result
Source code in SaigeToolkit/data/crop/edge_cropper.py
get_coordinates_from_center
get_coordinates_from_center(h_center: int, w_center: int, h: int, w: int) -> Tuple[int, int, int, int]
get left/right top/bottom coordinates from picked center point.
Parameters:
-
h_center(int) –picked center point h-coordinate
-
w_center(int) –picked center point w-coordinate
-
h(int) –image size height
-
w(int) –image size width
Returns:
-
Tuple[int, int, int, int]–Tuple[int, int, int, int]: (crop_left, crop_top, crop_right, crop_bottom)
Source code in SaigeToolkit/data/crop/edge_cropper.py
CenterWithSatellite
dataclass
CenterWithSatellite(point: Iterable[int], window_manager: WindowManager, satellite: Optional[Iterable[int]] = None)
Center point with 'satellites'. 'Satellites' is a point within center point's window, but not the center point. Also intra distance between 'satellites' should shorter than window size.
Attributes:
-
coordinate(Iterable[int]) –center point coordinate
-
window_manager(WindowManager) –WindowManager
-
max_width_distance(int) –max width-wise positive distance of satellites
-
max_height_distance(int) –max height-wise positive distance of satellites
-
min_width_distance(int) –max width-wise negative distance of satellites
-
min_height_distance(int) –max height-wise negative distance of satellites
-
satellite_points(List[Dict[str, any]]) –satellite_points data
Source code in SaigeToolkit/data/crop/edge_cropper.py
calculate_relation_as_satellite
calculate_relation_as_satellite(point: Iterable[int]) -> Union[Dict[str, Any], List[Dict[str, Any]]]
calculate relation between target point and center point. case 1: If target point is outside of center point's window, target point should be considered as next center point candidate.
case 2
Even when target point is in the window, check whether distance between satellites is larger than window size. If so, target point should be considered as next center point candidate.
Parameters:
-
point(Iterable[int]) –target point to be checked
Returns:
-
Union[Dict[str, Any], List[Dict[str, Any]]]–Union[Dict[str, Any], List[Dict[str, Any]]]: Dict[str, Any]: simple relation between center point and target point List[Dict[str, Any]]: reltations between satellite points and target point
Source code in SaigeToolkit/data/crop/edge_cropper.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
_compare_point_and_calculate_error
Refactored from the original code from Defect Generation (Function extracted).
Source code in SaigeToolkit/data/crop/edge_cropper.py
EdgeCropper
EdgeCropper(crop_w: int = 512, crop_h: int = 512, random_sample: bool = False, patch_per_polygon: int = 1, strict_inner_patch: bool = True, ok_patch_prob: float = 0.01)
Bases: BaseCropper
Crop image patches along the contour of mask (or polygon)
Attributes:
-
window_manager(WindowManager) –WindowManager
-
random_sample(bool) –whether randomly select the first point of each polygon
-
patch_per_polygon(bool) –number of repeat for cropping patches around each polygon.
Source code in SaigeToolkit/data/crop/edge_cropper.py
window_manager
instance-attribute
window_manager = WindowManager(crop_w, crop_h, strict_inner_patch)
init_centers
get_n_patch
PolygonCropper crops all polygon areas {self.patch_per_polygon} times per polygons, and also crops random position of image {self.random_patch_per_img} times per images. Therefore, resultant number of patches is weighted-sum result as coded below.
Parameters:
-
polygons(List[ndarray]) –polygons of single image in dataset.
Returns:
-
int(int) –number of patches to be cropped in single image
Source code in SaigeToolkit/data/crop/edge_cropper.py
__call__
__call__(image: Union[Image, ndarray], mask: Union[Image, ndarray], polygons: Optional[List[ndarray]] = None, **kwargs) -> List[dict]
crop image into image patches.
Parameters:
-
image(Union[Image, ndarray]) –original image
-
polygons(List[ndarray], default:None) –polygon data (# of polygons, (4, 2)) polygon should have 4 points
-
mask(Union[Image, ndarray]) –segmentation mask image
Returns:
-
List[dict]–List[dict]: list of cropped image data dict
Source code in SaigeToolkit/data/crop/edge_cropper.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
get_data_dict_from_center_point
get_data_dict_from_center_point(image: Union[Image, ndarray], mask: Union[Image, ndarray], center, **kwargs)
Source code in SaigeToolkit/data/crop/edge_cropper.py
pick_centers_from_polygon
Source code in SaigeToolkit/data/crop/edge_cropper.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
get_proper_point
get_proper_point(state_as_satellite: Union[Dict[str, Any], List[Dict[str, Any]]], must_contain_coordinate: List[Iterable[int]], point_in: Iterable[int], point_out: Iterable[int]) -> Optional[Iterable[int]]
get proper next center point that contains every must_contain_coordinate
Parameters:
-
state_as_satellite(Union[Dict[str, Any], List[Dict[str, Any]]]) –relation between center point(s) and target point.
-
must_contain_coordinate(List[Iterable[int]]) –points should be in next window
-
point_in(Iterable[int]) –point inside window from previous boundary
-
point_out(Iterable[int]) –point outside window from previous boundary
Returns:
-
Optional[Iterable[int]]–Optional[Iterable[int]]: proper next center (or None)
Source code in SaigeToolkit/data/crop/edge_cropper.py
get_destination_point
get_destination_point(relation: WindowState, point_ref: Iterable[int], point_in: Iterable[int], point_out: Iterable[int]) -> ndarray
calculate point a-window-away from reference point
Parameters:
-
relation(WindowState) –relation between reference point and next center
-
point_ref(Iterable[int]) –reference point to calculate next center
-
point_in(Iterable[int]) –point inside window from previous boundary
-
point_out(Iterable[int]) –point outside window from previous boundary
Returns:
-
ndarray–np.ndarray: result destination point
Source code in SaigeToolkit/data/crop/edge_cropper.py
remove_inner_points_and_get_outer_points
remove_inner_points_and_get_outer_points(center: Iterable[int], points: List[Iterable[int]]) -> Tuple[List[Iterable[int]], Iterable[int], Iterable[int]]
once center decided, check other points and remove insiders.
Parameters:
-
center(Iterable[int]) –next center coordinate
-
points(List[Iterable[int]]) –current points on polygon
Returns:
-
Tuple[List[Iterable[int]], Iterable[int], Iterable[int]]–Tuple[List[Iterable[int]], Iterable[int], Iterable[int]]: - List[Iterable[int]]: clean-up'd polygon points - Iterable[int]: boundary point (positive direction) - Iterable[int]: boundary point (negative direction)
Source code in SaigeToolkit/data/crop/edge_cropper.py
read_image_size
read_image_size(image: Union[Image, Tensor, ndarray]) -> ImageSizeType
image size: (W, H)
Source code in SaigeToolkit/data/transform/image_function.py
crop
crop(image: Union[Image, Tensor, ndarray], coordinates: Union[List[int], Tuple[int, int, int, int]]) -> Union[Image, Tensor, ndarray]
이미지의 coordinates 좌표영역을 크롭합니다. coordinates가 이미지를 벗어나는 경우 zero padding 합니다.
Parameters:
-
image(Union[Image, Tensor, ndarray]) –image
-
coordinates(Union[List[int], Tuple[int, int, int, int]]) –[left, top, right, bottom]
Returns:
-
Union[Image, Tensor, ndarray]–Union[Image.Image, torch.Tensor, np.ndarray]: cropped image
Source code in SaigeToolkit/data/transform/image_function.py
subdivide_points_into_min_resolution
subdivide_points_into_min_resolution(points: List[Iterable[int]], width_resolution: int = 32, height_resolution: int = 32) -> List[Iterable[int]]
refine cv2.findContours result. long straight edge in mask results in long distance between two points.
Parameters:
-
points(List[Iterable[int]]) –cv2.findContours result
-
width_resolution(int, default:32) –maximum distance width-wise. Defaults to 32.
-
height_resolution(int, default:32) –maximum distance height-wise. Defaults to 32.
Returns:
-
List[Iterable[int]]–List[Iterable[int]]: refined polygon points
Source code in SaigeToolkit/data/crop/edge_cropper.py
get_mid_point
In case failed to get next center point, get mid point from remaining polygon points.
Parameters:
-
points(List[Iterable[int]]) –remaining polygon points
Returns:
-
Iterable[int]–Iterable[int]: next center point (mid)