# Luminous Landscape Forum

## Raw & Post Processing, Printing => Digital Image Processing => Topic started by: Guillermo Luijk on March 19, 2011, 11:15:31 am

Title: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Guillermo Luijk on March 19, 2011, 11:15:31 am
Hi all, I am doing some tests about local/global contrast algorithms and need a simple equation to define an arbitrary S-shaped contrast curve. It's simple if the turning point is assumed to be (0.5, 0.5), but couldn't find a straight equation if the turning point has to be arbitrarily set.

These are the typical contrast curves I achieved for turning point in the middle (0.5, 0.5):

(http://www.guillermoluijk.com/misc/contrast.gif)

And the simple equations follow this rule:

y = x^2*2 for x<=0.5
y = 1-(1-x)^2*2 for x>0.5

I would like to be able to allocate the turning point to any (x,y) position and still have a smooth S-shaped curve where the slope is continuous on both sides of the turning point. I would like to keep the equation as simple and straightforward as possible, avoiding the use of splines or any other more complex math.

Any ideas? I tried to google but found nothing yet.

Regards

PS: what I want to try is to enhance local contrast at the same time as reducing global contrast (something typical in HDR imaging), using locally defined curves. The goal is to apply a custom contrast curve to every pixel, with its turning point calculated according to the average luminance of the pixel's neighbourhood:

(http://www.guillermoluijk.com/misc/algo.gif)
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning po
Post by: joofa on March 19, 2011, 11:28:55 am
Hi Guillermo,

Would a sigmoid function work?

Sincerely,

Joofa
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Guillermo Luijk on March 19, 2011, 11:31:23 am
Anything with a smooth S-shape and where the turning point can be freely located, but the sigmoid doesn't guarantee (0,0) and (1,1), right?

Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: crames on March 19, 2011, 11:59:08 am
Hi Guillermo,

Have you searched "sigmoid function" (or sigmoidal curve)? You will find a lot of different formulas, but I'm not sure if any are simple enough for your purposes.

edit: Joofa beat me to it!
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Brad Smith on March 19, 2011, 12:30:37 pm
Hi,

Do you always want 0,0 and 1,1 on your curve? How many other points do you want to specify--just one?
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Guillermo Luijk on March 19, 2011, 12:50:13 pm
Do you always want 0,0 and 1,1 on your curve? How many other points do you want to specify--just one?

Ideally it would be a curve with just 3 parameters:
* X location of turning point
* Y location of turning point
* Intensity of the S shape (that can determine both an S shape or inverted S shape)

And must achieve:
* Include (0,0) and (1,1)
* Be smooth (no gaps in the first derivative, even at the turning point)

So it's basically a 3 point curve, with S shape.
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Christoph C. Feldhaim on March 19, 2011, 04:17:06 pm
You might want to have a look at Bézier Spline Curves.
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: ejmartin on March 19, 2011, 04:56:53 pm
Hi all, I am doing some tests about local/global contrast algorithms and need a simple equation to define an arbitrary S-shaped contrast curve. It's simple if the turning point is assumed to be (0.5, 0.5), but couldn't find a straight equation if the turning point has to be arbitrarily set.

These are the typical contrast curves I achieved for turning point in the middle (0.5, 0.5):

(http://www.guillermoluijk.com/misc/contrast.gif)

And the simple equations follow this rule:

y = x^2*2 for x<=0.5
y = 1-(1-x)^2*2 for x>0.5

I would like to be able to allocate the turning point to any (x,y) position and still have a smooth S-shaped curve where the slope is continuous on both sides of the turning point. If would like to keep the equation as simple and straightforward as possible, avoiding the use of splines or any other more complex math.

Your curve is a spline -- a piecewise polynomial curve with conditions of continuity of the curve and its first derivative at the segment boundaries.

Just for fun, take the first curve and scale it:

y = a*(x/a)^2

This is a curve which reaches y=a at x=a, and has slope 2 at that point; and slope zero at x=0.  If we take your second curve and scale it by (1-a):

(1-y) =  (1-a)*((1-x)/(1-a))^2

this is a curve which reaches 1-y=1-a at 1-x=1-a, or in other words y=a at x=a; it has slope 2 at that point; and slope zero at x=1, where also y=1.  So we have a sigmoidal curve with slope zero at x=0 and x=1, and slope 2 at x=a where the curve crosses the straight line x=y.

Now we can add a control parameter: Take the above curve, call it f(x); now form the curve

y = (1-s)*x + s*f(x)

This is a curve that has slope (1-s) at x=0 and x=1; and slope (1-s)+2*s=1+s at x=a.  Thus by dialing s between 0 and 1, the slope at the neutral point x=a (where y=a of course) is dialed between 1 and 2; the contrast is gradually increased, and the curve is monotonic.  For contrast decrease, you can either allow s to be negative, or if you want the effect to be symmetric about s=0, you want to interchange the roles of y and x in this regime.
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Guillermo Luijk on March 19, 2011, 08:44:26 pm
Perfect solution Emil, simple and elegant. Just needs an extra powering to shift the turning point from (a,a) to (a,b):

y = [ (1-s)*x + s*a*(x/a)^E ] ^ ( log(b)/log(a) )

Parameters:
• (a,b): turning point
• s: slope at ends
• E: contrast strength

(http://www.guillermoluijk.com/misc/contrastab.gif)

Thanks a lot and thanks to all who helped.

Regards
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: kirkt on March 19, 2011, 08:52:17 pm
Love it. This should be fun.

Kirk
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: George Machen on March 20, 2011, 12:42:12 pm
turning point — "point of inflection"
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Guillermo Luijk on March 20, 2011, 02:42:47 pm
turning point — "point of inflection"

It's fun because in Spanish it's also called 'punto de inflexión', but I googled it and found that the most appropiate translation could be 'turning point'.

I got the very first result with quite strong parameters. I have to play more with their values to optimise halos, but the result is according to what I expected:

(http://www.guillermoluijk.com/misc/test.gif)

Regards

Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: ejmartin on March 21, 2011, 09:04:42 am
I'm a little confused about the method.  If one does USM with high radius, low amount, one is effectively taking the difference with the neighborhood average and amplifying it, then adding that back to the neighborhood average.  Your approach doesn't look like it differs much from HR-LA USM except that the amount of amplification (aka contrast) itself depends on the neighborhood average, and a tone curve is being applied to the neighborhood average for tonal compression (S/H tool).  What is the advantage of having an entire curve, if one is only adjusting the value of the pixel relative to its neighborhood average?  Perhaps I am just misinterpreting your figure:

(http://www.guillermoluijk.com/misc/algo.gif)
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: PierreVandevenne on March 21, 2011, 10:12:22 am
If the discussion runs its course, we'll be hitting "a trous" wavelet functions before the end of the year...  ;)
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Guillermo Luijk on March 25, 2011, 05:04:34 pm
What is the advantage of having an entire curve, if one is only adjusting the value of the pixel relative to its neighborhood average?  Perhaps I am just misinterpreting your figure:

A different curve is applied to every pixel according to the neighbourhood average, but that curve is chosen from a limited set of pre-calculated curves. The curve chosen is that which has the inflection point in the pixel's neighbourhood average luminance.

This anim shows 16 of the 256 curves calculated for increasing of parameter E (contrast slope at inflection point):

(http://www.guillermoluijk.com/misc/gonzo_anim.gif)

The result is similar to PS's USM + S/H as you say.

Now with the coloured image, strong parameters provide that HDR'ish look:

(http://img59.imageshack.us/img59/6137/gonzocolor.jpg)

Regards
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Carter Lee on January 01, 2015, 11:36:18 pm
Hi. Sir.
I've just tried implement with your equation. but it doesn't work.

x   a   b   s   E      Y
0   0.3   0.4   1   1      0
0.1   0.3   0.4   1   1      0.173358043
0.2   0.3   0.4   1   1      0.29379519
0.3   0.3   0.4   1   1      0.4
0.4   0.3   0.4   1   1      0.497903715
0.5   0.3   0.4   1   1      0.590064265
0.6   0.3   0.4   1   1      0.677892263
0.7   0.3   0.4   1   1      0.762273695
0.8   0.3   0.4   1   1      0.843812691
0.9   0.3   0.4   1   1      0.922945352
1   0.3   0.4   1   1      1

Title: Re:
Post by: Guillermo Luijk on January 02, 2015, 01:01:49 pm
I confirm the equation. I used it to generate the sample plots.
Title: Re: HELP: Equation for a Contrast Curve (with precise location of the turning point)
Post by: Rory on January 04, 2015, 02:29:36 pm
I am interested too.  What have I done wrong?

(http://roryhill.zenfolio.com/img/s12/v174/p706794249-4.jpg)
Title: Re:
Post by: Guillermo Luijk on January 05, 2015, 01:58:21 am
"And the simple equations follow this rule:

y = x^2*2 for x<=0.5
y = 1-(1-x)^2*2 for x>0.5"

I think you are missing it's a function defined in two stages. Please read the entire thread.