HR/LR文件夹灰度图像读取后显示偏绿问题排查及灰度显示方案咨询
问题原因及解决方法
问题根源
- 维度不匹配导致数据异常:你设置的目标图像尺寸是3通道格式(如
(256,256,3)),但通过as_gray=True读取的是单通道灰度图,两者维度不兼容。transform.resize在处理时会错误调整数据结构,后续显示时触发异常渲染。 - 默认配色导致绿色显示:matplotlib对单通道图像默认使用
viridis色系(绿色调),没有指定灰度配色方案时,就会呈现绿色。
修正步骤
1. 统一图像维度
如果仅需处理灰度图,将目标尺寸改为单通道格式:
low_resolution_shape = (64, 64) high_resolution_shape = (256, 256)
如果后续模型需要3通道输入,可在resize后显式扩展通道(替代原注释的代码):
# 在resize后添加通道扩展代码 hr_img1_high_resolution = np.repeat(hr_img1_high_resolution[..., np.newaxis], 3, axis=-1) lr_img1_low_resolution = np.repeat(lr_img1_low_resolution[..., np.newaxis], 3, axis=-1)
2. 指定灰度显示配色
在imshow调用时添加cmap='gray'参数,强制使用灰度配色:
# 显示高分辨率图 axs[i].imshow(high_resolution_images[i], cmap='gray') # 显示低分辨率图 axs[i].imshow(low_resolution_images[i], cmap='gray')
完整修正代码
from skimage import io, transform import os import numpy as np import matplotlib.pyplot as plt import glob def sample_images(data_dir, batch_size, high_resolution_shape, low_resolution_shape): hr_dir = os.path.join(data_dir, 'hr') # 高分辨率图像文件夹路径 lr_dir = os.path.join(data_dir, 'lr') # 低分辨率图像文件夹路径 # 获取HR和LR目录下所有图像的路径列表 hr_images = glob.glob(os.path.join(hr_dir, '*.*')) lr_images = glob.glob(os.path.join(lr_dir, '*.*')) # 随机选择一批图像 hr_images_batch = np.random.choice(hr_images, size=batch_size) lr_images_batch = np.random.choice(lr_images, size=batch_size) low_resolution_images = [] high_resolution_images = [] for hr_img, lr_img in zip(hr_images_batch, lr_images_batch): # 读取当前HR图像为单通道灰度图 hr_img1 = io.imread(hr_img, as_gray=True) # 调整HR图像尺寸 hr_img1_high_resolution = transform.resize(hr_img1, high_resolution_shape, mode='constant') # 如果需要3通道输入,取消下面一行注释 # hr_img1_high_resolution = np.repeat(hr_img1_high_resolution[..., np.newaxis], 3, axis=-1) # 读取当前LR图像为单通道灰度图 lr_img1 = io.imread(lr_img, as_gray=True) # 调整LR图像尺寸 lr_img1_low_resolution = transform.resize(lr_img1, low_resolution_shape, mode='constant') # 如果需要3通道输入,取消下面一行注释 # lr_img1_low_resolution = np.repeat(lr_img1_low_resolution[..., np.newaxis], 3, axis=-1) # 数据增强:随机水平翻转 if np.random.random() < 0.5: hr_img1_high_resolution = np.fliplr(hr_img1_high_resolution) lr_img1_low_resolution = np.fliplr(lr_img1_low_resolution) high_resolution_images.append(hr_img1_high_resolution) low_resolution_images.append(lr_img1_low_resolution) # 将列表转换为numpy数组并返回 return np.array(high_resolution_images), np.array(low_resolution_images) data_dir = r'C:\\Users\\hp\\Desktop\\dataasets\\Newfolder\\data\\' batch_size = 2 low_resolution_shape = (64, 64) # 单通道灰度图尺寸 high_resolution_shape = (256, 256) # 单通道灰度图尺寸 # 采样一批图像 high_resolution_images, low_resolution_images = sample_images(data_dir=data_dir, batch_size=batch_size, low_resolution_shape=low_resolution_shape, high_resolution_shape=high_resolution_shape) # 显示高分辨率图像 fig, axs = plt.subplots(nrows=1, ncols=batch_size, figsize=(12, 4)) for i in range(batch_size): axs[i].imshow(high_resolution_images[i], cmap='gray') axs[i].axis('off') plt.suptitle('高分辨率图像') plt.show() # 显示低分辨率图像 fig, axs = plt.subplots(nrows=1, ncols=batch_size, figsize=(12, 4)) for i in range(batch_size): axs[i].imshow(low_resolution_images[i], cmap='gray') axs[i].axis('off') plt.suptitle('低分辨率图像') plt.show()
内容的提问来源于stack exchange,提问作者stella
相关产品推荐
相关产品推荐

