Skip to content

Commit

Permalink
Elastic support bboxes (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipet committed Aug 28, 2022
1 parent c3cb70a commit 228c9d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Spatial-level transforms will simultaneously change both an input image as well
| [Crop](https://albumentations.ai/docs/api_reference/augmentations/crops/transforms/#albumentations.augmentations.crops.transforms.Crop) |||||
| [CropAndPad](https://albumentations.ai/docs/api_reference/augmentations/crops/transforms/#albumentations.augmentations.crops.transforms.CropAndPad) |||||
| [CropNonEmptyMaskIfExists](https://albumentations.ai/docs/api_reference/augmentations/crops/transforms/#albumentations.augmentations.crops.transforms.CropNonEmptyMaskIfExists) |||||
| [ElasticTransform](https://albumentations.ai/docs/api_reference/augmentations/geometric/transforms/#albumentations.augmentations.geometric.transforms.ElasticTransform) ||| | |
| [ElasticTransform](https://albumentations.ai/docs/api_reference/augmentations/geometric/transforms/#albumentations.augmentations.geometric.transforms.ElasticTransform) ||| | |
| [Flip](https://albumentations.ai/docs/api_reference/augmentations/geometric/transforms/#albumentations.augmentations.geometric.transforms.Flip) |||||
| [GridDistortion](https://albumentations.ai/docs/api_reference/augmentations/geometric/transforms/#albumentations.augmentations.geometric.transforms.GridDistortion) |||| |
| [GridDropout](https://albumentations.ai/docs/api_reference/augmentations/dropout/grid_dropout/#albumentations.augmentations.dropout.grid_dropout.GridDropout) ||| | |
Expand Down
24 changes: 23 additions & 1 deletion albumentations/augmentations/geometric/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class ElasticTransform(DualTransform):
Enabling this option gives ~2X speedup.
Targets:
image, mask
image, mask, bbox
Image types:
uint8, float32
Expand Down Expand Up @@ -230,6 +230,28 @@ def apply_to_mask(self, img, random_state=None, **params):
self.same_dxdy,
)

def apply_to_bbox(self, bbox, random_state=None, **params):
rows, cols = params["rows"], params["cols"]
mask = np.zeros((rows, cols), dtype=np.uint8)
bbox_denorm = F.denormalize_bbox(bbox, rows, cols)
x_min, y_min, x_max, y_max = bbox_denorm[:4]
x_min, y_min, x_max, y_max = int(x_min), int(y_min), int(x_max), int(y_max)
mask[y_min:y_max, x_min:x_max] = 1
mask = F.elastic_transform(
mask,
self.alpha,
self.sigma,
self.alpha_affine,
cv2.INTER_NEAREST,
self.border_mode,
self.mask_value,
np.random.RandomState(random_state),
self.approximate,
)
bbox_returned = bbox_from_mask(mask)
bbox_returned = F.normalize_bbox(bbox_returned, rows, cols)
return bbox_returned

def get_params(self):
return {"random_state": random.randint(0, 10000)}

Expand Down

0 comments on commit 228c9d8

Please sign in to comment.