def online_affine_perterb(pil_images, rng, **kw): """ Args: pil_images (list) list of images to receive the same transform Exception: >>> from clab.augment import * >>> from PIL import Image >>> import ubelt as ub >>> import numpy as np >>> import plottool as pt >>> rng = np.random >>> fpath = ub.grabdata('https://i.imgur.com/oHGsmvF.png', fname='carl.png') >>> img = Image.open(fpath) >>> kw = {} >>> imaug, = online_affine_perterb([img], rng) >>> #imaug.show() >>> x = np.array(imaug)[:, :, ::-1] >>> y = np.array(imaug)[:, :, ::-1] >>> pt.imshow(np.array(imaug)[:, :, ::-1]) """ augkw = PERTERB_AUG_KW.copy() augkw.update(kw) affine_args = random_affine_args(rng=rng, **augkw) for img in pil_images: w1, h1 = img.size x, y = w1 / 2, h1 / 2 Aff = affine_around_mat2x3(x, y, *affine_args) pil_aff_params = list(it.chain.from_iterable(Aff)) imgaug = img.transform((w1, h1), Image.AFFINE, pil_aff_params, fill=-1) # Image.AFFINE # # Image.BICUBIC # Image.NEAREST # imaug = cv2.warpAffine( # img, Aff, # dsize=(w1, h1), # flags=cv2.INTER_LANCZOS4, # borderMode=cv2.BORDER_REFLECT # ) yield imgaug
def online_affine_perterb_np(np_images, rng, interp='cubic', border_mode='reflect', **kw): """ Args: np_images (list) list of images to receive the same transform Exception: >>> from clab.augment.augment_numpy_online import * >>> import ubelt as ub >>> import numpy as np >>> import plottool as pt >>> rng = np.random >>> fpath = ub.grabdata('https://i.imgur.com/oHGsmvF.png', fname='carl.png') >>> img = imutil.imread(fpath) >>> np_images = [img] >>> kw = {} >>> imaug, = online_affine_perterb_np([img], rng) >>> pt.imshow(np.array(imaug)) Ignore: Aff = affine_around_mat2x3(0, 0) matrix = np.array(Aff + [[0, 0, 1]]) skaff = skimage.transform.AffineTransform(matrix=matrix) # CONCLUSION: this can handle n-channel images img2 = np.random.rand(24, 24, 5) imaug2 = skimage.transform.warp( img2, skaff, output_shape=img2.shape, order=0, mode='reflect', clip=True, preserve_range=True) """ augkw = PERTERB_AUG_KW.copy() augkw.update(kw) affine_args = random_affine_args(rng=rng, **augkw) if not ub.iterable(interp): interps = [interp] * len(np_images) else: interps = interp assert len(interps) == len(np_images) for img, interp_ in zip(np_images, interps): h1, w1 = img.shape[0:2] x, y = w1 / 2, h1 / 2 Aff = affine_around_mat2x3(x, y, *affine_args) matrix = np.array(Aff + [[0, 0, 1]]) skaff = skimage.transform.AffineTransform(matrix=matrix) order = SKIMAGE_INTERP_LOOKUP[interp_] imaug = skimage.transform.warp( img, skaff, output_shape=img.shape, order=order, mode=border_mode, # cval=0.0, clip=True, preserve_range=True) imaug = imaug.astype(img.dtype) # imaug = cv2.warpAffine( # img, Aff, # dsize=(w1, h1), # flags=cv2.INTER_LANCZOS4, # borderMode=cv2.BORDER_REFLECT # ) yield imaug
def random_params(self): affine_args = augment_common.random_affine_args(**self.augkw, rng=self.rng) return affine_args