【发布时间】:2021-10-29 05:21:49
【问题描述】:
我已将一些图像从 RGB 转换为灰度以用于机器学习。 但是转换后的灰度图的形状还是3,和彩色图一样。
转换代码:
from PIL import Image
img = Image.open('path/to/color/image')
imgGray = img.convert('L')
imgGray.save('path/to/grayscale/image')
检查图像形状的代码:
import cv2
im_color = cv2.imread('path/to/color/image')
print(im_color.shape)
im_gray2 = cv2.imread('path/to/grayscale/image')
print(im_gray2.shape)
【问题讨论】:
-
使用 skimage 库。函数:rgb2gray()。您将获得单层灰度图像作为输出。只需确保将 RGB 图像作为 numpy ndarray 即可。
-
你为什么要使用 PIL 和 OpenCV - 你很少需要两者,而且它往往会导致混乱。
标签: python image image-processing python-imaging-library cv2