Python源码示例:imgaug.augmenters.Sharpen()
示例1
def chapter_augmenters_sometimes():
aug = iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=2.0))
run_and_save_augseq(
"sometimes.jpg", aug,
[ia.quokka(size=(64, 64)) for _ in range(16)], cols=8, rows=2,
seed=2
)
aug = iaa.Sometimes(
0.5,
iaa.GaussianBlur(sigma=2.0),
iaa.Sequential([iaa.Affine(rotate=45), iaa.Sharpen(alpha=1.0)])
)
run_and_save_augseq(
"sometimes_if_else.jpg", aug,
[ia.quokka(size=(64, 64)) for _ in range(16)], cols=8, rows=2
)
示例2
def chapter_augmenters_sharpen():
aug = iaa.Sharpen(alpha=(0.0, 1.0), lightness=(0.75, 2.0))
run_and_save_augseq(
"sharpen.jpg", aug,
[ia.quokka(size=(64, 64)) for _ in range(16)], cols=8, rows=2
)
#alphas = [1/8*i for i in range(8)]
alphas = np.linspace(0, 1.0, num=8)
run_and_save_augseq(
"sharpen_vary_alpha.jpg",
[iaa.Sharpen(alpha=alpha, lightness=1.0) for alpha in alphas],
[ia.quokka(size=(64, 64)) for _ in range(8)], cols=8, rows=1,
quality=90
)
#lightnesses = [1/8*i for i in range(8)]
lightnesses = np.linspace(0.75, 1.5, num=8)
run_and_save_augseq(
"sharpen_vary_lightness.jpg",
[iaa.Sharpen(alpha=1.0, lightness=lightness) for lightness in lightnesses],
[ia.quokka(size=(64, 64)) for _ in range(8)], cols=8, rows=1,
quality=90
)
示例3
def test_alpha_zero(self):
aug = iaa.Sharpen(alpha=0, lightness=1)
observed = aug.augment_image(self.base_img)
expected = self.base_img
assert np.allclose(observed, expected)
示例4
def test_alpha_one(self):
aug = iaa.Sharpen(alpha=1.0, lightness=1)
observed = aug.augment_image(self.base_img)
expected = self.base_img_sharpened
assert np.allclose(observed, expected)
示例5
def test_alpha_050(self):
aug = iaa.Sharpen(alpha=0.5, lightness=1)
observed = aug.augment_image(self.base_img)
expected = self._compute_sharpened_base_img(
0.5*1, 0.5 * self.m_noop + 0.5 * self.m)
assert np.allclose(observed, expected.astype(np.uint8))
示例6
def test_alpha_075(self):
aug = iaa.Sharpen(alpha=0.75, lightness=1)
observed = aug.augment_image(self.base_img)
expected = self._compute_sharpened_base_img(
0.75*1, 0.25 * self.m_noop + 0.75 * self.m)
assert np.allclose(observed, expected)
示例7
def test_alpha_is_stochastic_parameter(self):
aug = iaa.Sharpen(alpha=iap.Choice([0.5, 1.0]), lightness=1)
observed = aug.augment_image(self.base_img)
expected1 = self._compute_sharpened_base_img(
0.5*1, 0.5 * self.m_noop + 0.5 * self.m)
expected2 = self._compute_sharpened_base_img(
1.0*1, 0.0 * self.m_noop + 1.0 * self.m)
assert (
np.allclose(observed, expected1)
or np.allclose(observed, expected2)
)
示例8
def test_alpha_1_lightness_2(self):
aug = iaa.Sharpen(alpha=1.0, lightness=2)
observed = aug.augment_image(self.base_img)
expected = self._compute_sharpened_base_img(1.0*2, self.m)
assert np.allclose(observed, expected)
示例9
def test_alpha_1_lightness_3(self):
aug = iaa.Sharpen(alpha=1.0, lightness=3)
observed = aug.augment_image(self.base_img)
expected = self._compute_sharpened_base_img(1.0*3, self.m)
assert np.allclose(observed, expected)
示例10
def test_alpha_1_lightness_is_stochastic_parameter(self):
aug = iaa.Sharpen(alpha=1.0, lightness=iap.Choice([1.0, 1.5]))
observed = aug.augment_image(self.base_img)
expected1 = self._compute_sharpened_base_img(1.0*1.0, self.m)
expected2 = self._compute_sharpened_base_img(1.0*1.5, self.m)
assert (
np.allclose(observed, expected1)
or np.allclose(observed, expected2)
)
示例11
def test_failure_if_lightness_has_bad_datatype(self):
# don't use assertRaisesRegex, because it doesnt exist in 2.7
got_exception = False
try:
_ = iaa.Sharpen(alpha=1.0, lightness="test")
except Exception as exc:
assert "Expected " in str(exc)
got_exception = True
assert got_exception
# this part doesnt really work so far due to nonlinearities resulting
# from clipping to uint8
示例12
def test_pickleable(self):
aug = iaa.Sharpen(alpha=(0.0, 1.0), lightness=(1, 3), seed=1)
runtest_pickleable_uint8_img(aug, iterations=20)
示例13
def __init__(self, alpha=(0.2, 0.5), lightness=(0.5, 1.), prob=0.5):
super().__init__(prob)
self.processor = iaa.Sharpen(alpha, lightness)
示例14
def __init__(self, alpha=(0.2, 0.5), lightness=(0.5, 1.), prob=0.5):
super().__init__(prob)
self.processor = iaa.Sharpen(alpha, lightness)
示例15
def __init__(self, alpha=(0.2, 0.5), lightness=(0.5, 1.), prob=0.5):
super().__init__(prob)
self.processor = iaa.Sharpen(alpha, lightness)
示例16
def chapter_augmenters_someof():
aug = iaa.SomeOf(2, [
iaa.Affine(rotate=45),
iaa.AdditiveGaussianNoise(scale=0.2*255),
iaa.Add(50, per_channel=True),
iaa.Sharpen(alpha=0.5)
])
run_and_save_augseq(
"someof.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
aug = iaa.SomeOf((0, None), [
iaa.Affine(rotate=45),
iaa.AdditiveGaussianNoise(scale=0.2*255),
iaa.Add(50, per_channel=True),
iaa.Sharpen(alpha=0.5)
])
run_and_save_augseq(
"someof_0_to_none.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
aug = iaa.SomeOf(2, [
iaa.Affine(rotate=45),
iaa.AdditiveGaussianNoise(scale=0.2*255),
iaa.Add(50, per_channel=True),
iaa.Sharpen(alpha=0.5)
], random_order=True)
run_and_save_augseq(
"someof_random_order.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
示例17
def chapter_augmenters_oneof():
aug = iaa.OneOf([
iaa.Affine(rotate=45),
iaa.AdditiveGaussianNoise(scale=0.2*255),
iaa.Add(50, per_channel=True),
iaa.Sharpen(alpha=0.5)
])
run_and_save_augseq(
"oneof.jpg", aug,
[ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2
)
示例18
def processor(self):
return iaa.Sharpen(self.alpha, self.lightness)
示例19
def _create_augment_pipeline():
from imgaug import augmenters as iaa
### augmentors by https://github.com/aleju/imgaug
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
# Define our sequence of augmentation steps that will be applied to every image
# All augmenters with per_channel=0.5 will sample one value _per image_
# in 50% of all cases. In all other cases they will sample new values
# _per channel_.
aug_pipe = iaa.Sequential(
[
# apply the following augmenters to most images
#iaa.Fliplr(0.5), # horizontally flip 50% of all images
#iaa.Flipud(0.2), # vertically flip 20% of all images
#sometimes(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width
sometimes(iaa.Affine(
#scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
#translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
#rotate=(-5, 5), # rotate by -45 to +45 degrees
#shear=(-5, 5), # shear by -16 to +16 degrees
#order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
#cval=(0, 255), # if mode is constant, use a cval between 0 and 255
#mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
)),
# execute 0 to 5 of the following (less important) augmenters per image
# don't execute all of them, as that would often be way too strong
iaa.SomeOf((0, 5),
[
#sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
iaa.OneOf([
iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
]),
iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
#iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
# search either for all edges or for directed edges
#sometimes(iaa.OneOf([
# iaa.EdgeDetect(alpha=(0, 0.7)),
# iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
#])),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
iaa.OneOf([
iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
#iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
]),
#iaa.Invert(0.05, per_channel=True), # invert color channels
iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
iaa.Multiply((0.5, 1.5), per_channel=0.5), # change brightness of images (50-150% of original value)
iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
#iaa.Grayscale(alpha=(0.0, 1.0)),
#sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
#sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) # sometimes move parts of the image around
],
random_order=True
)
],
random_order=True
)
return aug_pipe
示例20
def get_augmentations():
# applies the given augmenter in 50% of all cases,
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
# Define our sequence of augmentation steps that will be applied to every image
seq = iaa.Sequential([
# execute 0 to 5 of the following (less important) augmenters per image
iaa.SomeOf((0, 5),
[
iaa.OneOf([
iaa.GaussianBlur((0, 3.0)),
iaa.AverageBlur(k=(2, 7)),
iaa.MedianBlur(k=(3, 11)),
]),
iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
# search either for all edges or for directed edges,
# blend the result with the original image using a blobby mask
iaa.SimplexNoiseAlpha(iaa.OneOf([
iaa.EdgeDetect(alpha=(0.5, 1.0)),
iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
])),
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
iaa.OneOf([
iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
]),
iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
# either change the brightness of the whole image (sometimes
# per channel) or change the brightness of subareas
iaa.OneOf([
iaa.Multiply((0.5, 1.5), per_channel=0.5),
iaa.FrequencyNoiseAlpha(
exponent=(-4, 0),
first=iaa.Multiply((0.5, 1.5), per_channel=True),
second=iaa.ContrastNormalization((0.5, 2.0))
)
]),
iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
],
random_order=True
)
],
random_order=True
)
return seq
### data transforms