0

Using this code :

from scipy import misc
import matplotlib.pyplot as plt

images = data.test.images[0:9]
plt.imshow(images[0].reshape(28 , 28))

print(images[0].shape)

I plot the image :

enter image description here

Using misc from scipy :

face = misc.face()
misc.imsave('face.png', face) # First we need to create the PNG file
face = misc.imread('face.png')

print(face.shape)
plt.imshow(face)

enter image description here

I plot image :

How to convert face to 2 dimensional image which can be plotted using plt.imshow ?

Using :

plt.imshow(face.reshape(768 , 1024))

produces error :

ValueError                                Traceback (most recent call last)
<ipython-input-104-99fef1cec0d2> in <module>()
      6 plt.imshow(face)
      7 
----> 8 plt.imshow(face.reshape(768 , 1024))

ValueError: cannot reshape array of size 2359296 into shape (768,1024)

I'm not attempting to convert the image to greyscale but to 2 dimensional instead of 3 dimensional.

Update :

Checking the a single pixel value : print(face[0][0]) is : [121 112 131] . Should I take the average of [121 112 131] as part of reshaping ?

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 4
    Possible duplicate of [How can I convert an RGB image into grayscale in Python?](https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python) – Cris Luengo May 29 '18 at 19:20
  • 1
    The `face` image is a 3D array representing a 2D image where each pixel has 3 values (RGB). The 3rd dimension is the various channels. Converting it to a gray value image turns it into a 2D array. This is (typically implemented as) a weighted average of the three values (green is usually weighted more strongly because that is what our eyes are most sensitive to). This weighted average is the most sensical approach, but you can certainly do many other things. It all depends on the application. What do you want to do with the image? – Cris Luengo May 29 '18 at 19:47
  • @CrisLuengo I'm working on a convolutional neural network, indeed you are right, i mistakenly assumed converting to ' gray value ' removes colour portion of image. If I may I'll add an answer for this... – blue-sky May 29 '18 at 20:00
  • Converting to gray value certainly removes the color, and leaves only the image intensity component. If color is important in your application, put the full 3D array into your CNN. I don't know of a sensical approach that converts the color image to a 2D matrix and keeps all information intact. – Cris Luengo May 29 '18 at 20:06
  • @CrisLuengo are the images in mnist dataset not a 2d matrix ? – blue-sky May 29 '18 at 20:12
  • 1
    Yes, MNIST is only gray-value images. If you have a gray-value image stored as a PNG, and being read as a color image, then all three of the RGB channels will be identical. You can simply use indexing to remove the 3D dimension: `face[:,:,0]` (this discards the duplicate data). – Cris Luengo May 29 '18 at 20:17

2 Answers2

1

This code works as expected :

   def rgb2gray(rgb):
        return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])


    gray = rgb2gray(face)  
    print(gray.shape)

    plt.imshow(gray)

But colour is skewed. rgb2gray source : How can I convert an RGB image into grayscale in Python?

Also kudos to comments in helping me arrive at this.

blue-sky
  • 51,962
  • 152
  • 427
  • 752
0

I don't really understand what you are trying to do here. If you want to be able to plot 3D data with imshow that is possible out of the box. If you want to convert to gray scale, check out this. Alternatively, if you want to slice a 3D dataset such that you get a 2D matrix, look at this and :

from scipy.misc import face
f = face()
print(f.shape)
print(f[..., 0].shape) # slicing in last dimension
import matplotlib.pyplot as plt
plt.imshow(f[..., 0])
cvanelteren
  • 1,633
  • 9
  • 16