Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color quantization #347

Merged
merged 12 commits into from
Jul 23, 2019
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
* [rarely breaking] Added a `pad_mode` argument to `imgaug.pool()`,
`imgaug.avg_pool()`, `imgaug.max_pool()`, `imgaug.min_pool()` and
`imgaug.median_pool()`. This breaks code relying on the order of the
functions arguments.
functions arguments.s
* Changed the default `pad_mode` of `avg_pool` from `constant` (`cval=128`)
to `reflect`.
* Changed the default `pad_mode` of `max_pool` from `constant` (`cval=0`)
Expand All @@ -137,6 +137,18 @@
* Renamed argument `cval` to `pad_cval` in `imgaug.pool()`,
`imgaug.avg_pool()` and `imgaug.max_pool()`. The old name `cval` is now
deprecated.
* Added `augmenters.color._AbstractColorQuantization`. #347
* Added `augmenters.color.KMeansColorQuantization` and corresponding
`augmenters.color.quantize_colors_kmeans()`. Both deal with quantizing
similar colors using k-Means clustering. #347
* Added a check script for `KMeansColorQuantization` under
`checks/check_kmeans_color_quantization.py`. #347
* Added `augmenters.color.UniformColorQuantization` and corresponding
`augmenters.color.quantize_colors_uniform()`. Both deal with quantizing
similar colors using k-Means clustering. #347
* Added a check script for `UniformColorQuantization` under
`checks/check_uniform_color_quantization.py`. #347


## Fixes

Expand Down
31 changes: 31 additions & 0 deletions checks/check_kmeans_color_quantization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import print_function, division
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa


def main():
image = ia.quokka_square((256, 256))
image_q2 = iaa.quantize_colors_kmeans(image, 2)
image_q16 = iaa.quantize_colors_kmeans(image, 16)
ia.imshow(np.hstack([image_q2, image_q16]))

from_cs = "RGB"
to_cs = ["RGB", "Lab"]
kwargs = {"from_colorspace": from_cs, "to_colorspace": to_cs}
augs = [
iaa.KMeansColorQuantization(2, **kwargs),
iaa.KMeansColorQuantization(4, **kwargs),
iaa.KMeansColorQuantization(8, **kwargs),
iaa.KMeansColorQuantization((2, 16), **kwargs),
]

images_aug = []
for aug in augs:
images_aug.extend(aug(images=[image]*8))

ia.imshow(ia.draw_grid(images_aug, cols=8))


if __name__ == "__main__":
main()
25 changes: 25 additions & 0 deletions checks/check_uniform_color_quantization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import print_function, division
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa


def main():
image = ia.quokka_square((256, 256))
ia.imshow(
ia.draw_grid([
iaa.quantize_colors_uniform(image, 2),
iaa.quantize_colors_uniform(image, 4),
iaa.quantize_colors_uniform(image, 8),
iaa.quantize_colors_uniform(image, 16),
iaa.quantize_colors_uniform(image, 32),
iaa.quantize_colors_uniform(image, 64)
], cols=6)
)

aug = iaa.UniformColorQuantization((2, 16))
ia.imshow(ia.draw_grid(aug(images=[image] * 16)))


if __name__ == "__main__":
main()
Loading