imgaug
imgaug copied to clipboard
Problem with rgb input for the cval parameter, when applying affine transformations to image
from the documentation, the cval parameter is described as follows:
The constant value used to fill up pixels in the result image that didn’t exist in the input image (e.g. when translating to the left, some new pixels are created at the right).
I read the comments from the following link: https://github.com/aleju/imgaug/blob/master/imgaug/augmenters/geometric.py (line 375)
backend : str, optional Framework to use as a backend. Valid values are
auto
,skimage
(scikit-image's warp) andcv2
(OpenCV's warp). Ifauto
is used, the augmenter will automatically try to usecv2
where possible (order must be in[0, 1, 3]
and image's dtype uint8, otherwise skimage is chosen). It will silently fall back to skimage if order/dtype is not supported by cv2. cv2 is generally faster than skimage. It also supports RGB cvals, while skimage will resort to intensity cvals (i.e. 3x the same value as RGB). Ifcv2
is chosen and order is 2 or 4, it will automatically fall back to order 3.
so from the paragraph above I understand that it is possible to insert RGB value to the cval parameter if backend = 'cv2' is used, but I receive dimensions error.
here is the code I use:
seq = iaa.Sequential([iaa.Affine(rotate=30,cval=(0,0,255), backend='cv2')])
seq_det = seq.to_deterministic()
new_image = seq_det.augment_image(image)
and here is the error message:
AssertionError: Expected parameter 'cval' with type tuple to have exactly two entries, but got 3.
Thank you friends
A tuple is expected to be of form (a, b)
and interpreted as the bounds of a uniform distribution. The RGB value will then be generated per image by sampling three values independently from uniform(a, b)
. E.g. (10, 30)
may result in RGB (15, 25, 17)
, but not in (10, 10, 255)
.
I think there is currently no predefined way to provide a constant RGB color, you can only provide a single integer value as the intensity, e.g. cval=255
will always sample RGB (255, 255, 255)
. However, you can rather easily create your own parameter which returns full RGB colors:
import numpy as np
import imgaug as ia
from imgaug import augmenters as iaa
from imgaug import parameters as iap
class DeterministicColor(iap.StochasticParameter):
def __init__(self, color):
self.color = np.uint8(color)
def _draw_samples(self, size, random_state):
assert size[-1] == 3
arr = np.zeros(size, dtype=np.uint8)
arr[..., :] = self.color
return arr
aug = iaa.Affine(rotate=45,
cval=DeterministicColor([0, 0, 255]),
mode="constant")
image_aug = aug.augment_image(ia.quokka(size=(128, 128)))
ia.imshow(image_aug)
Output:
Thanks a lot! Just wanted to share that I'm making affine augmentations to pictures taken with green screen as a background so it's quite necessary in my scenario.
A tuple is expected to be of form
(a, b)
and interpreted as the bounds of a uniform distribution. The RGB value will then be generated per image by sampling three values independently fromuniform(a, b)
. E.g.(10, 30)
may result in RGB(15, 25, 17)
, but not in(10, 10, 255)
.I think there is currently no predefined way to provide a constant RGB color, you can only provide a single integer value as the intensity, e.g.
cval=255
will always sample RGB(255, 255, 255)
. However, you can rather easily create your own parameter which returns full RGB colors:import numpy as np import imgaug as ia from imgaug import augmenters as iaa from imgaug import parameters as iap class DeterministicColor(iap.StochasticParameter): def __init__(self, color): self.color = np.uint8(color) def _draw_samples(self, size, random_state): assert size[-1] == 3 arr = np.zeros(size, dtype=np.uint8) arr[..., :] = self.color return arr aug = iaa.Affine(rotate=45, cval=DeterministicColor([0, 0, 255]), mode="constant") image_aug = aug.augment_image(ia.quokka(size=(128, 128))) ia.imshow(image_aug)
Output:
This is very interesting and very much needed. How should the DeterministicColor class be modified for Cutout augmenter?