Skip to content

Commit

Permalink
Add Keypoint.xy and Keypoint.xy_int
Browse files Browse the repository at this point in the history
  • Loading branch information
aleju committed Nov 24, 2019
1 parent df270b3 commit 6e94012
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/master/20191113_iterable_augmentables.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
* Added ability to access coordinates of `BoundingBox`, `Polygon` and
`LineString` using indices or slices, e.g. `line_string[1:]` to get an
array of all coordinates except the first one.
* Added property `Keypoint.xy`.
* Added property `Keypoint.xy_int`.
24 changes: 24 additions & 0 deletions imgaug/augmentables/kps.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ def y_int(self):
"""
return int(np.round(self.y))

@property
def xy(self):
"""Get the keypoint's x- and y-coordinate as a single array.
Returns
-------
ndarray
A ``(2,)`` ``ndarray`` denoting the xy-coordinate pair.
"""
return self.coords[0, :]

@property
def xy_int(self):
"""Get the keypoint's xy-coord, rounded to closest integer.
Returns
-------
ndarray
A ``(2,)`` ``ndarray`` denoting the xy-coordinate pair.
"""
return np.round(self.xy).astype(np.int32)

def project(self, from_shape, to_shape):
"""Project the keypoint onto a new position on a new image.
Expand Down
10 changes: 10 additions & 0 deletions test/augmentables/test_kps.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ def test_y_int_for_float_inputs(self):
assert np.isclose(kp.y, 1.7)
assert kp.y_int == 2

def test_xy(self):
kp = ia.Keypoint(x=2, y=1.7)
assert np.allclose(kp.xy, (2, 1.7))

def test_xy_int(self):
kp = ia.Keypoint(x=1.3, y=1.6)
xy = kp.xy_int
assert np.allclose(xy, (1, 2))
assert xy.dtype.name == "int32"

def test_project_same_image_size(self):
kp = ia.Keypoint(y=1, x=2)
kp2 = kp.project((10, 10), (10, 10))
Expand Down

0 comments on commit 6e94012

Please sign in to comment.