4

I have a small code sample to plot images in matplotlib, and the image is shown as this :

enter image description here

Notice the image in the black box has black background, while my desired output is this :

enter image description here

My code to plot the image is this :

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

My understanding is that cmap=grey should display it in grayscale. Below is a snippet of the matrix img being plotted :

[[ 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....

What am I missing here ?

Maddy
  • 2,025
  • 5
  • 26
  • 59
CyprUS
  • 4,159
  • 9
  • 48
  • 93
  • Maybe this can help http://stackoverflow.com/questions/3823752/display-image-as-grayscale-using-matplotlib – Maddy Oct 18 '16 at 15:11
  • @Mani : Using `gray_r` does not help . It shows up in partial white with black edges. – CyprUS Oct 18 '16 at 15:16
  • @CyprUS That link (and http://matplotlib.org/examples/color/colormaps_reference.html) seems to suggests `Greys_r`. Not sure about the `_r`. Could you try both? I don't think that is the problem however. – pingul Oct 18 '16 at 15:17
  • @Mani : Actually I tried all `Greys_r`, `Greys', `gray` and `gray_r` . Nothing seems to work – CyprUS Oct 18 '16 at 15:33
  • @CyprUS Have you tried to rescale the data between `[0, 1]`? – pingul Oct 18 '16 at 15:41
  • @pingul : Yes I tried that using a sigmoid function. It shows up as a pure black and white image, no grey color at all. I think the reason my image does not have gray element is due to my normalizing it between [0,255]. This results in any gray color being converted to 0, I think. not sure yet – CyprUS Oct 18 '16 at 15:56
  • I do not know how it handles imaginary numbers, but probably the `vmin` and `vmax` arguments of imshow will do the trick, see [this answer](http://stackoverflow.com/questions/12760797/imshowimg-cmap-cm-gray-shows-a-white-for-128-value). – Jan Kuiken Oct 18 '16 at 16:27

2 Answers2

1

For my case, the color (gray) which I wanted is actually "negative" pixels. Subtracting 128 from the image matrix brings the range of pixels from 0-255 to -128 to +127. The negative pixels is displayed in the "gray" color by the package 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()

I will mark my own answer as accepted, as the answer accepted earlier does not talk about treating the pixels on negative scale.

novice
  • 800
  • 2
  • 11
  • 25
CyprUS
  • 4,159
  • 9
  • 48
  • 93
0

The problem seems to be that you have three channels while there should be only one, and that the data should be normalized between [0, 1]. I get a proper looking gray scaled image using this:

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()

This gives me:

enter image description here

Also, a snapshot of the data:

[[ 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]
 ...]
pingul
  • 3,351
  • 3
  • 25
  • 43