2016-10-18 187 views
1

我有一个小的代码示例绘制在matplotlib图像,并且该图像被示出为这样的:matplotlib图像示出了在黑色和白色,但我想灰色

enter image description here

请注意,在黑色图像框有黑色的背景,而我所需的输出是这样的:

enter image description here

我的代码来绘制图像是这样的:

plt.subplot(111) 
plt.imshow(np.abs(img), cmap = 'gray') 
plt.title('Level 0'), plt.xticks([]), plt.yticks([]) 
plt.show() 

我的理解是,应cmap=grey灰度显示。下面是矩阵img的片段被画在:

[[ 192.77504036 +1.21392817e-11j 151.92357434 +1.21278246e-11j 
    140.67585733 +6.71014111e-12j 167.76903747 +2.92050743e-12j 
    147.59664180 +2.33718944e-12j 98.27986577 +3.56896094e-12j 
    96.16252035 +5.31530804e-12j 112.39194666 +5.86689097e-12j.... 

缺少什么我在这里?

+0

也许这可以帮助http://stackoverflow.com/questions/3823752/display-image-as-grayscale-using-matplotlib – Maddy

+0

@Mani:使用'gray_r'没有帮助。它显示为带有黑色边缘的部分白色。 – CyprUS

+0

@CyprUS该链接(和http://matplotlib.org/examples/color/colormaps_reference.html)似乎暗示'Greys_r'。不确定'_r'。你可以试试吗?不过,我认为这不是问题。 – pingul

回答

0

对于我的情况下,我想要的颜色(灰色)实际上是“负面”像素。从图像矩阵中减去128会使像素范围从0-255到-128到+127。负像素通过matplotlib包以“灰色”颜色显示。

val = np.subtract(imageMatrix,128) 
plt.subplot('111') 
plt.imshow(np.abs(val), cmap=plt.get_cmap('gray'),vmin=0,vmax=255) 
plt.title('Image'), plt.xticks([]), plt.yticks([]) 
plt.show() 

我将迎来自己的答案接受,因为较早接受了答案不谈论负面规模治疗的像素。

0

的问题似乎是,你有三个通道,而应该只有一个,而数据应该[0, 1]之间进行标准化。我得到使用这个正确的面色灰白缩放图像:

import matplotlib.pyplot as plt 
import matplotlib.image as mpimg 
import numpy as np 

img = mpimg.imread('Lenna.png') 
# The formula below can be changed -- the point is that you go from 3 values to 1 
imgplot = plt.imshow(np.dot(img[...,:3], [0.33, 0.33, 0.33]), cmap='gray') 
plt.show() 

这给了我:

enter image description here

此外,数据的快照:

[[ 0.63152942 0.63152942 0.63800002 ..., 0.64705883 0.59658825 0.50341177] 
[ 0.63152942 0.63152942 0.63800002 ..., 0.64705883 0.59658825 0.50341177] 
[ 0.63152942 0.63152942 0.63800002 ..., 0.64705883 0.59658825 0.50341177] 
...] 
+0

我会试试这个,让你知道 – CyprUS

相关问题