【发布时间】:2021-08-09 17:26:30
【问题描述】:
我在 NumPy 数组中有一组灰度图像。我想在将图像输入 CNN 之前将它们转换为 RGB(我正在使用迁移学习)。但是当我尝试将图像转换为 RGB 时,图像中的几乎所有信息都丢失了!我尝试了几个库,但它们都导致(几乎)相同的结果:
原图:
import matplotlib.pyplot as plt
plt.imshow(X[0])
使用 cv2:
import cv2
img = X[0].astype('float32') #cv2 needs float32 or uint8 for handling images
rgb_image = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
plt.imshow(rgb_image)
img = X[0].astype('uint8') #cv2 needs float32 or uint8 for handling images
rgb_image = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
plt.imshow(rgb_image)
使用 PIL
from PIL import Image
img = Image.fromarray(X[0])
img_rgb = img.convert("RGB")
plt.imshow(img_rgb)
使用 NumPy
>从这里:https://stackoverflow.com/a/40119878/14420572
stacked_img = np.stack((X[0],)*3, axis=-1)
plt.imshow(stacked_img)
我应该怎么做才能确保图像在没有质量损失的情况下转换?
【问题讨论】:
-
首先要搞清楚灰度图的数据类型是什么。
-
灰度图像是形状为 (224, 224) 的 NumPy 数组。谢谢,
-
什么是数据类型? uint8?整数8?整数16?浮动32?等
-
另外,您应该找出灰度图像中的最小和最大灰度值。
-
它是
numpy.float64,图像中的最小值和最大值很奇怪,np.max(X) = 4387.0和np.min(X) = -126.0。你认为这是问题所在吗?
标签: python image numpy opencv python-imaging-library