![]() To my eyes, there is a subtle difference in the quality of the colors. We will create a new class to enable this:Ĭomparison of whole image palette reduction and unique pixels palette reduction on different imagesįrom afar, there are very small differences: there seems to be about the same distribution of colors in the reduced palettes which can be attributed to the initial conditions of the k-means clustering algorithm. For fun, let’s hack around and do clustering only on the unique pixels in the image (instead of all the pixels) and give them each equal opportunity. Naturally, this will skew cluster centers around more abundant colors in the image these are the colors that would immediately pop in the original image making them visually salient. Chances are, around a particular region of an image, there are maybe tens or hundreds of the same pixel RGB value (particularly for static backgrounds, skies, and the like). We did clustering based on all the pixels in the image but in reality, not all the pixels are unique. Let’s experiment a bit with this before moving on. It’s because the colors in the transferring image don’t get a good enough color to swap with but instead takes the nearest cluster center despite it being very distant to the transferring pixel value. This is why in situations where the source image doesn’t have many colors, or the reduced palette is too small, we get some cartoon-like effects in the overall image. The RGB values of the transferring pixel are then swapped with the closest cluster center in the palette. What happens here is that the distance of each pixel in the new image is measured against the reduced palette (i.e. From left to right, original image by MIO ITO, Daniel Roe, and Carles Rabada on Unsplash Transferring the reduced palette of Gozha’s flower onto other images. Here's what a reduced palette class could look like: The simple solution to this is just to round up these values to the nearest integer and cast it to np.uint8 type before displaying it. When we call kmn.predict(src), the output values are all floats. We have to be careful as the cluster centers are not np.uint8 type but rather np.float64. The code for this is relatively straightforward: run k-means clustering on all the pixels of an image and the resulting cluster centers are your reduced palette. Palette reduction is the algorithmic selection of a subset of the original palette of an image and mapping of all pixel values in the original palette to the closest values in the reduced palette. ![]() This number is usually in the tens of thousands for a small photographic image. What I mean by palette is the set of RGB pixel values in an image. So let’s get some definitions in place before anything else. Palette reduction with k-means clustering Look for the code on a prepared Colab notebook containing everything explained in this article. Get ready to use tools from numpy, scikit-learn and dask. In this article, I’m going to explain how this artificial task of palette transfer can be done and how to take it further. While playing around, I casually thought: “what if we transferred the reduced palette of one image into another?” It’s a seemingly innocuous question that expanded my knowledge of colors and parallel-processing toolbox. Today’s use? Well, if you’re particularly nostalgic about the artistry of low-bit graphics or building something to display on a LED matrix, this could be the use case. since only 8 bits or 16 bits are allocated to the entire color palette (in contrast to the 24-bit RGB that is ubiquitous now). By reducing the palette, you can conform to the limited color palette of PAL or SECAM, etc. This was particularly useful in the old days of video game graphics if an image needed to be rendered in a device with limited memory. The number of colors that you want in your reduced palette is the k value in k-means clustering in other words the number of cluster centers in RGB space that your image inhabit. This is achieved by doing k-means clustering on the RGB values of the pixels in an image. ![]() ![]() I recall -from an image processing class back in college - that you can reduce the number of unique colors in an image down to a few colors while keeping the important ones. A few weekends ago, I wanted to brush up on my clustering techniques. ![]()
0 Comments
Leave a Reply. |