Commit 6fd4ebc1 authored by Jean-Marie Lepioufle's avatar Jean-Marie Lepioufle
Browse files

enabling same augmentation for different tiles and same loc

parent ead1abec
from osmaug.transformation.affine import tf_from_center_random
from osmaug.transformation.affine import tf_from_center_random_param, tf_from_center_random
augm_param_dict = {
"affineRandom": tf_from_center_random_param}
augm_dict = {
"affineRandom": tf_from_center_random}
......@@ -4,14 +4,14 @@ import numpy as np
from skimage import io, transform, util
def tf_from_center_random(img: np.ndarray):
def tf_from_center_random_param(img_shape0,img_shape1):
"""
Transform an image around its center with random rotation, random shear and reflection
"""
# For a transformatino around the center of the image
# one compose a translation to change the origin, a transformatino, and finally the inverse of the first translation.
# https://scikit-image.org/docs/stable/auto_examples/transform/plot_transform_types.html#sphx-glr-auto-examples-transform-plot-transform-types-py
shift = transform.EuclideanTransform(translation=-np.array([img.shape[1],img.shape[0]]) / 2)
shift = transform.EuclideanTransform(translation=-np.array([img_shape1,img_shape0]) / 2)
# random rotation between -pi and pi
rotation = transform.EuclideanTransform(rotation=random.uniform(-np.pi, np.pi))
# shear parallel to the x-axis.
......@@ -40,6 +40,10 @@ def tf_from_center_random(img: np.ndarray):
[0, 0, 1]])
# transformation matrix
matrix = np.linalg.inv(shift.params) @ reflection @ shear_x @ shear_y @ rotation.params @ shift.params
tform = transform.EuclideanTransform(matrix)
return(matrix)
def tf_from_center_random(img, tf_matrix):
tform = transform.EuclideanTransform(tf_matrix)
tf_img = transform.warp(img, tform.inverse)
return(tf_img)
......@@ -4,8 +4,7 @@ import os
from skimage import io, img_as_float, img_as_ubyte
from PIL import Image
import numpy as np
from osmaug.dictionnary.dictionnary_tf import augm_dict
from osmaug.dictionnary.dictionnary_tf import augm_param_dict,augm_dict
from osmaug.transformation.crop import crop
def augmosm_function(params: Dict):
......@@ -17,7 +16,9 @@ def augmosm_function(params: Dict):
for index, row in files.iterrows():
print("-> "+row['name'])
img = img_as_float(io.imread(os.path.join(workdir,row["name"])))
augm_param = augm_param_dict[row['augmentation']]
augm = augm_dict[row['augmentation']]
img = augm(img)
augm_matrix = augm_param(img.shape[0],img.shape[1])
img = augm(img,augm_matrix)
img = crop(img, delta=row["delta_pixel"])
pil_img = Image.fromarray(img_as_ubyte(img)).save(os.path.join(resdir,"aug_"+row['augmentation']+"_"+row["name"]))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment