def chapter_augmenters_fastsnowylandscape(): fn_start = "weather/fastsnowylandscape" image = LANDSCAPE_IMAGE aug = iaa.FastSnowyLandscape( lightness_threshold=140, lightness_multiplier=2.5 ) run_and_save_augseq( fn_start + ".jpg", aug, [image for _ in range(4*2)], cols=4, rows=2) aug = iaa.FastSnowyLandscape( lightness_threshold=[128, 200], lightness_multiplier=(1.5, 3.5) ) run_and_save_augseq( fn_start + "_random_choice.jpg", aug, [image for _ in range(4*2)], cols=4, rows=2) aug = iaa.FastSnowyLandscape( lightness_threshold=(100, 255), lightness_multiplier=(1.0, 4.0) ) run_and_save_augseq( fn_start + "_random_uniform.jpg", aug, [image for _ in range(4*2)], cols=4, rows=2)
def get_seq(flag_normal, flag_affine, flag_noise, flag_snow, flag_cloud, flag_fog, flag_snowflakes, flag_rain, flag_dropout): if flag_normal: seq_list = [ iaa.SomeOf((1, 2), [ iaa.LinearContrast((0.5, 2.0), per_channel=0.5), iaa.Grayscale(alpha=(0.0, 1.0)), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), ]) ] else: seq_list = [] if flag_affine: seq_list.append( iaa.Sometimes( 0.7, iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_percent={ "x": (-0.2, 0.2), "y": (-0.2, 0.2) }, rotate=(-25, 25), shear=(-8, 8)))) if flag_noise: seq_list.append( iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), ])) if flag_snow: seq_list.append( iaa.FastSnowyLandscape(lightness_threshold=(100, 255), lightness_multiplier=(1.0, 4.0))) elif flag_cloud: seq_list.append(iaa.Clouds()) elif flag_fog: seq_list.append(iaa.Fog()) elif flag_snowflakes: seq_list.append( iaa.Snowflakes(flake_size=(0.2, 0.7), speed=(0.007, 0.03))) elif flag_rain: seq_list.append(iaa.Rain()) if flag_dropout: seq_list.append( iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ])) return iaa.Sequential(seq_list, random_order=True)
def test_vary_lightness_multiplier(self): # test when varying lightness_multiplier between images image = np.arange(0, 6 * 6 * 3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=_TwoValueParam( 1.5, 2.0)) mask = (image_hls[..., 1] < 100) expected1 = np.copy(image_hls).astype(np.float64) expected1[..., 1][mask] *= 1.5 expected1 = np.clip(np.round(expected1), 0, 255).astype(np.uint8) expected1 = cv2.cvtColor(expected1, cv2.COLOR_HLS2RGB) mask = (image_hls[..., 1] < 100) expected2 = np.copy(image_hls).astype(np.float64) expected2[..., 1][mask] *= 2.0 expected2 = np.clip(np.round(expected2), 0, 255).astype(np.uint8) expected2 = cv2.cvtColor(expected2, cv2.COLOR_HLS2RGB) observed = aug.augment_images([image] * 4) assert np.array_equal(observed[0], expected1) assert np.array_equal(observed[1], expected2) assert np.array_equal(observed[2], expected1) assert np.array_equal(observed[3], expected2)
def main(): image = imageio.imread("https://upload.wikimedia.org/wikipedia/commons/8/89/Kukle%2CCzech_Republic..jpg", format="jpg") augs = [ ("iaa.FastSnowyLandscape(64, 1.5)", iaa.FastSnowyLandscape(64, 1.5)), ("iaa.FastSnowyLandscape(128, 1.5)", iaa.FastSnowyLandscape(128, 1.5)), ("iaa.FastSnowyLandscape(200, 1.5)", iaa.FastSnowyLandscape(200, 1.5)), ("iaa.FastSnowyLandscape(64, 2.5)", iaa.FastSnowyLandscape(64, 2.5)), ("iaa.FastSnowyLandscape(128, 2.5)", iaa.FastSnowyLandscape(128, 2.5)), ("iaa.FastSnowyLandscape(200, 2.5)", iaa.FastSnowyLandscape(200, 2.5)), ("iaa.FastSnowyLandscape(64, 3.5)", iaa.FastSnowyLandscape(64, 3.5)), ("iaa.FastSnowyLandscape(128, 3.5)", iaa.FastSnowyLandscape(128, 3.5)), ("iaa.FastSnowyLandscape(200, 3.5)", iaa.FastSnowyLandscape(200, 3.5)), ("iaa.FastSnowyLandscape()", iaa.FastSnowyLandscape()) ] for descr, aug in augs: print(descr) images_aug = aug.augment_images([image] * 64) ia.imshow(ia.draw_grid(images_aug))
def test_zero_sized_axes(self): shapes = [(0, 0, 3), (0, 1, 3), (1, 0, 3)] for shape in shapes: with self.subTest(shape=shape): image = np.zeros(shape, dtype=np.uint8) aug = iaa.FastSnowyLandscape(100, 1.5, from_colorspace="RGB") image_aug = aug(image=image) assert image_aug.dtype.name == "uint8" assert image_aug.shape == shape
def test_basic_functionality(self): # basic functionality test aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=2.0) image = np.arange(0, 6 * 6 * 3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) mask = (image_hls[..., 1] < 100) expected = np.copy(image_hls).astype(np.float32) expected[..., 1][mask] *= 2.0 expected = np.clip(np.round(expected), 0, 255).astype(np.uint8) expected = cv2.cvtColor(expected, cv2.COLOR_HLS2RGB) observed = aug.augment_image(image) assert np.array_equal(observed, expected)
def test___init__(self): # check parameters aug = iaa.FastSnowyLandscape(lightness_threshold=[100, 200], lightness_multiplier=[1.0, 4.0]) assert isinstance(aug.lightness_threshold, iap.Choice) assert len(aug.lightness_threshold.a) == 2 assert aug.lightness_threshold.a[0] == 100 assert aug.lightness_threshold.a[1] == 200 assert isinstance(aug.lightness_multiplier, iap.Choice) assert len(aug.lightness_multiplier.a) == 2 assert np.allclose(aug.lightness_multiplier.a[0], 1.0) assert np.allclose(aug.lightness_multiplier.a[1], 4.0)
def imgaug_snowylandscape(images, val_thresh, mulfactor, base_save_path): fastsnow = iaa.FastSnowyLandscape(val_thresh, mulfactor) fastsnow_imgs = fastsnow.augment_images(images) fastsnow_path = '\\snowylandscape\\' if not os.path.exists(base_save_path + fastsnow_path): os.mkdir(base_save_path + fastsnow_path) name_index = 0 for img in fastsnow_imgs: name_index += 1 imageio.imwrite(base_save_path+fastsnow_path+'img_aug_snowlandscape_'+ time.strftime('%Y%m%d_%H',time.localtime()) \ + '_' +str(name_index)+'.jpg',img)
def test_from_colorspace(self): # test BGR colorspace aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=2.0, from_colorspace="BGR") image = np.arange(0, 6 * 6 * 3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS) mask = (image_hls[..., 1] < 100) expected = np.copy(image_hls).astype(np.float32) expected[..., 1][mask] *= 2.0 expected = np.clip(np.round(expected), 0, 255).astype(np.uint8) expected = cv2.cvtColor(expected, cv2.COLOR_HLS2BGR) observed = aug.augment_image(image) assert np.array_equal(observed, expected)
def create_augmenters(height, width, height_augmentable, width_augmentable, only_augmenters): def lambda_func_images(images, random_state, parents, hooks): return images def lambda_func_heatmaps(heatmaps, random_state, parents, hooks): return heatmaps def lambda_func_keypoints(keypoints, random_state, parents, hooks): return keypoints def assertlambda_func_images(images, random_state, parents, hooks): return True def assertlambda_func_heatmaps(heatmaps, random_state, parents, hooks): return True def assertlambda_func_keypoints(keypoints, random_state, parents, hooks): return True augmenters_meta = [ iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=False, name="Sequential_2xNoop"), iaa.Sequential([iaa.Noop(), iaa.Noop()], random_order=True, name="Sequential_2xNoop_random_order"), iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=False, name="SomeOf_3xNoop"), iaa.SomeOf((1, 3), [iaa.Noop(), iaa.Noop(), iaa.Noop()], random_order=True, name="SomeOf_3xNoop_random_order"), iaa.OneOf([iaa.Noop(), iaa.Noop(), iaa.Noop()], name="OneOf_3xNoop"), iaa.Sometimes(0.5, iaa.Noop(), name="Sometimes_Noop"), iaa.WithChannels([1, 2], iaa.Noop(), name="WithChannels_1_and_2_Noop"), iaa.Noop(name="Noop"), iaa.Lambda(func_images=lambda_func_images, func_heatmaps=lambda_func_heatmaps, func_keypoints=lambda_func_keypoints, name="Lambda"), iaa.AssertLambda(func_images=assertlambda_func_images, func_heatmaps=assertlambda_func_heatmaps, func_keypoints=assertlambda_func_keypoints, name="AssertLambda"), iaa.AssertShape((None, height_augmentable, width_augmentable, None), name="AssertShape"), iaa.ChannelShuffle(0.5, name="ChannelShuffle") ] augmenters_arithmetic = [ iaa.Add((-10, 10), name="Add"), iaa.AddElementwise((-10, 10), name="AddElementwise"), #iaa.AddElementwise((-500, 500), name="AddElementwise"), iaa.AdditiveGaussianNoise(scale=(5, 10), name="AdditiveGaussianNoise"), iaa.AdditiveLaplaceNoise(scale=(5, 10), name="AdditiveLaplaceNoise"), iaa.AdditivePoissonNoise(lam=(1, 5), name="AdditivePoissonNoise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.Dropout((0.01, 0.05), name="Dropout"), iaa.CoarseDropout((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseDropout"), iaa.ReplaceElementwise((0.01, 0.05), (0, 255), name="ReplaceElementwise"), #iaa.ReplaceElementwise((0.95, 0.99), (0, 255), name="ReplaceElementwise"), iaa.SaltAndPepper((0.01, 0.05), name="SaltAndPepper"), iaa.ImpulseNoise((0.01, 0.05), name="ImpulseNoise"), iaa.CoarseSaltAndPepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSaltAndPepper"), iaa.Salt((0.01, 0.05), name="Salt"), iaa.CoarseSalt((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarseSalt"), iaa.Pepper((0.01, 0.05), name="Pepper"), iaa.CoarsePepper((0.01, 0.05), size_percent=(0.01, 0.1), name="CoarsePepper"), iaa.Invert(0.1, name="Invert"), # ContrastNormalization iaa.JpegCompression((50, 99), name="JpegCompression") ] augmenters_blend = [ iaa.Alpha((0.01, 0.99), iaa.Noop(), name="Alpha"), iaa.AlphaElementwise((0.01, 0.99), iaa.Noop(), name="AlphaElementwise"), iaa.SimplexNoiseAlpha(iaa.Noop(), name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha((-2.0, 2.0), iaa.Noop(), name="FrequencyNoiseAlpha") ] augmenters_blur = [ iaa.GaussianBlur(sigma=(1.0, 5.0), name="GaussianBlur"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.BilateralBlur(d=(3, 11), name="BilateralBlur"), iaa.MotionBlur(k=(3, 11), name="MotionBlur") ] augmenters_color = [ # InColorspace (deprecated) iaa.WithColorspace(to_colorspace="HSV", children=iaa.Noop(), name="WithColorspace"), iaa.WithHueAndSaturation(children=iaa.Noop(), name="WithHueAndSaturation"), iaa.MultiplyHueAndSaturation((0.8, 1.2), name="MultiplyHueAndSaturation"), iaa.MultiplyHue((-1.0, 1.0), name="MultiplyHue"), iaa.MultiplySaturation((0.8, 1.2), name="MultiplySaturation"), iaa.AddToHueAndSaturation((-10, 10), name="AddToHueAndSaturation"), iaa.AddToHue((-10, 10), name="AddToHue"), iaa.AddToSaturation((-10, 10), name="AddToSaturation"), iaa.ChangeColorspace(to_colorspace="HSV", name="ChangeColorspace"), iaa.Grayscale((0.01, 0.99), name="Grayscale"), iaa.KMeansColorQuantization((2, 16), name="KMeansColorQuantization"), iaa.UniformColorQuantization((2, 16), name="UniformColorQuantization") ] augmenters_contrast = [ iaa.GammaContrast(gamma=(0.5, 2.0), name="GammaContrast"), iaa.SigmoidContrast(gain=(5, 20), cutoff=(0.25, 0.75), name="SigmoidContrast"), iaa.LogContrast(gain=(0.7, 1.0), name="LogContrast"), iaa.LinearContrast((0.5, 1.5), name="LinearContrast"), iaa.AllChannelsCLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), name="AllChannelsCLAHE"), iaa.CLAHE(clip_limit=(2, 10), tile_grid_size_px=(3, 11), to_colorspace="HSV", name="CLAHE"), iaa.AllChannelsHistogramEqualization( name="AllChannelsHistogramEqualization"), iaa.HistogramEqualization(to_colorspace="HSV", name="HistogramEqualization"), ] augmenters_convolutional = [ iaa.Convolve(np.float32([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), name="Convolve_3x3"), iaa.Sharpen(alpha=(0.01, 0.99), lightness=(0.5, 2), name="Sharpen"), iaa.Emboss(alpha=(0.01, 0.99), strength=(0, 2), name="Emboss"), iaa.EdgeDetect(alpha=(0.01, 0.99), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.01, 0.99), name="DirectedEdgeDetect") ] augmenters_edges = [iaa.Canny(alpha=(0.01, 0.99), name="Canny")] augmenters_flip = [ iaa.Fliplr(1.0, name="Fliplr"), iaa.Flipud(1.0, name="Flipud") ] augmenters_geometric = [ iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=0, mode="constant", cval=(0, 255), name="Affine_order_0_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=1, mode="constant", cval=(0, 255), name="Affine_order_1_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=3, mode="constant", cval=(0, 255), name="Affine_order_3_constant"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=1, mode="edge", cval=(0, 255), name="Affine_order_1_edge"), iaa.Affine(scale=(0.9, 1.1), translate_percent={ "x": (-0.05, 0.05), "y": (-0.05, 0.05) }, rotate=(-10, 10), shear=(-10, 10), order=1, mode="constant", cval=(0, 255), backend="skimage", name="Affine_order_1_constant_skimage"), # TODO AffineCv2 iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="constant", name="PiecewiseAffine_4x4_order_1_constant"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=0, mode="constant", name="PiecewiseAffine_4x4_order_0_constant"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=4, nb_cols=4, order=1, mode="edge", name="PiecewiseAffine_4x4_order_1_edge"), iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=8, nb_cols=8, order=1, mode="constant", name="PiecewiseAffine_8x8_order_1_constant"), iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=False, name="PerspectiveTransform"), iaa.PerspectiveTransform(scale=(0.01, 0.05), keep_size=True, name="PerspectiveTransform_keep_size"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=0, mode="constant", cval=0, name="ElasticTransformation_order_0_constant"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="constant", cval=0, name="ElasticTransformation_order_1_constant"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="nearest", cval=0, name="ElasticTransformation_order_1_nearest"), iaa.ElasticTransformation( alpha=(1, 10), sigma=(0.5, 1.5), order=1, mode="reflect", cval=0, name="ElasticTransformation_order_1_reflect"), iaa.Rot90((1, 3), keep_size=False, name="Rot90"), iaa.Rot90((1, 3), keep_size=True, name="Rot90_keep_size") ] augmenters_pooling = [ iaa.AveragePooling(kernel_size=(1, 16), keep_size=False, name="AveragePooling"), iaa.AveragePooling(kernel_size=(1, 16), keep_size=True, name="AveragePooling_keep_size"), iaa.MaxPooling(kernel_size=(1, 16), keep_size=False, name="MaxPooling"), iaa.MaxPooling(kernel_size=(1, 16), keep_size=True, name="MaxPooling_keep_size"), iaa.MinPooling(kernel_size=(1, 16), keep_size=False, name="MinPooling"), iaa.MinPooling(kernel_size=(1, 16), keep_size=True, name="MinPooling_keep_size"), iaa.MedianPooling(kernel_size=(1, 16), keep_size=False, name="MedianPooling"), iaa.MedianPooling(kernel_size=(1, 16), keep_size=True, name="MedianPooling_keep_size") ] augmenters_segmentation = [ iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="cubic", name="Superpixels_max_size_64_cubic"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=64, interpolation="linear", name="Superpixels_max_size_64_linear"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=128, interpolation="linear", name="Superpixels_max_size_128_linear"), iaa.Superpixels(p_replace=(0.05, 1.0), n_segments=(10, 100), max_size=224, interpolation="linear", name="Superpixels_max_size_224_linear"), iaa.UniformVoronoi(n_points=(250, 1000), name="UniformVoronoi"), iaa.RegularGridVoronoi(n_rows=(16, 31), n_cols=(16, 31), name="RegularGridVoronoi"), iaa.RelativeRegularGridVoronoi(n_rows_frac=(0.07, 0.14), n_cols_frac=(0.07, 0.14), name="RelativeRegularGridVoronoi"), ] augmenters_size = [ iaa.Resize((0.8, 1.2), interpolation="nearest", name="Resize_nearest"), iaa.Resize((0.8, 1.2), interpolation="linear", name="Resize_linear"), iaa.Resize((0.8, 1.2), interpolation="cubic", name="Resize_cubic"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False, name="CropAndPad"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False, name="CropAndPad_edge"), iaa.CropAndPad(percent=(-0.2, 0.2), pad_mode="constant", pad_cval=(0, 255), name="CropAndPad_keep_size"), iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), keep_size=False, name="Pad"), iaa.Pad(percent=(0.05, 0.2), pad_mode="edge", pad_cval=(0, 255), keep_size=False, name="Pad_edge"), iaa.Pad(percent=(0.05, 0.2), pad_mode="constant", pad_cval=(0, 255), name="Pad_keep_size"), iaa.Crop(percent=(0.05, 0.2), keep_size=False, name="Crop"), iaa.Crop(percent=(0.05, 0.2), name="Crop_keep_size"), iaa.PadToFixedSize(width=width + 10, height=height + 10, pad_mode="constant", pad_cval=(0, 255), name="PadToFixedSize"), iaa.CropToFixedSize(width=width - 10, height=height - 10, name="CropToFixedSize"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10, width=width - 10), interpolation="nearest", name="KeepSizeByResize_CropToFixedSize_nearest"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10, width=width - 10), interpolation="linear", name="KeepSizeByResize_CropToFixedSize_linear"), iaa.KeepSizeByResize(iaa.CropToFixedSize(height=height - 10, width=width - 10), interpolation="cubic", name="KeepSizeByResize_CropToFixedSize_cubic"), ] augmenters_weather = [ iaa.FastSnowyLandscape(lightness_threshold=(100, 255), lightness_multiplier=(1.0, 4.0), name="FastSnowyLandscape"), iaa.Clouds(name="Clouds"), iaa.Fog(name="Fog"), iaa.CloudLayer(intensity_mean=(196, 255), intensity_freq_exponent=(-2.5, -2.0), intensity_coarse_scale=10, alpha_min=0, alpha_multiplier=(0.25, 0.75), alpha_size_px_max=(2, 8), alpha_freq_exponent=(-2.5, -2.0), sparsity=(0.8, 1.0), density_multiplier=(0.5, 1.0), name="CloudLayer"), iaa.Snowflakes(name="Snowflakes"), iaa.SnowflakesLayer(density=(0.005, 0.075), density_uniformity=(0.3, 0.9), flake_size=(0.2, 0.7), flake_size_uniformity=(0.4, 0.8), angle=(-30, 30), speed=(0.007, 0.03), blur_sigma_fraction=(0.0001, 0.001), name="SnowflakesLayer") ] augmenters = (augmenters_meta + augmenters_arithmetic + augmenters_blend + augmenters_blur + augmenters_color + augmenters_contrast + augmenters_convolutional + augmenters_edges + augmenters_flip + augmenters_geometric + augmenters_pooling + augmenters_segmentation + augmenters_size + augmenters_weather) if only_augmenters is not None: augmenters_reduced = [] for augmenter in augmenters: if any([ re.search(pattern, augmenter.name) for pattern in only_augmenters ]): augmenters_reduced.append(augmenter) augmenters = augmenters_reduced return augmenters
def test_pickleable(self): aug = iaa.FastSnowyLandscape(lightness_threshold=(50, 150), lightness_multiplier=(1.0, 3.0), random_state=1) runtest_pickleable_uint8_img(aug)
import random from imgaug import augmenters as iaa from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage import cv2 from verifier import fitCoords aug = iaa.Sequential( [ iaa.Sometimes(0.5, iaa.Crop(percent=(0.1, 0.3), keep_size=False)), iaa.Sometimes(0.5, iaa.MotionBlur(15, random.randint(0, 360))), iaa.OneOf([ iaa.AllChannelsCLAHE(clip_limit=10), iaa.AdditiveGaussianNoise(scale=(10, 35)), iaa.FastSnowyLandscape(lightness_threshold=(50, 115), from_colorspace="BGR") ]), # iaa.Sometimes(0.25, iaa.Affine(scale={"x": (1.0, 1.2), "y": (1.0, 1.2)})), iaa.Sometimes(0.25, iaa.Multiply((0.85, 1.15))), iaa.Sometimes(0.25, iaa.ContrastNormalization((0.85, 1.15))), # iaa.Affine(rotate=(0, 360)) ], random_order=False ) def customAugmentations(image, box): y1, x1, y2, x2 = box bb = BoundingBox(x1=x1, x2=x2, y1=y1, y2=y2) augImage, augBox = aug(image=image, bounding_boxes=bb) augBox = fitCoords([augBox.y1_int, augBox.x1_int, augBox.y2_int, augBox.x2_int], augImage.shape[:2])
def test_FastSnowyLandscape(): reseed() # check parameters aug = iaa.FastSnowyLandscape(lightness_threshold=[100, 200], lightness_multiplier=[1.0, 4.0]) assert isinstance(aug.lightness_threshold, iap.Choice) assert len(aug.lightness_threshold.a) == 2 assert aug.lightness_threshold.a[0] == 100 assert aug.lightness_threshold.a[1] == 200 assert isinstance(aug.lightness_multiplier, iap.Choice) assert len(aug.lightness_multiplier.a) == 2 assert np.allclose(aug.lightness_multiplier.a[0], 1.0) assert np.allclose(aug.lightness_multiplier.a[1], 4.0) # basic functionality test aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=2.0) image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) mask = (image_hls[..., 1] < 100) expected = np.copy(image_hls).astype(np.float32) expected[..., 1][mask] *= 2.0 expected = np.clip(expected, 0, 255).astype(np.uint8) expected = cv2.cvtColor(expected, cv2.COLOR_HLS2RGB) observed = aug.augment_image(image) assert np.array_equal(observed, expected) # test when varying lightness_threshold between images image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) class _TwoValueParam(iap.StochasticParameter): def __init__(self, v1, v2): super(_TwoValueParam, self).__init__() self.v1 = v1 self.v2 = v2 def _draw_samples(self, size, random_state): arr = np.full(size, self.v1, dtype=np.float32) arr[1::2] = self.v2 return arr aug = iaa.FastSnowyLandscape(lightness_threshold=_TwoValueParam(75, 125), lightness_multiplier=2.0) mask = (image_hls[..., 1] < 75) expected1 = np.copy(image_hls).astype(np.float64) expected1[..., 1][mask] *= 2.0 expected1 = np.clip(expected1, 0, 255).astype(np.uint8) expected1 = cv2.cvtColor(expected1, cv2.COLOR_HLS2RGB) mask = (image_hls[..., 1] < 125) expected2 = np.copy(image_hls).astype(np.float64) expected2[..., 1][mask] *= 2.0 expected2 = np.clip(expected2, 0, 255).astype(np.uint8) expected2 = cv2.cvtColor(expected2, cv2.COLOR_HLS2RGB) observed = aug.augment_images([image] * 4) assert np.array_equal(observed[0], expected1) assert np.array_equal(observed[1], expected2) assert np.array_equal(observed[2], expected1) assert np.array_equal(observed[3], expected2) # test when varying lightness_multiplier between images aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=_TwoValueParam(1.5, 2.0)) mask = (image_hls[..., 1] < 100) expected1 = np.copy(image_hls).astype(np.float64) expected1[..., 1][mask] *= 1.5 expected1 = np.clip(expected1, 0, 255).astype(np.uint8) expected1 = cv2.cvtColor(expected1, cv2.COLOR_HLS2RGB) mask = (image_hls[..., 1] < 100) expected2 = np.copy(image_hls).astype(np.float64) expected2[..., 1][mask] *= 2.0 expected2 = np.clip(expected2, 0, 255).astype(np.uint8) expected2 = cv2.cvtColor(expected2, cv2.COLOR_HLS2RGB) observed = aug.augment_images([image] * 4) assert np.array_equal(observed[0], expected1) assert np.array_equal(observed[1], expected2) assert np.array_equal(observed[2], expected1) assert np.array_equal(observed[3], expected2) # test BGR colorspace aug = iaa.FastSnowyLandscape(lightness_threshold=100, lightness_multiplier=2.0, from_colorspace="BGR") image = np.arange(0, 6*6*3).reshape((6, 6, 3)).astype(np.uint8) image_hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS) mask = (image_hls[..., 1] < 100) expected = np.copy(image_hls).astype(np.float32) expected[..., 1][mask] *= 2.0 expected = np.clip(expected, 0, 255).astype(np.uint8) expected = cv2.cvtColor(expected, cv2.COLOR_HLS2BGR) observed = aug.augment_image(image) assert np.array_equal(observed, expected)
iaa.Add((-10, 10), per_channel=0.5), # 像素乘上0.5或者1.5之间的数字. iaa.Multiply((0.8, 1.2), per_channel=0.5), # 将整个图像的对比度变为原来的一半或者二倍 iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5), # 将RGB变成灰度图然后乘alpha加在原图上 iaa.Grayscale(alpha=(0.0, 0.2)), #把像素移动到周围的地方。这个方法在mnist数据集增强中有见到 # sometimes( # iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) # ), # 扭曲图像的局部区域 sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), iaa.GammaContrast((0.5, 1.5)), iaa.FastSnowyLandscape(lightness_threshold=(100, 200), lightness_multiplier=(0.5, 1.5)), iaa.Snowflakes(), iaa.AddToSaturation((-50, 50)) ], random_order=True # 随机的顺序把这些操作用在图像上 ) ]) #seq = iaa.Grayscale(alpha=(0.1, 0.4))#以0.1-0.4的概率对图像进行灰度化 #iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), #seq = iaa.GammaContrast((0.5,2.0)) #seq = iaa.FastSnowyLandscape(lightness_threshold=(100, 255),lightness_multiplier=(0.5, 1.5)) #seq = iaa.Fog() #seq = iaa.Snowflakes() #seq = iaa.Clouds() #seq = iaa.AddToSaturation((-50,50)) seq_det = seq.to_deterministic()
iaa.BlendAlphaElementwise( (0.0, 1.0), foreground=iaa.Add(100), background=iaa.Multiply(0.2)), iaa.BlendAlphaSimplexNoise(iaa.EdgeDetect(1.0)), iaa.BlendAlphaSimplexNoise( iaa.EdgeDetect(1.0), upscale_method="nearest"), iaa.BlendAlphaSimplexNoise( iaa.EdgeDetect(1.0), upscale_method="linear"), iaa.BlendAlphaSomeColors(iaa.TotalDropout(1.0)), iaa.BlendAlphaSomeColors( iaa.AveragePooling(7), alpha=[0.0, 1.0], smoothness=0.0), iaa.FastSnowyLandscape( lightness_threshold=140, lightness_multiplier=2.5), iaa.Clouds(), iaa.Fog(), iaa.Rain(speed=(0.1, 0.3)), ], random_order=True ) ], random_order=True ) images_aug = seq(images=images) i = 0 for img in images_aug:
import imgaug as ia from imgaug import augmenters as iaa import numpy as np import imageio ia.seed(1) img = imageio.imread("test.jpg") #read you image images = np.array( [img for _ in range(32)], dtype=np.uint8) # 32 means creat 32 enhanced images using following methods. seq = iaa.Sequential( [ iaa.FastSnowyLandscape(64,1.5), iaa.Snowflakes(0.075,0.9,0.7,0.8,30,0.03) ], random_order=True) # apply augmenters in random order images_aug = seq.augment_images(images) for i in range(32): imageio.imwrite(str(i)+'new.jpg', images_aug[i]) #write all changed images
return result def _augment_keypoints(self, keypoints_on_images, random_state, parents, hooks): return keypoints_on_images def _augment_heatmaps(self, **kwargs): return def get_parameters(self): return [self.abc] augment_object = iaa.Sequential([ iaa.Add((-20, 20)), iaa.Sometimes(0.5, iaa.AdditiveGaussianNoise(scale=0.03 * 255)), iaa.Sometimes(0.5, iaa.MotionBlur(angle=(0, 360))), iaa.Sometimes(0.2, iaa.GammaContrast(gamma=(0.5, 1.44))), iaa.Sometimes(0.1, iaa.FastSnowyLandscape(lightness_threshold=(0, 150))), iaa.OneOf([ iaa.Sometimes(0.8, RandomShadow()), iaa.Sometimes(0.4, RandomGravel()), iaa.Sometimes(0.2, RandomSunFlare()), iaa.Sometimes(0.3, RandomMotionBlur()) ]) ]) def augment_image(img): return augment_object.augment_image(img)