尝试标准化 Python 图像获取错误 - RGB 值必须在 0..1 范围内 [英] Trying to normalize Python image getting error - RGB values must be in the 0..1 range

查看:72
本文介绍了尝试标准化 Python 图像获取错误 - RGB 值必须在 0..1 范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个图像 (32, 32, 3) 和两个表示均值和标准差的向量 (3,).我正在尝试通过使图像进入可以减去均值并除以 std 的状态来标准化图像,但是当我尝试绘制它时出现以下错误.

I'm given an image (32, 32, 3) and two vectors (3,) that represent mean and std. I'm trying normalize the image by getting the image into a state where I can subtract the mean and divide by the std but I'm getting the following error when I try to plot it.

ValueError: Floating point image RGB values must be in the 0..1 range.

我理解错误,所以我想我在尝试标准化时没有执行正确的操作.下面是我尝试使用标准化图像的代码.

I understand the error so I'm thinking I'm not performing the correct operations when I try to normalize. Below is the code I'm trying to use normalize the image.

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

推荐答案

您的错误似乎表明图像缺乏标准化.

Your error seems to point to a lack of normalization of the image.

我在深度学习项目中使用这个函数来规范化图像

I've used this function to normalize images in my Deep Learning projects

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

这篇关于尝试标准化 Python 图像获取错误 - RGB 值必须在 0..1 范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆