1

I am trying to read a file of images from a path on my local computer and I want generate an arrary to represent these images. How do I represent them all in an array with dimension of 2.

images = [imageio.imread(path) for path in glob.glob([pathtoimages])]
images = np.asarray(images)
print(images.shape)
scaler = StandardScaler()

# Fit on training set only.
scaler.fit(images)
#
## Apply transform to both the training set and the test set.
#train_img = scaler.transform(images)

I am following this guide to do PCA on a set of images that are all 257x257. When I do print(images.shape), I get (130, 257, 257, 3) as there are 130 images of 257x257 with 3 channels. When I try to do StandardScaler I get the following error.

ValueError: Found array with dim 4. StandardScaler expected <= 2.

My main question is how do I compress the array of size 4 into an array with only 2 dimensions? I already have this post and this one, but am still unsure.

Also, make sure to replace [pathtoimages] in glob.glob() function when running code.

john
  • 619
  • 9
  • 24

1 Answers1

0

you need to flatten your image before adding it to the list. So your image of size (257,257,3) will become 1D array of size 257*257*3

images = [np.array(imageio.imread(path)).flatten()  for path in glob.glob(pathtoimages)]
images = np.array(images)
print(images.shape)
user8190410
  • 1,264
  • 2
  • 8
  • 15