From 22bfcb9ae30ca5f43ee1e96f12269d5ce7a4c14a Mon Sep 17 00:00:00 2001 From: lubebul Date: Wed, 28 Feb 2018 15:14:31 +0800 Subject: [PATCH 1/2] fix full crop --- Augmentor/Operations.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Augmentor/Operations.py b/Augmentor/Operations.py index 8981260..c653d12 100644 --- a/Augmentor/Operations.py +++ b/Augmentor/Operations.py @@ -848,19 +848,19 @@ def perform_operation(self, images): """ def do(image): - + w, h = image.size - - # TODO: Fix. We may want a full crop. - if self.width > w or self.height > h: - return image + diff_w, diff_h = w - self.width, h - self.height if self.centre: - return image.crop(((w/2)-(self.width/2), (h/2)-(self.height/2), (w/2)+(self.width/2), (h/2)+(self.height/2))) - else: - left_shift = random.randint(0, int((w - self.width))) - down_shift = random.randint(0, int((h - self.height))) - return image.crop((left_shift, down_shift, self.width + left_shift, self.height + down_shift)) + top_left_x, top_left_y = min(diff_w, w - diff_w), min(diff_h, h - diff_h) + return image.crop((top_left_x, top_left_y, w - top_left_x, h - top_left_y)) + + sign_w, sign_h = np.sign(diff_w), np.sign(diff_h) + + left_shift = sign_w * random.randint(0, int(sign_w * diff_w)) + down_shift = sign_h * random.randint(0, int(sign_h * diff_h)) + return image.crop((left_shift, down_shift, self.width + left_shift, self.height + down_shift)) augmented_images = [] From f99266cb024b2dd04d6c20d687b423b7e8229362 Mon Sep 17 00:00:00 2001 From: lubebul Date: Wed, 28 Feb 2018 20:31:53 +0800 Subject: [PATCH 2/2] move random out of the inner function --- Augmentor/Operations.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Augmentor/Operations.py b/Augmentor/Operations.py index c653d12..bc0315f 100644 --- a/Augmentor/Operations.py +++ b/Augmentor/Operations.py @@ -846,20 +846,21 @@ def perform_operation(self, images): :return: The transformed image(s) as a list of object(s) of type PIL.Image. """ - - def do(image): + + # The images must be of identical size, which is checked by Pipeline.ground_truth(). + w, h = images[0].size + + diff_w, diff_h = w - self.width, h - self.height + + sign_w, sign_h = np.sign(diff_w), np.sign(diff_h) - w, h = image.size - diff_w, diff_h = w - self.width, h - self.height - + left_shift = sign_w * random.randint(0, int(sign_w * diff_w)) + down_shift = sign_h * random.randint(0, int(sign_h * diff_h)) + + def do(image): if self.centre: top_left_x, top_left_y = min(diff_w, w - diff_w), min(diff_h, h - diff_h) return image.crop((top_left_x, top_left_y, w - top_left_x, h - top_left_y)) - - sign_w, sign_h = np.sign(diff_w), np.sign(diff_h) - - left_shift = sign_w * random.randint(0, int(sign_w * diff_w)) - down_shift = sign_h * random.randint(0, int(sign_h * diff_h)) return image.crop((left_shift, down_shift, self.width + left_shift, self.height + down_shift)) augmented_images = []