Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WithPolarWarping #489

Merged
merged 9 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelogs/master/added/20191110_bb_polygon_conversion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Bounding Box to Polygon Conversion #489

* Added method `imgaug.augmentables.bbs.BoundingBox.to_polygon()`.
* Added method
`imgaug.augmentables.bbs.BoundingBoxesOnImage.to_polygons_on_image()`.
6 changes: 6 additions & 0 deletions changelogs/master/added/20191110_polygon_subdivision.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Added Polygon Subdivision #489

* Added method `imgaug.augmentables.polys.Polygon.subdivide(N)`.
The method increases the polygon's corner point count by interpolating
`N` points on each edge with regular distance.
* Added method `imgaug.augmentables.polys.PolygonsOnImage.subdivide(N)`.
5 changes: 5 additions & 0 deletions changelogs/master/added/20191110_withpolarwarping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Added `WithPolarWarping` #489

* Added augmenter `imgaug.augmenters.geometric.WithPolarWarping`, an
augmenter that applies child augmenters in a polar representation of the
image.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Affine Translation Precision #489

* Removed a rounding operation in `Affine` translation that would unnecessarily
round floats to integers. This should make coordinate augmentation overall
more accurate.
5 changes: 5 additions & 0 deletions changelogs/master/fixed/20191110_fixed_affine_map_aug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Fixed Affine Translation of Map-Data #489

* Fixed `Affine` producing unaligned augmentations between images and
segmentation maps or heatmaps when using `translate_px` and the segmentation
map or heatmap had a different height/width than corresponding image.
35 changes: 35 additions & 0 deletions imgaug/augmentables/bbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ def extract_from_image(self, image, pad=True, pad_max=None,

# TODO also add to_heatmap
# TODO add this to BoundingBoxesOnImage
# TODO add label to keypoints?
def to_keypoints(self):
"""Convert the BB's corners to keypoints (clockwise, from top left).

Expand All @@ -780,6 +781,25 @@ def to_keypoints(self):
Keypoint(x=self.x1, y=self.y2)
]

def to_polygon(self):
"""Convert this bounding box to a polygon covering the same area.

Returns
-------
imgaug.augmentables.polys.Polygon
The bounding box converted to a polygon.

"""
# TODO get rid of this deferred import
from imgaug.augmentables.polys import Polygon

return Polygon([
(self.x1, self.y1),
(self.x2, self.y1),
(self.x2, self.y2),
(self.x1, self.y2)
], label=self.label)

def coords_almost_equals(self, other, max_distance=1e-4):
"""Estimate if this and another BB have almost identical coordinates.

Expand Down Expand Up @@ -1458,6 +1478,21 @@ def invert_to_keypoints_on_image_(self, kpsoi):
self.shape = kpsoi.shape
return self

def to_polygons_on_image(self):
"""Convert the bounding boxes to one ``PolygonsOnImage`` instance.

Returns
-------
imgaug.augmentables.polys.PolygonsOnImage
A ``PolygonsOnImage`` containing polygons. Each polygon covers
the same area as the corresponding bounding box.

"""
from .polys import PolygonsOnImage

polygons = [bb.to_polygon() for bb in self.bounding_boxes]
return PolygonsOnImage(polygons, shape=self.shape)

def copy(self):
"""Create a shallow copy of the ``BoundingBoxesOnImage`` instance.

Expand Down
41 changes: 41 additions & 0 deletions imgaug/augmentables/polys.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,30 @@ def change_first_point_by_index(self, point_idx):
)
return self.deepcopy(exterior=exterior)

def subdivide(self, points_per_edge):
"""Derive a new polygon with ``N`` interpolated points per edge.

See :func:`imgaug.augmentables.lines.LineString.subdivide` for details.

Parameters
----------
points_per_edge : int
Number of points to interpolate on each edge.

Returns
-------
imgaug.augmentables.polys.Polygon
Polygon with subdivided edges.

"""
if len(self.exterior) == 1:
return self.deepcopy()
ls = self.to_line_string(closed=True)
ls_sub = ls.subdivide(points_per_edge)
# [:-1] even works if the polygon contains zero points
exterior_subdivided = ls_sub.coords[:-1]
return Polygon(exterior_subdivided, label=self.label)

def to_shapely_polygon(self):
"""Convert this polygon to a ``Shapely`` ``Polygon``.

Expand Down Expand Up @@ -1495,6 +1519,23 @@ def shift(self, top=None, right=None, bottom=None, left=None):
]
return PolygonsOnImage(polys_new, shape=self.shape)

def subdivide(self, points_per_edge):
"""Interpolate ``N`` points on each polygon.

Parameters
----------
points_per_edge : int
Number of points to interpolate on each edge.

Returns
-------
imgaug.augmentables.polys.PolygonsOnImage
Subdivided polygons.

"""
polys_new = [poly.subdivide(points_per_edge) for poly in self.polygons]
return PolygonsOnImage(polys_new, shape=self.shape)

def to_xy_array(self):
"""Convert all polygon coordinates to one array of shape ``(N,2)``.

Expand Down
Loading