如何规范化图像?

时间:2022-03-30 15:10:10

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?

如果我有一系列像素,范围从-500到+1000,我如何规范化相同渐变上的所有像素,使它们落在特定范围(比如0到255)之间?

5 个解决方案

#1


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
}

#2


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.

你的问题不是很清楚,所以我假设你正在进行某种图像处理,你得到的结果是从-500到1000的值,现在你需要将颜色保存到每个值的文件中需要在0到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
  • 钳制值 - 0以下的任何值都替换为0,255以上的任何值都替换为255.例如,如果图像处理是某种插值,实际上不应达到这些值,则需要这样做

  • 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:

    线性归一化 - 线性地将您的最小值设置为0,将最大值线性化为255.当然,您首先需要找到最小值和最大值。你做:

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

这样做首先将值映射到[0,1],然后将它们拉回到[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,255]。

#3


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.

首先让它变得积极。如果最小值为-500,则将500添加到所有值。然后最小值为0,最大值为1500。

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

然后它只是三个规则,你有它:

[value in 0,255] = 255*(Pixel/1500)

#4


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.

顺便说一下,这是python代码。

#5


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

创建两个变量MinInputValue和MaxInputValue。将MinInputValue初始化为一个非常大的正数(高于您预期看到的最大像素值),将MaxInputValue初始化为一个非常大的负数(低于您期望看到的最低像素值)。

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.

循环遍历图像中的每个像素。对于每个像素,如果像素值PixelValue低于MinInputValue,则将MinInputValue设置为PixelValue。如果像素值高于MaxInputValue,请将MaxInputValue设置为PixelValue。

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

创建一个新变量InputValueRange,并将其设置为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.

完成此操作后,再次循环覆盖图像中的每个像素。对于每个像素PixelValue,计算输出像素值为255.0 *(PixelValue - MinInputValue)/ InputValueRange。您可以将此新值指定回原始PixelValue,也可以在相同大小的输出图像中设置相应的像素。

#1


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
}

#2


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.

你的问题不是很清楚,所以我假设你正在进行某种图像处理,你得到的结果是从-500到1000的值,现在你需要将颜色保存到每个值的文件中需要在0到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
  • 钳制值 - 0以下的任何值都替换为0,255以上的任何值都替换为255.例如,如果图像处理是某种插值,实际上不应达到这些值,则需要这样做

  • 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:

    线性归一化 - 线性地将您的最小值设置为0,将最大值线性化为255.当然,您首先需要找到最小值和最大值。你做:

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

这样做首先将值映射到[0,1],然后将它们拉回到[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,255]。

#3


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.

首先让它变得积极。如果最小值为-500,则将500添加到所有值。然后最小值为0,最大值为1500。

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

然后它只是三个规则,你有它:

[value in 0,255] = 255*(Pixel/1500)

#4


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.

顺便说一下,这是python代码。

#5


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

创建两个变量MinInputValue和MaxInputValue。将MinInputValue初始化为一个非常大的正数(高于您预期看到的最大像素值),将MaxInputValue初始化为一个非常大的负数(低于您期望看到的最低像素值)。

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.

循环遍历图像中的每个像素。对于每个像素,如果像素值PixelValue低于MinInputValue,则将MinInputValue设置为PixelValue。如果像素值高于MaxInputValue,请将MaxInputValue设置为PixelValue。

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

创建一个新变量InputValueRange,并将其设置为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.

完成此操作后,再次循环覆盖图像中的每个像素。对于每个像素PixelValue,计算输出像素值为255.0 *(PixelValue - MinInputValue)/ InputValueRange。您可以将此新值指定回原始PixelValue,也可以在相同大小的输出图像中设置相应的像素。