Skip to main content
 首页 » 编程设计

python之图像数据处理后超出0-1范围

2024年09月07日7langtianya

我正在随机调整图像的亮度、饱和度等。但经过调整后,img_data的值超出了0-1,导致imshow无法正常工作。

tf.InteractiveSession() 
image_raw_data=tf.read_file('C:/Users/User/PycharmProjects/Neural_Network\\cat.jpg') 
sess=tf.Session() 
img_data=tf.image.decode_jpeg(image_raw_data,channels=3) 
img_data=tf.image.convert_image_dtype(img_data,dtype=tf.float32) 
img_data=tf.image.resize_images(img_data,[300,300],method=0) 
img_data=tf.image.random_brightness(img_data,max_delta=32/255) 
plt.imshow(img_data.eval()) 

错误是:

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

我可以知道我应该如何正确地转换图像以便显示它吗?

请您参考如下方法:

我要做的纯粹是将图像可视化,即添加图像中的最小值,然后除以最大值,以如下方式将图像限制在 0 和 1 之间:

import numpy as np 
img_data_np = img_data.eval() 
min_val = np.min(img_data_np) 
max_val = np.max(img_data_np) 
img_data_clamped = (img_data_np - min_val) / (max_val - min_val) 
plt.imshow(img_data_clamped)