我给出了一个图像(32,32,3)和两个代表均值和标准的矢量(3,)。我试图通过将图像置于可以减去均值并除以标准的状态来对图像进行标准化,但是当我尝试绘制它时我得到以下错误。
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))
答案 0 :(得分: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 :(得分:0)
通过将图像的均值设置为零并将其标准偏差设置为1来对图像进行标准化,就像您一样,会产生一个图像,其中大部分但不是全部的像素都在[-2,2]范围内。这对于进一步处理非常有效,并且通常在某些机器学习方法中明确应用。我已经看到它被称为"美白",但更恰当地称为standardization transform。
您使用的绘图功能似乎要求图像在[0,1]范围内。这是绘图功能的限制,而不是标准化的限制。 Other image display functions完全能够展示您的形象。
要标准化为[0,1]范围,您不应使用均值和标准差,而应使用最大值和最小值,如Pitto's answer所示。