图像未转换为灰度

Images is not converting to gray scale

我已经通过OpenCV读取了一个文件夹中的所有图像。然后转换为灰度,使用 pyplot 我已经显示了所有图像。但不是灰度图像看起来像黄色。

import glob
import cv2
import matplotlib.pyplot as plt
from skimage.color import rgb2gray

images = [cv2.imread(file) for file in glob.glob(r"C:\Users\USER\Handcrafted dataset/*.jpg")]
for img in images:
    img = rgb2gray(img)
    plt.figure(figsize=(10,10))
    plt.imshow(img)

示例输出:

我应该怎么做才能将它们转换为正确的灰度图像?

发生这种情况是因为 Matplotlib 在绘制单个通道时使用了颜色图。在他们的 docs 中有一个声明说:

The input may either be actual RGB(A) data, or 2D scalar data, which will be rendered as a pseudocolor image. Note: For actually displaying a grayscale image set up the color mapping using the parameters cmap='gray', vmin=0, vmax=255.

因此,将最后一行更改为:

plt.imshow(img, cmap='gray')

这将使用灰度映射绘制通道。