v9.1.1
Menu

Color Quantization using Median Cut

hardPROColor SpacesComputer Vision 2

Given a list of RGB pixels and a target number of colors K, reduce the color palette using the median cut algorithm.

Algorithm:

  1. Start with all pixels in one bucket
  2. While the number of buckets < K:
    • Find the bucket with the most pixels
    • Determine which color channel (R=0, G=1, B=2) has the greatest range (max minus min) in that bucket. If channels tie, pick the lowest-numbered one (R before G before B)
    • Sort the bucket by that channel (a stable sort: pixels with equal values keep their current order)
    • Split at the median index ⌊n/2⌋\lfloor n/2 \rfloor into two halves (the first half gets ⌊n/2⌋\lfloor n/2 \rfloor pixels)
    • If several buckets tie for the most pixels, split the one created earliest (a lower half counts as created before its upper half)
  3. For each bucket, compute the representative color as the per-channel mean of all its pixels, rounded to the nearest integer using Python's built-in rounding (an exact half goes to the nearest even integer, so 0.5→00.5 \to 0 and 127.5→128127.5 \to 128)
  4. Sort output colors by R, then G, then B (ascending)

Return the list of K representative colors.

Example

Input
pixels = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0]]
K = 2
Output
[[0, 128, 128], [255, 128, 0]]
Reasoning
  • Input: pixels holds 4 colors, each a triple of channel values in [0,255][0, 255]: red, green, blue and yellow. K = 2 means the palette must end with exactly two buckets, so one split.
  • Choosing the channel: in the single starting bucket every channel spans 255−0=255255 - 0 = 255. All three tie, so R wins the tie.
  • Splitting: ordered by R the bucket reads green (0,255,0)(0,255,0), blue (0,0,255)(0,0,255), red (255,0,0)(255,0,0), yellow (255,255,0)(255,255,0), with the two R = 0 pixels keeping their original order. With n=4n = 4 the cut falls after ⌊4/2⌋=2\lfloor 4/2 \rfloor = 2 pixels: {green, blue} and {red, yellow}.
  • Averaging: {green, blue} gives (0+02,255+02,0+2552)=(0,127.5,127.5)\left(\frac{0+0}{2}, \frac{255+0}{2}, \frac{0+255}{2}\right) = (0, 127.5, 127.5), which rounds to (0,128,128)(0, 128, 128). {red, yellow} gives (255,127.5,0)(255, 127.5, 0), which rounds to (255,128,0)(255, 128, 0).
  • Output: ordered by R, the result is [[0, 128, 128], [255, 128, 0]].
  • Notice the tie rule matters here: splitting on G instead would pair {red, blue} and {green, yellow}, giving (128,0,128)(128, 0, 128) and (128,255,0)(128, 255, 0), a different palette.

Constraints

  • pixels is a list of [R, G, B] lists
  • K >= 1 and K <= len(pixels)
  • Return K colors as [R, G, B] lists (integer values)
  • Sort output by R, then G, then B

More from Computer Vision 2

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.