How do I normalize an image?

前端 未结 5 1856
轻奢々
轻奢々 2020-12-31 14:18

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

相关标签:
5条回答
  • 2020-12-31 14:38

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

    0 讨论(0)
  • 2020-12-31 14:44

    First make it all positive. If the minimum is -500 then add 500 to all values. Then the minimum would be 0, and the maximum would be 1500.

    Then it is just a rule of three and you have it:

    [value in 0,255] = 255*(Pixel/1500)
    
    0 讨论(0)
  • 2020-12-31 14:45

    Create two variables, MinInputValue and MaxInputValue. Initialize MinInputValue to a very large positive number (higher than the largest pixel value you ever expect to see) and MaxInputValue to a very large negative number (lower than the lowest pixel value you ever expect to see).

    Loop over every pixel in the image. For each pixel, if the pixel value PixelValue is lower than MinInputValue, set MinInputValue to PixelValue. If the pixel value is higher than MaxInputValue, set MaxInputValue to PixelValue.

    Create a new variable, InputValueRange, and set it to MaxInputValue - MinInputValue.

    Once this is done, loop over every pixel in the image again. For each pixel PixelValue, calculate the output pixel value as 255.0 * (PixelValue - MinInputValue) / InputValueRange. You can assign this new value back to the original PixelValue, or you can set the corresponding pixel in an output image of the same size.

    0 讨论(0)
  • 2020-12-31 14:48

    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
    }
    
    0 讨论(0)
  • 2020-12-31 14:52

    Some pseudo code may help:

    foreach( pixel_value in pixel_values): # between -500 and 1000
    
        position = (pixel_value + 500) / 1500 # gives you a 0 to 1 decimal
        new_value = int(postion * 255) # or instead of casting, you could round it off
    

    That's python code by the way.

    0 讨论(0)
提交回复
热议问题