Thread: re-allocating a range of values for a program

1. re-allocating a range of values for a program

Please forgive me if this is the wrong forum. I am not quite sure under which heading this particular problem falls. Mods, please move it if it is not in the correct place. Thanks.

OK, I wonder if some bright mathematician can help me with a PHP programming problem please?

Let me explain. Greys in a black and white image are in the range 0-255. These are the only possible values. Each greyscale pixel has a value in this range.

I have an image where the range is quite narrow (57-205) and I want to re-distribute it throughout the scale(0-255) in as even way as possible. I am writing a program to re-allocate these values and I would like help in working out a formula or formulas to achieve this.

Can anyone work out a formula or a couple of formulas to allow me to re-distribute these values over the full range please. It does not matter if there are spaces without values, but I would like a nice relatively even spread if possible.

Thank you so much for any super-duper mathematical solutions. As you can tell, I am not great at maths. :-)

Paul

2. OK, I don't know if this is going to make much sense but here is my attempt!

Let the small range be from "a" to "b". Let the big range be from "A" to "B". Then we want the ratios between the color values to remain the same. Here is what I tried. Let "x" be a point between "a" and "b", it will be one of the color values in the initial range. Then we map it to a point "y" in the big range in such a way that the distance from "x" to "a" relative to the whole range ("a" to "b") is the same as the distance from "A" to "y" relative the whole big range.

Here is the mathematical way of saying it:

$\displaystyle \frac{x-a}{b-a} = \frac{y-A}{B-A}$

Solving for y, you get:

$\displaystyle y = (x-a)\frac{B-A}{b-a}+A$

Similarly, if you want to go the other way, you just use

$\displaystyle x = (y-A)\frac{b-a}{B-A}+a$

In your case you have a = 57, b = 205 and A = 0 and B = 255. Then you get

$\displaystyle y = (x-57)\frac{255}{148}$

3. Vlasev, This makes perfect sense.
Thank you so much for spending the time and effort to sort this out for me.

Thanks again.
Paul

4. Hello, ocpaul20!

I got the same result as Vlasev . . .

You have a range of numbers from 57 to 205.
And you want to "spread" them to range from 0 to 255.

So we have these two sets of numbers:

. . $\begin{array}{c||c|c|c|c|c}
x & 57 & 58 & 59 & \hdots & 205 \\ \hline
y & 0 & - & - & \hdots & 255\end{array}$

So we have a linear function which ranges from $(57,0)$ to $(205,255)$

. . The slope of this line is: . $m \;=\;\frac{255-0}{205-57} \;=\;\frac{255}{148}$

. . The line passes through $(57,0)$

Therefore, the equation of the line is: . $y \;=\;\frac{255}{148}(x - 57)$

5. soroban, thanks to you too.

Program working well and calculating both ways. :-)