I am working on a project where i have to first normalize the image to [0,1] and then perform dwt and idwt on the image after the processing. so first i convert the image to an array then i normalize it with this code
我正在开发一个项目,我必须先将图像标准化为[0,1],然后在处理后对图像执行dwt和idwt。所以首先我将图像转换为数组,然后使用此代码对其进行标准化
def normalization (array):
maxs = max([max(l) for l in array])
mins = min([min(l) for l in array])
range = max - mins
A = []
for x in array:
m = [(float(xi) - mins)/range for xi in x]
A.append(m)
return A
the code works well and now i have no idea how can i denormalize it back to the actual range. can anybody help?
代码运行良好,现在我不知道如何将其反规范化回实际范围。任何人都可以帮忙吗?
2 个解决方案
#1
I use the following to map to and from any interval [a, b] --> [c, d] and back:
我使用以下内容来映射到任何区间[a,b] - > [c,d]和后面:
import numpy as np
def interval_mapping(image, from_min, from_max, to_min, to_max):
# map values from [from_min, from_max] to [to_min, to_max]
# image: input array
from_range = from_max - from_min
to_range = to_max - to_min
scaled = np.array((image - from_min) / float(from_range), dtype=float)
return to_min + (scaled * to_range)
An example:
image = np.random.randint(0, 255, (3, 3))
image
returns:
array([[186, 158, 187],
[172, 176, 232],
[124, 167, 155]])
Now map this from [0, 255] to [0, 1]
现在将其从[0,255]映射到[0,1]
norm_image = interval_mapping(image, 0, 255, 0.0, 1.0)
norm_image
returns:
array([[ 0.72941176, 0.61960784, 0.73333333],
[ 0.6745098 , 0.69019608, 0.90980392],
[ 0.48627451, 0.65490196, 0.60784314]])
now from [0, 1] back to [0, 255]:
现在从[0,1]回到[0,255]:
orig_image =interval_mapping(norm_image, 0.0, 1.0, 0, 255).astype('uint8')
orig_image
returns:
array([[186, 158, 187],
[172, 176, 232],
[124, 167, 155]], dtype=uint8)
You could also use it one a single column of image
and map it to [-1.0, 1.0]:
您也可以在一列图像中使用它并将其映射到[-1.0,1.0]:
col = image[:, 1]
print col
interval_mapping(col, 0, 255, -1.0, 1.0)
returns:
[158 176 167]
array([ 0.23921569, 0.38039216, 0.30980392])
or a scalar:
或标量:
interval_mapping(1.0, 0, 255, -1.0, 1.0)
returns:
-0.99215686274509807
#2
You just need to do the inverse of the normalisation. So, multiply by the original range and add the minimum. Just typing untested code:
您只需要执行规范化的反转。因此,乘以原始范围并添加最小值。只需输入未经测试的代码:
def denormalization (array, mins, range):
A = []
for x in array:
m = [(float(xi) * range) + mins for xi in x]
A.append(m)
return A
Obviously you'd need to keep your original range and minimum as globals in order to use them in this function.
显然,你需要保持原始范围和最小值作为全局变量才能在此函数中使用它们。
#1
I use the following to map to and from any interval [a, b] --> [c, d] and back:
我使用以下内容来映射到任何区间[a,b] - > [c,d]和后面:
import numpy as np
def interval_mapping(image, from_min, from_max, to_min, to_max):
# map values from [from_min, from_max] to [to_min, to_max]
# image: input array
from_range = from_max - from_min
to_range = to_max - to_min
scaled = np.array((image - from_min) / float(from_range), dtype=float)
return to_min + (scaled * to_range)
An example:
image = np.random.randint(0, 255, (3, 3))
image
returns:
array([[186, 158, 187],
[172, 176, 232],
[124, 167, 155]])
Now map this from [0, 255] to [0, 1]
现在将其从[0,255]映射到[0,1]
norm_image = interval_mapping(image, 0, 255, 0.0, 1.0)
norm_image
returns:
array([[ 0.72941176, 0.61960784, 0.73333333],
[ 0.6745098 , 0.69019608, 0.90980392],
[ 0.48627451, 0.65490196, 0.60784314]])
now from [0, 1] back to [0, 255]:
现在从[0,1]回到[0,255]:
orig_image =interval_mapping(norm_image, 0.0, 1.0, 0, 255).astype('uint8')
orig_image
returns:
array([[186, 158, 187],
[172, 176, 232],
[124, 167, 155]], dtype=uint8)
You could also use it one a single column of image
and map it to [-1.0, 1.0]:
您也可以在一列图像中使用它并将其映射到[-1.0,1.0]:
col = image[:, 1]
print col
interval_mapping(col, 0, 255, -1.0, 1.0)
returns:
[158 176 167]
array([ 0.23921569, 0.38039216, 0.30980392])
or a scalar:
或标量:
interval_mapping(1.0, 0, 255, -1.0, 1.0)
returns:
-0.99215686274509807
#2
You just need to do the inverse of the normalisation. So, multiply by the original range and add the minimum. Just typing untested code:
您只需要执行规范化的反转。因此,乘以原始范围并添加最小值。只需输入未经测试的代码:
def denormalization (array, mins, range):
A = []
for x in array:
m = [(float(xi) * range) + mins for xi in x]
A.append(m)
return A
Obviously you'd need to keep your original range and minimum as globals in order to use them in this function.
显然,你需要保持原始范围和最小值作为全局变量才能在此函数中使用它们。