plt.imshow 显示 IPython 中灰度图像的彩色图像 [英] plt.imshow shows color images for grayscale images in IPython

查看:54
本文介绍了plt.imshow 显示 IPython 中灰度图像的彩色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 RGB 图像 imRGB

import cv2
import matplotlib.pyplot as plt

#Loading the RGB image
imRGB = cv2.imread('*path*')

imRGB.shape
>>(128L, 128L, 3L)

#plotting
plt.imshow(imRGB)

我将其转换为灰度图像 imGray

imGray = cv2.cvtColor(imRGB, cv2.COLOR_BGR2GRAY)

imGray.shape
>>(128L, 128L)

#plotting
plt.imshow(imGray)

问题:为什么灰度图像imGray显示为彩色?

Question: Why does the grayscale image imGray is shown in color?

推荐答案

imRGB 是一个 3D 矩阵(高 x 宽 x 3).
现在 imGray 是一个 2D 矩阵,其中没有颜色,只有亮度"值.所以 matplotlib 默认应用了一个颜色图.

imRGB was a 3D matrix (height x width x 3).
Now imGray is a 2D matrix in which there are no colors, only a "luminosity" value. So matplotlib applied a colormap by default.

阅读本页并尝试应用灰度颜色图或不同的颜色图,检查结果.

Read this page and try to apply a grayscale colormap or a different colormap, check the results.

plt.imshow(imGray, cmap="gray")

使用此页面作为颜色图的参考.某些颜色图无法工作,如果发生,请尝试另一个颜色图.

Use this page as reference for colormaps. Some colormap could not work, if it occurs try another colormap.

这篇关于plt.imshow 显示 IPython 中灰度图像的彩色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆