0

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.

secavfr
  • 113
  • What you're asking is unclear. Could you clarify? – Allawonder Jul 03 '18 at 11:46
  • Is it so that the function has to be of the form $$ f(x) = a_0 + a_1x + a_2x^2 + a_3 x^3 $$ ? If not, what is the exact form that it has to follow? As you can see, simply $f(x) = x^3$ will not do and it needs some coefficients. – Matti P. Jul 03 '18 at 11:47
  • 2
    You need to have 4 points to uniquely determine a cubic polynomial – ab123 Jul 03 '18 at 11:49
  • The quadratic $\frac {257}{255} x - \frac {2}{65025}x^2$ already works. If you insist on a cubic you should add another point. – lulu Jul 03 '18 at 11:53
  • Thanks for your answers. Here is the function I am aiwaiting : https://imgur.com/uK6jwW6 – secavfr Jul 03 '18 at 11:59
  • If you have a graph you believe in you should be able to add a fourth point to your data. – lulu Jul 03 '18 at 12:00
  • Worth noting: you can do this in Wolfram Alpha. The syntax is "InterpolatingPolynomial[(0,0),(255,255),(255/2,128)]" If you have a fourth point you just add it to the list. – lulu Jul 03 '18 at 12:01
  • I fear that if I add a 4th value, it would be too much estimated, like $f(64) ~= 115$ – secavfr Jul 03 '18 at 12:04
  • Dou you need the tangent to be horizontal in $(128,128)$? – zar Jul 03 '18 at 12:05
  • @lulu, it seems that with your technics, I get a pretty good result with WolframAlpha : InterpolatingPolynomial[(0,0),(255,255),(255/2,128),(64,115)].

    The only problem is that it isn't exact.

    – secavfr Jul 03 '18 at 12:08
  • If you add the point $(64,115)$ WA gives $\frac {3291827}{50473965600}x^3 - \frac {2521352551}{100947931200 }x^2+ \frac {1238395021}{395874240}x$ – lulu Jul 03 '18 at 12:08
  • @zar, I need that $f(127) >= f(128) >= f(129)$. – secavfr Jul 03 '18 at 12:09
  • Yes. It shouldn't matter too much if your point is a little inaccurate but if you are worried about that, test it. try the point $(64,100)$ instead. And so on. Convince yourself that you are close enough (or not). – lulu Jul 03 '18 at 12:09
  • Thank you @lulu, I will do like that. Post your answer as... answer, so I accept it ! – secavfr Jul 03 '18 at 12:10
  • Oh, it's just a WA calculation. If you are so inclined post your own answer, maybe with some testing around the extra point. – lulu Jul 03 '18 at 12:11

3 Answers3

1

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

orion
  • 15,781
  • Great mathematical explanation. I can't upvote now but I will. Thank you for your answer.

    Here is the final equation : $y(x)=(\frac12+\frac12\left(\frac{((2x/255)-1)^3+a(2x-1)}{1+a}\right))*255$

    – secavfr Jul 03 '18 at 12:59
  • Accepted as answer as it is the most mathematically correct answer and computer-friendly (no big figures). – secavfr Jul 03 '18 at 13:03
0

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.

secavfr
  • 113
0

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$.