Help with gmic map command

Hi All,

Begining GMIC user here and could use some help ith the gmic map command.

Scenario:
I dynamically create an 8-bit 2-D array, with at most 8 levels (classes), one which is the background,
Values in array are are 0,16,48…240.

NOTE: I can have less than 8 classes

I then create a png:
gmic ENV_array.txt -output ENV_gmic.png
#Wish I had known before gmic can take an array directly:-(

Question:
I want to map the 8 levels from (0,16,48…255) → (green, lime, yellow, blah, orange, red). 8 discreet colors.
Basically a map ranging from low risk to high.

Or if there are less than 8, still map each to the appropriate color.

Don’t know what I should do when there’s empty classes, yet, maybe map to very distinct color (e.g cyan or something, but how would I do this?

Where I’m at:

gmic ENV_gmic.png +luminance map[-2] 3 -output EL434_env_map2.png
Interesting, but not really correct. I see that map[-2] uses one of the internal color tables
I would like to define my own clut and pass it to map.

Also trying to understand this from the docs, but I obviously need more reading…

gmic ENV_gmic.png +rgb2ycbcr split[-1] c (0,255,0) resize[-1] 256,1,1,1,3 map[-4] [-1] remove[-1] append[-3–1] c ycbcr2rgb[-1]

regards,
Nick

Images are free to a good home :slight_smile:

EL434_gmic EL434_env_map1_000001

BTW, I asked a related question in the summer,

To have a reference of colors I believe that a gradient in Lab mode is a good solution.
Example Red to Green :

NbColorsApprox=256
$NbColorsApprox,64,1,4
fx_linear_gradient[-1] 255,0,0,255,0,255,0,255,0,0,0,100,2

There are two things you have to do here:

  1. First, remap the values of your initial images so that they take values of the class number, rather than some random integer. This can be done like this:
$ gmic test.png +colormap 0 index.. . rm.

This creates an image with values from 0 to N-1, where N is your number of different classes. This N is actually the width of the estimated colormap [1].

  1. Create your desired custom colormap, and use map to map it to the image you’ve just got with the previous step.
    To create a colormap similar to what you are looking for, I can use command palette hsv, keep only the first part (red \rightarrow green), mirror it along the x-axis and finally resize it to 8 elements (or N). I also set the first pixel to be black, to keep the background color unchanged.

To create that colormap,

$ gmic palette hsv columns 0,59 mirror x r2dx 8 point 0

Then putting all this together gives:

$ gmic test.png +colormap 0 index.. . N={w} rm. palette hsv columns. 0,59 mirror. x r2dx. \$N point. 0 map.. . rm.

which gives:
test2

Note that this will work for any number of classes in your initial input image.

Side note:
If you want to do that on a sequence of similar images where a value must always be mapped to the same color, then it can be convenient to first read all your images, stack them along the z-axis, do the mapping as I did before, then split the result along z again.

Something like:

$ gmic test*.png a z +colormap 0 index.. . N={w} rm. palette hsv columns. 0,59 mirror. x r2dx. \$N point. 0 map.. . rm. s z

An example with ‘replace_color’
names of colors Web colors - Wikipedia

test_loop

to_graya
to_rgba
index=0
repeat 16
replace_color 1,0,$index,$index,$index,255,0,0,0,255 # black
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,0,128,0,255 # green
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,0,255,0,255 # lime
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,173,245,47,255 # GreenYellow
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,255,255,0,255 # yellow
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,255,218,0,255 # PeachPuff
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,255,165,0,255 # orange
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,255,0,0,255 # red
index={$index+1}
done
repeat 16
replace_color 1,0,$index,$index,$index,255,0,255,255,255 # white
index={$index+1}
done

Thank you both David and Sami, you guys are great!

A couple of of observation:

  1. These are the histograms:
    Original histo David’s histo Sami’s histo
    value count value count value count
    0 126950 0 126950 0 126950
    1 2350 1 2350 1 2350
    2 4500 2 4500 2 4500
    3 16700 3 25 3 16700
    4 19150 4 6025 4 25
    5 10850 5 10850 5 6025
    6 6025 6 19150 6 10850
    7 25 7 16700 7 19150

There is some class swapping in both color images that I do not understand. Is this because the colors(map) defined is not linear B–>W where the original is?

  1. I tried scripting (my first) Sami example, and I get an rgb image it is still gray. I commented out the to_graya line and still get the same thing.
    Also added some questions where the script was foggy to me

attached is the script and the resulting image I getEL434_gmic

Thanx again for the jump start,

Nick
EDIT: Adding script cause it did not attach

###From Sami on pixlus: my first attempt at scripting…
####called as
#gmic colorize.gmic color_my_map input
color_my_map :
echo “Colorizing the map”
#Force selected images to be in GRAYA mode
to_graya
#Force selected images to be in RGBA mode
to_rgba
#Index start
index=0
#Repeat the following command 16 times#WHY 16?
repeat 16
replace_color 1,0,$index,$index,$index,255,0,0,0,255 # black
index={$index+1}
done
#Repeat the following command 32 times#WHY 32 now?
repeat 32
replace_color 1,0,$index,$index,$index,255,0,128,0,255 # green
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,0,255,0,255 # lime
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,173,245,47,255 # GreenYellow
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,255,255,0,255 # yellow
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,255,218,0,255 # PeachPuff
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,255,165,0,255 # orange
index={$index+1}
done
repeat 32
replace_color 1,0,$index,$index,$index,255,255,0,0,255 # red
index={$index+1}
done
repeat 16
replace_color 1,0,$index,$index,$index,255,0,255,255,255 # white
index={$index+1}
done

It is an example and you are free to use it or not.
If you want to use a map or an image you have to define it and use the map functions.
In the example I gave the approach is different where we replace an interval of colors by a color.
It is not very subtle but radical.

You can also create your command in a text file and attach it to G’MIC command line ( Step 1 https://gmic.eu/tutorials/bouncing_balls3d/ ).

I attach an example with its comments which you can use directly in Gimp or Krita :

to_gray # to gray without alpha channel
to_rgba # to rgba with alpha  channel
index=0 # array
repeat 16 # array 0,15
replace_color 1,0,$index,$index,$index,255,0,0,0,255 # black
index={$index+1}
done
repeat 32 # array 16,47
replace_color 1,0,$index,$index,$index,255,0,128,0,255 # green
index={$index+1}
done
repeat 32  # array 48,79
replace_color 1,0,$index,$index,$index,255,0,255,0,255 # lime
index={$index+1}
done
repeat 32  # array 80,111
replace_color 1,0,$index,$index,$index,255,173,245,47,255 # GreenYellow
index={$index+1}
done
repeat 32  # array 112,143
replace_color 1,0,$index,$index,$index,255,255,255,0,255 # yellow
index={$index+1}
done
repeat 32  # array 144,175
replace_color 1,0,$index,$index,$index,255,255,218,0,255 # PeachPuff
index={$index+1}
done
repeat 32  # array 176,207
replace_color 1,0,$index,$index,$index,255,255,165,0,255 # orange
index={$index+1}
done
repeat 32  # array 208,239
replace_color 1,0,$index,$index,$index,255,255,0,0,255 # red
index={$index+1}
done
repeat 16  # array 240,255
replace_color 1,0,$index,$index,$index,255,0,255,255,255 # white
index={$index+1}
done

Sorry Sami,
I did create a script (from reading the bouncing ball tutorial), and attached it above.
I thought what you posted was what you used to create the colorized map you attached.
thanx,
Nick

The image was created with this script in G’MIC-Gimp.


Gmic command line
I attach an archive where you have the image to colorize and the script (test_20201016.zip).
You must unzip in the directory where the gmic executable is located.
In a terminal run this command line :
gmic m Test_samj9colors.gmic i Nick.png samj9colors o Nick9colors.png

test_20201016.zip (3.7 KB)

@olNick

rep_olnick_map:
repeat $! l[$>]
 if s==2||s==4 s. c,{s-1} rm. fi
 if s>1
  ss={s}
  compose_channels + /. $ss
 fi
 width={w}
 height={h}
 $width,$height,1,4,"begin(
 color_1=[0,0,0];
 color_2=[0,128,0];
 color_3=[0,255,0];
 color_4=[173,245,47];
 color_5=[255,255,0];
 color_6=[255,218,0];
 color_7=[255,165,0];
 color_8=[255,0,0];
 color_9=[0,255,255];
 );
 color=(
 i0#0<16?(color_1):
 i0#0<48?(color_2):
 i0#0<96?(color_3):
 i0#0<140?(color_4):
 i0#0<180?(color_5):
 i0#0<200?(color_6):
 i0#0<220?(color_7):
 i0#0<240?(color_8):
 color_9
 );
 [color,255]
 "
 rm..
endl done

This script allows you to set where to apply color. I thought this would be a good solution.

Hello all,
I want to thank Sami for the above.
I’ve been using it to create this output. Posterized also.
this is my command, executed from my shell:

set cc “$f_CC.png” #classed and colorized
set cp “$f_CP.png” #classed and posterized
gmic m sami_color.gmic i $f sami_colors o $cc -fx_posterize 150,30,1,8,0,0,1,0 o $cp

Yielding:

The only caveat I have is on my old pc it is quite slow, but runs fast enough on modern hardware.
Tx again Sam,

Nick

Hello Reptorian,

Thanx for this script; I am able to colorize the original image from above (it’s discreet class values range from 0-7), but I can’t seem to get it to work w/ my images that have 8 discreet values of 0,16,48 etc.
I fiddled with it…

Getting this error:
gmic m reptorian_ansi.gmic i EL434.png rep_olnick_map o EL434_rep.png

[gmic]-0./ Start G’MIC interpreter.
[gmic]-0./ Import commands from file ‘reptorian_ansi.gmic’ (1 new, total: 4175).
[gmic]-0./ Input file ‘EL434.png’ at position 0 (1 image 410x455x1x2).
[gmic] *** Error in ./rep_olnick_map/*repeat#2/*local#2/ (file ‘reptorian_ansi.g
mic’, line #10) *** Command ‘input’: File ‘0’, format does not take any input op
tions (options ‘0,1,4,begin(’ specified).

Also realized that gmic2.9.1 does not like utf-8 scripts, ansi only…

anyway, regards to all

Nick
PS Heres the one of the grayscale files used above
EL434

Here is a new archive with the 2 corrected scripts (white color).

4 Examples of command lines:

gmic m olNick.gmic i Indexed.png to_rgb rep_olnick_map_B o rep_olnick_map_B.png

gmic m olNick.gmic i Indexed_alpha.png to_rgb rep_olnick_map_B to_rgba replace_color 0,0,0,0,0,255,0,0,0,0 o rep_olnick_map_B_alpha.png

gmic m olNick.gmic i Indexed.png samj9colorsV3 o samj9colorsV3.png

gmic m olNick.gmic i Indexed_2.alpha samj9colorsV3 to_rgba replace_color 0,0,0,0,0,255,0,0,0,0 o samj9colorsV3_alpha.png

test_20201022.zip (5.0 KB)

Thanx again Sami,

I’m on a usb boot Linux OS today, as this Win machine keeps crashing. I need to install gmic here first and test the above.
Funny how this machine runs fine w/ Slack but crashes on Win 7.

regards,
Nick