Loading...
Implement K-means clustering for image color quantization using NumPy.
Color quantization reduces the number of distinct colors in an image, used for compression and artistic effects.
Algorithm:
Distance metric: d(p,c)=(rp−rc)2+(gp−gc)2+(bp−bc)2
image = [[[255,0,0], [0,255,0]],
[[0,0,255], [255,255,255]]]
k = 2Colors quantized to 2 clusters
Step 1: Reshape Pixels: [[255,0,0], [0,255,0], [0,0,255], [255,255,255]]
Step 2: Initialize centers Random selection: e.g., [255,0,0] and [255,255,255]
Step 3: Iterate Assign pixels to nearest center, update centers...
After convergence, each pixel is replaced by its cluster center color.
With k=2 on this image, typical result groups similar colors together.