“cmap=’Greys’对彩色图像不起作用是为什么?”

huangapple go评论57阅读模式
英文:

Why cmap='Greys' does not do anything for a colored image?

问题

为什么仅仅改变颜色映射并不能将彩色图像转换为黑白图像?

img = imread('https://cdn.shopify.com/s/files/1/1257/7487/articles/bee-flower_1024x.jpg?v=1585344606')
plt.imshow(img, cmap='Greys')

plt.colorbar()
plt.xticks([])
plt.yticks([])
plt.show()
英文:

Why simply changing the color map does not convert a colored image to black and white?

img = imread('https://cdn.shopify.com/s/files/1/1257/7487/articles/bee-flower_1024x.jpg?v=1585344606')
plt.imshow(img, cmap='Greys')

plt.colorbar()
plt.xticks([])
plt.yticks([])
plt.show()

答案1

得分: 2

img是一个类似数组的数据结构。它的大小为(M,N,3),其中MN是原始图像的边长,以像素为单位。这意味着数据格式为RGB值。根据matplotlib文档,如果输入数组为RBG或RBGA格式,plt.imshow(...)cmap参数将被忽略。

文档链接:https://matplotlib.org/stable/api/_as-gen/matplotlib.pyplot.imshow.html#matplotlib.pyplot.imshow

英文:

Explanation

img is an array-like data structure. It has size (M,N,3) where M and N are the side lengths of the original image, in pixels. This means that the data is formatted as RGB values. According to the matplotlib documentation, the cmap parameter of plt.imshow(...) is ignored if the input array is in RBG or RBGA format.

Link to documentation: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html#matplotlib.pyplot.imshow

huangapple
  • 本文由 发表于 2023年2月26日 19:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定