Palette to clut to application

Indeed, I messed up the PNG. I updated the post with palettes A and B. Thanks for taking the time to contribute.

I will do tests with the 2 new pallets in the evening (Eastern time).
:o)

Sorry about the poor first answer, I ran out of time there. G’MIC in fact has so many ways of dealing with colour mapping I can hardly cover it all. CLUTs first:

gmic clut summer image.jpg map_clut. ..

That’s how to generate one of the pre-supplied cluts and apply it. Identity clut I’m not so sure about (there could be a command), but it’s not too hard to make one. Here’s one such method in a split up way you can play around with:

gmic 8,1,1,3,"int(x/(2^c))%2" r 2,2,2,3,-1 r 64,64,64,3,3 r 512,512,1,3,-1

Which is generate a starting clut, resize to a cube with no interpolation, resize to a larger cube with interpolation, resize to a 2d image for viewing.

A list of some commands you can use for colour mapping: index, map, warp, colormap, autoindex, quantize, patchmatch. Here’s an example with warp (I’m not declaring it looks great, it’s only for example!):

gmic pal.png colormap 5 r 4096,1,1,3,3 boats.jpg norm. n. 0,4096 warp.. .,0

boats2

1 Like

Here are the new tests with the 2 new pallets.

test_transfert_palette_v2.xcf.bz2 (4.0 MB)

1 Like

test_transfert_palette_v3.xcf.bz2 (4.0 MB)

The second one in your first post is really nice, as it doesn’t look posterised yet. It has a great balance between toned but still kinda real looking!
:+1:t4:

Thank you.
To prevent posterization of the image, it is necessary to obtain a large number of colors from the original palette.
Gradients in CIE mode provide a large number of colors without intermediate colors out of range.
David Tschumperlé’s Transfer colors [advanced] filter provides results with many nuances :o)

I haven’t used resize in this way before. Much to learn. If you please, it would be nice to know how to go back and forth among the different representations. E.g., do and apply @Morgan_Hardwood’s approach #1 in gmic.

I’m never sure of the most efficient way, but here’s a possibility. First lets define two commands to make the example clearer:

#@cli hald
#@cli : Insert identity CLUT at the end of the image list.
hald: skip ${1=2}
  e. "Input identity CLUT with resolution $1."
  v - b={int($1)^2} s={$b^1.5}
  (0,1;0,1/0,1;0,1^0,0;1,1/0,0;1,1^0,0;0,0/1,1;1,1)
  *. 255 r. $b,$b,$b,3,3 r. $s,$s,1,3,-1
  nm "[CLUT: identity]" v +

#@cli rle_values
#@cli : Reduce by repeating values removal.
rle_values:
  e. "Reduce image$? by repeating values removal." v -
  repeat $! -l[$>] r {w},1 f "i!=j(-1)?i:0;" s c discard 0 a c
  endl done nm "[RLE: values]" v +

The first is obviously to generate an identity clut of desired size. The second is to easily reduce the input image to single pixel values - we could of course create this by hand as above but I like the idea of making a command :slight_smile:
Now we can do this:

gmic pal2.png rle_values transpose hald 5 -r.. . norm. orientation.. -* boats.jpg map_clut. ..

whereby we turn the input colours into a 5 pixel vector, generate a clut, resize the palette and combine them (OK, I did cheat here using unit colour vectors and norm but the principal is similar). The last step is as above applying the clut to the image.

As an aside the resize command is very useful for interpolation and averaging. For example you can find the average r,g,b values of an image simply by resizing to 1x1x1x3 in average mode.

1 Like

@samj Do the sizes of the gradients matter when you feed them to fx_transfer_rgb? I am guessing that the command only looks for unique colors…

@afre
The result depends on the size of the color gradient.
I add a file with examples (xcf for Gimp 2.9.9).
test_transfert_palette_v4.xcf.bz2 (6.9 MB)

I know that length matters. What isn’t clear to me is whether width does too, or height as shown below. It doesn’t introduce more in-between colors as length does. From the v3 example:

I guess I could test it myself :slight_smile:.

The width of the linear gradient will determine the number of colors. The height is not important.

1 Like

@samj I tried samj_Palette_De_Degrades in CLI and got an error:

[gmic]./samj_Palette_De_Degrades/ *** Error *** Command ‘rv’: Invalid selection [-2,-1] (contains indice ‘-2’, not in range -1…0).

Also, I was wondering whether you could make it an arbitrary number of colours. I may have less than or more than 12. Thanks!

I’ve quickly tested a simple idea, let me explain it briefly.

Considering one source image I and one reference image R, we want to generate an image J that looks like I but with the colors of R.
What I’ve tried is :

  1. Take all the existing colors in R and put them in a CLUT. We end up with a volumetric image with a few colored points, and black everywhere else (so a lot of black).
  2. Use the G’MIC solidify algorithm (command solidify) on that sparse CLUT, to fill the missing values by interpolation the known colors of R (it uses diffusion PDEs for that task). → Now, we have a full CLUT, with only interpolated colors from R.
  3. Apply that CLUT to the source image I to transfer the colors from R to I.

This is done basically by this code sample:

trcol :
  sp butterfly,dog  # Source and reference images
  64,64,64,4        # Resolution of the 3D CLUT : 64x64x64, with alpha-channel
  f.. "I(#-1,round(I/256*w#-1)) = [ I,255 ]"  # Fill CLUT with known colors of `R`.
  solidify. 75%,1,20  # CLUT completion -> all CLUT entries will get a color.
  +map_clut... .  # Apply that CLUT to the source image

I get something like this, for instance (Butterfly = source, Dog = reference) :

Another example (Pencils = source, Wall = reference) :

And another one (Greece = source, David = reference) :

Of course, if your reference image has a lot of different colors in it, there won’t be a lot of difference between the source image I and the generated image J.

Maybe this could be an idea for a new G’MIC filter in the plug-in, what do you think ?

2 Likes

@afre

This command is very slow if you use G’MIC CLI.
The number of colors with the original script is limited to 12, you can adapt it according to your wishes, it is free software.
Here are 2 examples including one with 4 random colors.

gmic -v - -m sylvie_alexandre.gmic 1024,1024,1,3 -samj_Palette_De_Degrades 159,190,195,55,67,140,54,40,39,140,81,88,207,175,190,220,202,196,170,186,192,130,149,139,112,96,96,237,168,138,220,199,205,234,217,219,0,0,512,20 -rm[-1] -o degrade.png


gmic -v - -m sylvie_alexandre.gmic 1024,1024,1,3 -samj_Palette_De_Degrades {(u)*255}
,{(u)*255}
,{(u)*255}
,{(u)*255}
,{(u)*255}
,{(u)*255}
,{(u)*255}
,{(u)*255}
,{(u)*255}
,{(u)*255}
,{(u)*255}
,{(u)*255}
,207,175,190,220,202,196,170,186,192,130,149,139,112,96,96,237,168,138,220,199,205,234,217,219,0,0,512,20 -rm[-1] -o degrade_4couleurs_aleatoires.png

source : https://github.com/dtschump/gmic-community/raw/master/include/sylvie_alexandre.gmic

@David_Tschumperle

It’s very, very pretty
:o)

I do gravitate toward simplicity :tiger::mage:, which this is. I wonder if we could make pencils –CLUT→ wall more interesting colour-wise (short of machine learning :wink:).

1 Are commands faster in the plugin?
2 I suppose I could adapt it. Thanks for sharing :slight_smile:.
3 Good idea about randomizing the remaining colours.

Actually, for #2, I meant to pose a broader question. Maybe @David_Tschumperle could respond to this. I have been thinking whether it would be possible to make the plugin filters more adaptable. E.g., in @samj’s -samj_Palette_De_Degrades, would it be possible for the user to choose the number of colours, and then *poof* the UI only has x number of colour selection menus. I see many filters that would benefit from this modular format.

Then, this wouldn’t be a CLUT anymore. It would probably mean that two pixels with exactly the same color in the source image may be assigned with two different colors of the reference image.

No reason for that.

Having such dynamic GUI would mean having a more complex language for describing GUI (which is quite basic in its current state), with probably conditions, loops, and everything that makes a real language, not just a simple description with a static list of items.
Meaning also, a lot of new things to learn and to implement.

You are right. The insomnia or sun is getting to me. :sunny: Still, it would be a good idea to explore the ability to transfer colours in a way where a colourful reference could make a dull target just, or at least half, as colourful. There are probably some papers on that subject.

I joked about the machine learning but it is what the cool kids are using nowadays and, if I recall correctly, there were musings on the possibility. :stuck_out_tongue: