If I have a series of pixels, which range from say -500 to +1000, how would I normalize all the pixels on the same gradient so that they fall between a specific range, say 0 and 255?
Some pseudocode like this would scale values linearly from one range to another
oldmin=-500
oldmax=1000
oldrange=oldmax-oldmin;
newmin=0
newmax=255;
newrange=newmax-newmin;
foreach(oldvalue)
{
//where in the old scale is this value (0...1)
scale=(oldvalue-oldmin)/oldrange;
//place this scale in the new range
newvalue=(newrange*scale)+newmin
}
Your question isn't very clear so I'm going to assume that you're doing some kind of image processing and the results you get are values from -500 to 1000 and now you need to save the color to a file where every value needs to be between 0 and 255.
How you do this is really very dependent in the application, what is really the meaning of the results and what exactly you want to do. The two main options are:
Linear normalization - linearly may your minimal value to 0 and your maximal value to 255. Of course you'll first need to find the minimum and maximum. You do:
v = (origv - min)/(max - min) * 255.0
What this does is first map the values to [0,1]
and then stretch them back to [0,255]
.
A third option is to mix and match between these two options. Your application might demand that you treat negative values as unneeded values and clamp them to 0 and positive values to linearly map to [0,255]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With