# Creating dynamic array

**URL:** https://discourse.processing.org/t/creating-dynamic-array/31794
**Category:** Coding Questions
**Created:** [August 19, 2021, 7:48pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794 "2021-08-19T19:48:31Z")
**Posts on this page:** 1
**Showing post:** 11

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [August 22, 2021, 1:03am UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/11 "2021-08-22T01:03:48Z")

</div>

There are many tools and libraries focused on counting groups of colors in an image. In general, counting many colors as one color is called “color quantization”:

> **[Color quantization](https://en.wikipedia.org/wiki/Color_quantization)**
>
> In computer graphics, color quantization or color image quantization is quantization applied to color spaces; it is a process that reduces the number of distinct colors used in an image, usually with the intention that the new image should be as visually similar as possible to the original image. Computer algorithms to perform color quantization on bitmaps have been studied since the 1970s. Color quantization is critical for displaying images with many colors on devices that can only display a li...

Related past discussion:

> [@How to apply color quantization to an image](https://discourse.processing.org/t/how-to-apply-color-quantization-to-an-image/4849/21):
>
> This is an interesting problem and I used to use it years ago to keep image file sizes down but I can’t remember what software I used. So it piqued my interest enough to create a sketch that uses the Quantize class code in the link provided by @jb4x (thanks). So here is the result ouput from my sketch [sample] I have simply created a new tab called Quantize.java and copied the code from the link into it. NOTE that the tab name must match precisely the class name to make it a top level class. …

If you want to group colors using a strategy that maintains visual qualities of the original color space, perhaps start with Median Cut – we had a [past conversation](https://discourse.processing.org/t/how-to-apply-color-quantization-to-an-image/4849/22) pointing to the Java implementation of Median Cut available from ImageJ. javax also has a [ColorQuantizer](https://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/ColorQuantizerDescriptor.html).

OpenCV also has some quantization built-in via k-means clustering. There is a good discussion of approaches using OpenCV here:

> <https://stackoverflow.com/questions/5906693/how-to-reduce-the-number-of-colors-in-an-image-with-opencv>

I’m guessing that is what you mean – rather than using a stock palette, you want to adaptively choose a small number of colors that best approximate the appearance of your original image. Is that right?

---

_[View the full topic](https://discourse.processing.org/t/creating-dynamic-array/31794)._
