Is there a better histogram solution?

Recently, I looked over to my variant of colormap (rep_colormap) and David’s variant of colormap. My helper code do allow one to generate a full colormap to appended histogram into it. And this has me thinking. Here’s the problem I have.

# This is a sketch
[0] => Image with color and frequency. [c0,...,cL]. All c0 to cL-1 is color channel. cL is freq.
[1] => Reduced color palette. No frequency here.

Is there a good solution that’s not slower than using the whole image and histogram which is seen in gmic-stdlib and current published rep_colormap.

I’m sure there is, my current solution is to use eval after the index (which collects the closest color index), and use histogram image that has been separated from [0] to sum a indice of a vector which size is equal to the number of reduced color. That indice correspond to closest color.

Another solution is to use eval to do both. However, still slower than the standard solution, but it is at least a few ms slower this way.

Both techniques wastes execution time, but also can be better if the number of unique color is small.

You can test the idea with this code in user.gmic:

And this in cli:

$ sp greece tic rep_colormap 5,-,0,2 toc

By removing # with the commented display, you can see the snapshot of the sketch.

I’m sorry @Reptorian , but from here on, I don’t understand what you’re trying to do.
A solution to what problem?

Could you please describe precisely what you are trying to achieve with these two images in the list ? And what is the algorithm you have in mind ?

Sort a reduced color by decreasing occurance. The catch is that instead of using the whole image like in your colormap approach, you use a color histogram image (color+frequency).

Right now, I’m only seeing this approach is faster for small histogram (256 colors), but not when there’s a lot of colors as the C++ commands are naturally faster.

Algorithm is simply index and then the index is used as accumulator using the frequency channel.

Command sort is able to sort image data according to the values defined in the first column/row/slice/channel, not sure if that is something you are looking for, but here is an example:

foo :
  sp colorful
  100%,100%,1,1,u a[-2,-1] c # Append a channel of random values
  w,h:=w,h
  r {wh},1,1,100%,-1 shift 0,0,0,1,2 # Make sure rand values are the first channel
  sort +,x
  shift 0,0,0,-1,2 channels 0,{s-2} r $w,$h,1,100%,-1 # Go back to original image size

The main difficulty here is just to arrange the image values so that sort can process them.