简体   繁体   English

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

[英]Converting an RGB image to grayscale in Python

I am trying to convert an RGB image to grayscale using skimage in Python. 我正在尝试使用Python中的skimage将RGB图像转换为灰度。 Here's what I do: 这是我做的:

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)

for a bunch of image files. 对于一堆图像文件。 My input image looks like this: 我的输入图像如下所示:

彩色图像

And the output image looks like this: 输出图像如下所示:

黑色图像

The expected output is this: 预期的输出是这样的:

灰色图像

What can be the issue here? 这可能是什么问题?

Figured it out. 弄清楚了。 The problem was of contrast. 问题是对比的。

I printed out the image and saw that the values were all close to 0. I introduced a small line to stretch the contrast between 0 and 255 in the loop that made it work. 我打印出图像,发现这些值都接近于0.我引入了一条小线,在循环中拉伸0到255之间的对比度,使其工作。

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

Where rescale_intensity was imported from skimage.exposure . rescale_intensity导入skimage.exposure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM