polygon_cropper
data.crop.polygon_cropper
BaseCropper
Bases: ABC
get_n_patch
abstractmethod
PolygonCropper
PolygonCropper(mode: str = 'center', crop_w: int = 512, crop_h: int = 512, patch_per_polygon: int = 1, random_patch_per_img: int = 1, max_patch_per_img: Optional[int] = None, force_random_patch_normal: bool = False, strict_inner_patch: bool = False, fixed_polygon_order: bool = False)
Bases: BaseCropper
Crop image patches using polygons
Attributes:
-
mode(str) –cropping mode. ["center", "defectrandom"] available
-
crop_w(int) –cropping width for image patch.
-
crop_h(int) –cropping height for image patch.
-
half_w(int) –half of cropping width.
-
half_h(int) –half of cropping height.
-
patch_per_polygon(int) –number of repeat for cropping patch around each polygon.
-
random_patch_per_img(int) –number of random patch cropping per image.
-
force_random_patch_normal(bool) –whether random patch should not contain polygon area.
-
strict_inner_patch(bool) –whether not allowing outer area of image.
-
fixed_polygon_order(bool) –whether order of polygons is fixed during cropping.
All input is directly set to class attribute.
Parameters:
-
mode(str, default:'center') –Defaults to "center".
-
crop_w(int, default:512) –Defaults to 512.
-
crop_h(int, default:512) –Defaults to 512.
-
patch_per_polygon(int, default:1) –Defaults to 1.
-
random_patch_per_img(int, default:1) –Defaults to 1.
-
force_random_patch_normal(bool, default:False) –Defaults to False.
-
strict_inner_patch(bool, default:False) –Defaults to False.
-
fixed_polygon_order(bool, default:False) –Defaults to False.
Source code in SaigeToolkit/data/crop/polygon_cropper.py
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/polygon_cropper.py
pick_point
picking cropping center point.
Parameters:
-
polygon(ndarray) –original polygon data
Raises:
-
NotImplementedError–only two modes ["center", "defectrandom"] available
Returns:
-
Tuple[int, int]–Tuple[int, int]: picked point, (h_center, w_center)
Source code in SaigeToolkit/data/crop/polygon_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/polygon_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/polygon_cropper.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
read_image_size
read_image_size(image: Union[Image, Tensor, ndarray]) -> ImageSizeType
image size: (W, H)