Finding most common colors?

    Nov 25 2006 | 2:47 am
    Hi All, I'm trying to select, say the first 2-3 most commonly occurring colors from a jitter matrix...ideally with a tolerance setting or something similar.
    Basically I want to look at video and set some DMX LED lights to be the same color as the primary and secondary colors from the video.
    Any hints or example patches? :)
    Thanks Much!

    • Nov 25 2006 | 3:07 am
    • Sep 12 2018 | 12:32 am
      Big bump! I want to do almost exactly what seanstevens here tried to do 12 years ago. Seems I'm a bit late to the party. I want to analyze the two most recurrent colors of a texture in realtime, to send color data to DMX lights immediately.
      I tried finding the color average of the entire matrix using jit.3m, but the results weren't very flattering, as can be seen in the example patch below. I guess a better approach would be to cathegorize some range of the recurrent colors, and finding some kind of mean value within that range.
      Does anyone have any idea how this can be done? Or even better, how to do it within the gen domain? (Not fond of CPU readbacks)
    • Sep 12 2018 | 10:08 pm
      I found that thread while doing research, read it carefully, and checked out the shaders and patch. It looks too computationally heavy sadly, as I need to analyze a texture in realtime to link the colors of DMX lights to the colors of the visuals. A rougher, realtime process is what I need to achieve, but I'm not even sure it's possible to analyze like that in Also, it seemed like I needed JRE 2 to run this, I only got error messages with an updated java installation. My flaw was not in not searching properly this time, but rather in not clearly stating that I need this to happen in realtime. I'll update my post, hope I didn't waste anyones time!
    • Sep 13 2018 | 5:23 am
      You can downsample the incoming texture if needed, which will make the process less CPU intensive but also less accurate. Without a working java install it would be hard for you to evaluate this, however.
      Getting this type of sorting into a low-latency, highly accurate scenario would likely require you to write your own Max object in C and use more efficient sorting algorithm than the one I devised. The Java system I shared was not designed for real-time and was intended to be a proof of concept that others could take further, although it appears no one has done that yet.
      I don't think is a suitable environment for sorting because it's aimed at per-pixel operations performed in parallel. Obviously you could re-implement the color space translations in gen, but not sure why you would since the shaders are already written.
    • Sep 14 2018 | 12:28 pm
      Thank you for your answer Jesse, your project was a great contribution.
      The end result for the real-time version I would like to make wouldn't have to be a very accurate color analysis. Downsampling the texture and making it fairly low res would be good enough for the job I figure, since the most recurrent colors will probably still be represented.
      You are probably right about not being suited for the job. I had a similar issue in another project where I wanted the cells to "talk to each other" to calculate euclidean distance between each cell, where I wanted to try iteration within gen to solve it. A max object written in C happened to be the best solution for that problem. This is the second time I would need to code something in C, and I feel powerless, as I can only program in Max. I'm not sure if it's worth taking the time to learn coding, rather than just pay someone to make the objects for me.
      Is it possible to do the analysis with GLSL? Just curious.
    • Sep 14 2018 | 6:41 pm
      maybe you can utilize a less sophisticated technique and get acceptable / usable results.
      this patch performs a parallel reduction to 4x4, sampling 4 times around the pixel and outputting the average color with each reduction. (in the patch, i show the comparison with the output from a single downsample to 4x4).
      I believe this type of technique is generally useful for color analysis on the gpu.
    • Sep 16 2018 | 5:09 pm
      That's amazing Rob, really clever! I'll play around with this technique, and pick out some colors from this, see where it leads me. If I can expand to something worth sharing, I'll share it.
      That link looks like a gold mine as well. Exciting reading material. I'm gonna be on the lookout for more informative places on the web like that one.