在 Python 中将 RGB 图像转换为灰度图像

Converting an RGB image to grayscale in Python

我正在尝试使用 Python 中的 skimage 将 RGB 图像转换为灰度图像。这是我的做法:

for im_path in glob.glob(os.path.join(pos_raw, "*")):
    im = imread(im_path)
    im = color.rgb2gray(im)
    image_name = os.path.split(im_path)[1].split(".")[0] + ".pgm"
    image_path = os.path.join(pos_img_path, image_name)
    imwrite(image_path, im)

一堆图像文件。 我的输入图像如下所示:

输出图像如下所示:

预期的输出是这样的:

这可能是什么问题?

想通了。问题是对比。

我打印出图像并看到所有值都接近于 0。我在循环中引入了一条小线来拉伸 0 到 255 之间的对比度,从而使其起作用。

im = rescale_intensity(im, out_range=(0, 255))

其中 rescale_intensity 是从 skimage.exposure 导入的。