From 3081dcabe079f5a5528e8b69be6c285fb0a28041 Mon Sep 17 00:00:00 2001 From: Rui Date: Tue, 8 Oct 2019 23:49:14 -0400 Subject: [PATCH 1/2] Add fit_output (False/True) option to PerspectiveTransform augmenter --- imgaug/augmenters/geometric.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/imgaug/augmenters/geometric.py b/imgaug/augmenters/geometric.py index dbac47e62..e0b2a2b03 100644 --- a/imgaug/augmenters/geometric.py +++ b/imgaug/augmenters/geometric.py @@ -2547,7 +2547,7 @@ class PerspectiveTransform(meta.Augmenter): """ def __init__(self, scale=0, cval=0, mode='constant', keep_size=True, - polygon_recoverer="auto", + polygon_recoverer="auto", fit_output=False, name=None, deterministic=False, random_state=None): super(PerspectiveTransform, self).__init__( name=name, deterministic=deterministic, random_state=random_state) @@ -2573,6 +2573,8 @@ def __init__(self, scale=0, cval=0, mode='constant', keep_size=True, if polygon_recoverer == "auto": self.polygon_recoverer = _ConcavePolygonRecoverer() + self.fit_output = fit_output + # Special order, mode and cval parameters for heatmaps and # segmentation maps. These may either be None or a fixed value. # Stochastic parameters are currently *not* supported. @@ -2831,6 +2833,20 @@ def _augment_bounding_boxes(self, bounding_boxes_on_images, random_state, return self._augment_bounding_boxes_as_keypoints( bounding_boxes_on_images, random_state, parents, hooks) + def _expand_transform(self, M, shape): + imgHeight, imgWidth = shape + rect = np.array([ + [0, 0], + [imgWidth - 1, 0], + [imgWidth - 1, imgHeight - 1], + [0, imgHeight - 1]], dtype='float32') + dst = cv2.perspectiveTransform(np.array([rect]), M)[0] + dst -= dst.min(axis=0, keepdims=True) + dst = np.around(dst, decimals=0) + M_expanded = cv2.getPerspectiveTransform(rect, dst) + maxWidth, maxHeight = dst.max(axis=0) + return M_expanded, maxWidth, maxHeight + def _create_matrices(self, shapes, random_state): # TODO change these to class attributes mode_str_to_int = { @@ -2929,6 +2945,10 @@ def _create_matrices(self, shapes, random_state): # compute the perspective transform matrix and then apply it m = cv2.getPerspectiveTransform(points, dst) + + if self.fit_output: + m, max_width, max_height = self._expand_transform(m, (h, w)) + matrices.append(m) max_heights.append(max_height) max_widths.append(max_width) From 2829657b4fb7f6515a146ba7cd6ea0efc6d16542 Mon Sep 17 00:00:00 2001 From: Rui Date: Thu, 10 Oct 2019 16:06:54 -0400 Subject: [PATCH 2/2] Calculate expanded WH properly --- imgaug/augmenters/geometric.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgaug/augmenters/geometric.py b/imgaug/augmenters/geometric.py index e0b2a2b03..2f6776c83 100644 --- a/imgaug/augmenters/geometric.py +++ b/imgaug/augmenters/geometric.py @@ -2844,7 +2844,7 @@ def _expand_transform(self, M, shape): dst -= dst.min(axis=0, keepdims=True) dst = np.around(dst, decimals=0) M_expanded = cv2.getPerspectiveTransform(rect, dst) - maxWidth, maxHeight = dst.max(axis=0) + maxWidth, maxHeight = dst.max(axis=0) + 1 return M_expanded, maxWidth, maxHeight def _create_matrices(self, shapes, random_state):