0

I have a numpy array(trainData.npy) for image processing. It contains 2000 images, garyscale and height 450 , width 600.

Train images shape:(2000, 1, 450, 600)

I'm looking for a way to plot or show one of these images. I've used this code but i'v got TypeError: Invalid dimensions for image data error/

import numpy as np
import matplotlib.pyplot as plt

img = img_train[0]
plt.imshow(img)
plt.show()
3
  • maybe you first should check for the shape of your img Commented Jun 1, 2018 at 15:02
  • I've already done. img_train[0].shape the answer is (1, 450, 600) Commented Jun 1, 2018 at 15:14
  • image show can show two dimensional arrays. img = img_train[0,0] Commented Jun 1, 2018 at 17:08

1 Answer 1

1

Just reshape your image:

import numpy as np
import matplotlib.pyplot as plt

img = img_train[0] #img has dim (ncolor=1, nlines=450, nrows=600)
img = reshape(450, 600) #img has dim (450, 600)
#img = img / img.max #if you need rescaling of greyscale to be in [0..1]
plt.imshow(img)
plt.show()

P.S.: I personally find it a bit more intuitive if pictures are dimensional-ordered (nlines, nrows, ncolorchanel) than your choice (ncolorchanel, nlines, nrows)

Sign up to request clarification or add additional context in comments.

1 Comment

img = img.reshape(450, 600) it worked great. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.