Translate operation
How can I perform a simply translate operation? I need to mantain the image size, and moving image left/right up/down of a random value.
# Create your new operation by inheriting from the Operation superclass:
from Augmentor.Operations import Operation
class TranslateImage(Operation):
# Here you can accept as many custom parameters as required:
def __init__(self, probability,max_left_translation,max_right_translation,max_down_translation,max_up_translation):
# Call the superclass's constructor (meaning you must
# supply a probability value):
Operation.__init__(self, probability)
# Set your custom operation's member variables here as required:
self.max_left_translation = -abs(max_left_translation)
self.max_right_translation = max_right_translation
self.max_down_translation = -abs(max_down_translation)
self.max_up_translation = max_up_translation
# Your class must implement the perform_operation method:
def perform_operation(self, images):
# Start of code to perform custom image operation.
def do(image):
random_left = random.randint(self.max_left_translation, 0)
random_right = random.randint(0, self.max_right_translation)
left_or_right = random.randint(0, 1)
translation_left_right = 0
if left_or_right == 0:
translation_left_right= random_left
elif left_or_right == 1:
translation_left_right = random_right
random_down = random.randint(self.max_down_translation, 0)
random_up = random.randint(0, self.max_up_translation)
up_or_down = random.randint(0, 1)
translation_up_down = 0
if up_or_down == 0:
translation_up_down = random_up
elif up_or_down == 1:
translation_up_down = random_down
a = 1
b = 0
c = translation_left_right #left/right (i.e. 5/-5)
d = 0
e = 1
f = translation_up_down #up/down (i.e. 5/-5)
#image = PIL.Image.fromarray(image)
image = image.transform(image.size, PIL.Image.AFFINE, (a, b, c, d, e, f))
return image
augmented_images = []
for image in images:
augmented_images.append(do(image))
return augmented_images
Next,add this on your pipeline:
trans = TranslateImage(probability = 0.5, max_left_translation=translation,
max_right_translation=translation,max_down_translation=translation,max_up_translation=translation)
# Add this to the current pipeline
p.add_operation(trans)
Hi @fabio-C, first of all sorry for the delay in replying I've been away from my office for several weeks now. Translate is currently not supported, but I can add it in the next release. The trouble is that you need to fill the empty space with some background colour (or transparency) and there are several ways to do that (such as extrapolating) so I will have to work on that before I can add it.
The code kindly provided by @ivicts will work but only if you are passing images one at at time through the pipeline (i.e. without any masks). This code here needs to be modified to be outside of the do() function so that the randomisation occurs once upon being called and the same values are used for each image passed through do():
random_left = random.randint(self.max_left_translation, 0)
random_right = random.randint(0, self.max_right_translation)
left_or_right = random.randint(0, 1)
translation_left_right = 0
if left_or_right == 0:
translation_left_right= random_left
elif left_or_right == 1:
translation_left_right = random_right
random_down = random.randint(self.max_down_translation, 0)
random_up = random.randint(0, self.max_up_translation)
up_or_down = random.randint(0, 1)
translation_up_down = 0
if up_or_down == 0:
translation_up_down = random_up
elif up_or_down == 1:
translation_up_down = random_down
So all the above code needs to be outside of do() and be executed once upon being called by the pipeline.
So @fabio-C, if you are willing to wait a week or so I can get this functionality added to the next version of Augmentor. Meanwhile you will need to rely on using a custom class, like that provided by @ivicts, but with the edits I have provided here (unless you are not using masks, in which case it will work fine).