Skip to main content
GameDev.net gamedev.net
🔒 Locked

Scale values from one range to another, but seems tricky

Started by mahendragr Sep 7, 2011 at 6:11 PM 2 replies 850+ views
Original Post
mahendragr
mahendragr
I have an array, whose size is 256. This array has elements which are in 0 to 1 range. Now, i need an array whose size should be 65536, where each element should be interpolated or scaled from the original array of 256 values. Some kind of mapping where, lets say, 10 values from the original image should match to more number values in the bigger array. Is it doable?
Telastyn
Telastyn

I have an array, whose size is 256. This array has elements which are in 0 to 1 range. Now, i need an array whose size should be 65536, where each element should be interpolated or scaled from the original array of 256 values. Some kind of mapping where, lets say, 10 values from the original image should match to more number values in the bigger array. Is it doable?


Absolutely. Otherwise image scaling, and a pile of other things would be impossible.
Kian
Kian
You can see that 256*256 = 65536. So to scale your values, first multiply the index by 256, to get the new locations for the actual values you are certain about.

So your value at position 3 in the first array ends at position 768 in the second array. This leaves rather large gaps between the values, however. How to fill them depends on how much computational power you want to spend, and what appearance you want the output to have.

I'll give an example with smaller sets below:

first array (size 4) -> resulting array (size 8)
We multiply the indices by 2

array[0]= 0.5 -> 0*2 -> newarray[0]= 0.5

array[1]= 0.3 -> 1*2 -> newarray[2]= 0.3

array[2]= 0.7 -> 2*2 -> newarray[4]= 0.7

array[3]= 0.1 -> 3*2 -> newarray[6]= 0.1

We know the positions of the original values, but we have gaps for the values newarray[1,3,5,7]. The quickest method would be to use the value of the previous element in the array. So newarray[1]=0.5, newarray[3]=0.3 and in general newarray[x]=newarray[x-1], when x is odd.

You can also do linear interpolation, where the value at each point is the midpoint of the values at each side. So for x=odd, newarray[x]=(newarray[x-1]+newarray[x+1])/2.

Depending on how smooth you want the output to look, you can go with quadratic or higher order interpolations. You should look up interpolation of you're not sure how to do it.
mahendragr
mahendragr
Awesome, thanks for the reply, i solved it!

You can see that 256*256 = 65536. So to scale your values, first multiply the index by 256, to get the new locations for the actual values you are certain about.

So your value at position 3 in the first array ends at position 768 in the second array. This leaves rather large gaps between the values, however. How to fill them depends on how much computational power you want to spend, and what appearance you want the output to have.

I'll give an example with smaller sets below:

first array (size 4) -> resulting array (size 8)
We multiply the indices by 2

array[0]= 0.5 -> 0*2 -> newarray[0]= 0.5

array[1]= 0.3 -> 1*2 -> newarray[2]= 0.3

array[2]= 0.7 -> 2*2 -> newarray[4]= 0.7

array[3]= 0.1 -> 3*2 -> newarray[6]= 0.1

We know the positions of the original values, but we have gaps for the values newarray[1,3,5,7]. The quickest method would be to use the value of the previous element in the array. So newarray[1]=0.5, newarray[3]=0.3 and in general newarray[x]=newarray[x-1], when x is odd.

You can also do linear interpolation, where the value at each point is the midpoint of the values at each side. So for x=odd, newarray[x]=(newarray[x-1]+newarray[x+1])/2.

Depending on how smooth you want the output to look, you can go with quadratic or higher order interpolations. You should look up interpolation of you're not sure how to do it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.