G'MIC exercises

I assumed you meant “searching for a such that target_y is the average of the processed image”.
Here is a quick attempt, so far it seems to work:

dichotomic_sigmoid :

  # Define the image processing function to consider.
  m "proc : n. 0,1 f. 1/(1+exp(-$""1*(i))) n. 0,1"

  # Find sigmoid parameter, such that processed image average is 0.5.
  search_dichotomic "+proc $""1 res={ia} rm. u $res",0.5 gamma=${}

  # Generate processed result.
  +proc $gamma

Then,

$ gmic sp tiger dichotomic_sigmoid mul. 255 to[0] Original to[1] Processed a x o res_sigmoid.png

EDIT: Oups, it doesn’t seem to work when I add -0.5 as a shift for the sigmoid function, I’ll investigate that :slight_smile:

EDIT2: It’s not that surprising in fact. The dichotomic search works only if the considered function is strictly increasing (according to the searched parameter), which is not the fact if we add a negative offset like -0.5 in it, as the plot below shows:

$ gmic plot "'1/(1+exp(-x*(-0.4)))'",1024,1,1,-10,10

gmic_negplot

What happen if that if your original image, normalized to [0,1] has most of its value below 0.5, then the average of all the applied sigmoid (with a shift of -0.5) will be a decreasing function of the searched parameter, thus making the dichotomic search fail.