尝试规范化Python图像获取错误-RGB值必须在0..1范围内
我得到了一个图像(32,32,3)和两个向量(3),它们代表平均值和标准。我试图通过使图像进入一种状态来规范化图像,在这种状态下,我可以减去平均值并除以标准值,但当我试图绘制它时,我得到了以下错误尝试规范化Python图像获取错误-RGB值必须在0..1范围内,python,image-processing,normalization,Python,Image Processing,Normalization,我得到了一个图像(32,32,3)和两个向量(3),它们代表平均值和标准。我试图通过使图像进入一种状态来规范化图像,在这种状态下,我可以减去平均值并除以标准值,但当我试图绘制它时,我得到了以下错误 ValueError: Floating point image RGB values must be in the 0..1 range. 我理解这个错误,所以我认为当我尝试正常化时,我没有执行正确的操作。下面是我试图使用的规范化图像的代码 mean.shape #(3,) std.shape #
ValueError: Floating point image RGB values must be in the 0..1 range.
我理解这个错误,所以我认为当我尝试正常化时,我没有执行正确的操作。下面是我试图使用的规范化图像的代码
mean.shape #(3,)
std.shape #(3,)
sample.shape #(32,32,3)
# trying to unroll and by RGB channels
channel_1 = sample[:, :, 0].ravel()
channel_2 = sample[:, :, 1].ravel()
channel_3 = sample[:, :, 2].ravel()
# Putting the vectors together so I can try to normalize
rbg_matrix = np.column_stack((channel_1,channel_2,channel_3))
# Trying to normalize
rbg_matrix = rbg_matrix - mean
rbg_matrix = rbg_matrix / std
# Trying to put back in "image" form
rgb_image = np.reshape(rbg_matrix,(32,32,3))
您的错误似乎表明图像缺乏规范化
mean.shape #(3,)
std.shape #(3,)
sample.shape #(32,32,3)
# trying to unroll and by RGB channels
channel_1 = sample[:, :, 0].ravel()
channel_2 = sample[:, :, 1].ravel()
channel_3 = sample[:, :, 2].ravel()
# Putting the vectors together so I can try to normalize
rbg_matrix = np.column_stack((channel_1,channel_2,channel_3))
# Trying to normalize
rbg_matrix = rbg_matrix - mean
rbg_matrix = rbg_matrix / std
# Trying to put back in "image" form
rgb_image = np.reshape(rbg_matrix,(32,32,3))
在我的深度学习项目中,我使用了这个函数来规范化图像
def normalize(x):
"""
Normalize a list of sample image data in the range of 0 to 1
: x: List of image data. The image shape is (32, 32, 3)
: return: Numpy array of normalized data
"""
return np.array((x - np.min(x)) / (np.max(x) - np.min(x)))
通过将图像的平均值设置为零,将图像的标准偏差设置为1(就像您所做的那样),对图像进行规格化,将生成大多数(但不是全部)像素都在范围[-2,2]内的图像。这对于进一步的处理是完全有效的,并且通常明确地应用于某些机器学习方法中。我见过它被称为“美白”,但更恰当的说法是a 您使用的绘图功能似乎希望图像在[0,1]范围内。这是打印功能的限制,而不是您的标准化。完全可以展示你的形象
要将标准化到[0,1]范围,不应使用平均值和标准偏差,而应使用最大值和最小值,如所示。如果值与平均值的距离超过一个标准差,那么会发生什么情况?您能提供整个堆栈跟踪吗?@user2743您好。。。如果您觉得我的答案有用,请投票表决并/或将其标记为解决方案。谢谢:)