Cubic function and values between $0$ and $255$

108 Views Asked by At

For a computer vision program, I have values between $0$ and $255$ that need to follow a cubic function ($y=x^3$) behavior so that :

  • $f(255) = 255$
  • $f(\frac{255}{2}) = 128$
  • $f(0) = 0$

But I don't know how to find its equation. Thank you for your help.

3

There are 3 best solutions below

2
On BEST ANSWER

A natural parameterIn addition to your own answer: a natural 4th condition is to fix the slope in the middle (that's the only thing you are allowing to change). This makes sense, because it defines the contrast of the nonlinear mapping. Using it this way, you can get through analytically.

Let's stretch everything to the unit square (divide $x$ and $y$ by $255$).

Start with a centered cubic curve (only odd terms allowed to keep the symmetry), it has one important parameter $a$ that sets the slope through the center: $$y(x)=x^3+ax$$

Make it go through $(1,1)$:

$$y(x)=\frac{x^3+ax}{1+a}$$

Now it goes from (-1,-1) to (1,1) and is symmetric around the center. Squeeze it into the box from (0,0) to (1,1) by scaling by factor of 1/2 and shifting by 1/2 in both axes:

$$y(x)=\frac12+\frac12\left(\frac{(2x-1)^3+a(2x-1)}{1+a}\right)$$

$2x-1$ is there so that it maps 0 to -1 and 1 to 1.

This is already a final solution (replace $x$ by $x/255$ and multiply result by $255$ to go back to byte values). This shows that most ugly terms come from expanding the powers, but actually, the function is much more simple and this is also how you should compute it if you are using floating point numerics (if using integers/bytes, then you must expand).

For different values of $a$ it has different slopes, but all go through desired points. A special case $a=0$ which has horizontal tangent is also interesting. Negative values of $a$ don't produce unique mappings (not one-to-one), so probably should be discarded, depending on what you want.

If you make $a$ very big, you converge towards linear mapping $y=x$.

An image showing a few samples:

enter image description here

0
On

Thanks to @lulu, I've found a good solution to my problem. Using Wolfram Alpha

First, here is what I was expecting as the result function : Original schema

But to determine a cubic function equation, you need a 4th parameter. I did the hypothesis that $f(64) ~= 115$.

So I've searched for InterpolatingPolynomial[(0,0),(255,255),(255/2,128),(64,115)] in Wolfram Alpha which outputed the following schema : Schema for hypothesis f(64) = 115

It was a pretty good result. But I wanted that $f(127)>=f(128)>=f(129)$.

So, advised by @lulu, I've tried InterpolatingPolynomial[(0,0),(255,255),(255/2,128),(64,110)].

And it outputed a satisfying result !

Schema for hypothesis f(64) = 110

Here is by consequence the final savage equation : $\frac{577744073*x}{197937120} - \frac{1136315963*x^2}{50473965600}+\frac{1483351*x^3}{25236982800}$.

Or for those who want it to computer programs : (577744073*x)/197937120 - (1136315963*(x^2))/50473965600 + (1483351 *(x^3))/25236982800.

0
On

Since $f(0)=0$, let us consider the cubic equation $$f(x)=a x+b x^2+c x^3$$ So $$f(255)=255 a+ (255)^2 b+(255)^3c$$ $$f\left(\frac{255}{2}\right)=a\left(\frac{255}{2}\right)+\left(\frac{255}{2}\right)^2b+\left(\frac{255}{2}\right)^3c$$ Solve for $a$ and $b$ to get $$a=\frac{65025 c}{2}+\frac{257}{255}\qquad \text{and}\qquad b=-\frac{765 c}{2}-\frac{2}{65025}$$

T Using that $$f(127)=8128 c+\frac{8290687}{65025}$$ $$f(128)=\frac{8355712}{65025}-8128 c$$ $$f(129)=\frac{935637}{7225}-24381 c$$

So, the inequalities would give a quite narrow range for $c$.