Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I normalize an image?

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?

like image 242
Nick Bolton Avatar asked Mar 29 '09 18:03

Nick Bolton


2 Answers

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
}
like image 169
Paul Dixon Avatar answered Oct 27 '22 11:10

Paul Dixon


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:

  • clamp the values - anything under 0 you replace by 0 and anything above 255 you replace by 255. You'll want to do this, for instance, if your image processing is some kind of interpolation which really shouldn't reach these values
  • 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].

like image 43
shoosh Avatar answered Oct 27 '22 10:10

shoosh