Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy flatten RGB image array

I have 1,000 RGB images (64X64) which I want to convert to an (m, n) array.

I use this:

import numpy as np
from skdata.mnist.views import OfficialImageClassification
from matplotlib import pyplot as plt
from PIL import Image                                                            
import glob
import cv2

x_data = np.array( [np.array(cv2.imread(imagePath[i])) for i in range(len(imagePath))] )
print x_data.shape

Which gives me: (1000, 64, 64, 3)

Now if I do:

pixels = x_data.flatten()
print pixels.shape

I get: (12288000,)

However, I require an array with these dimensions: (1000, 12288)

How can I achieve that?

like image 638
apples-oranges Avatar asked May 01 '16 14:05

apples-oranges


People also ask

How do I flatten an image in NumPy?

'C' means to flatten in row-major (C-style) order. 'F' means to flatten in column-major (Fortran- style) order. 'A' means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. 'K' means to flatten a in the order the elements occur in memory.

What does flatten () do in NumPy?

The flatten() function is used to get a copy of an given array collapsed into one dimension. 'C' means to flatten in row-major (C-style) order.

How do I flatten a list of NumPy arrays?

You can flatten a NumPy array ndarray with the numpy. label() function, or the ravel() and flatten() methods of numpy.


1 Answers

Apply the numpy method reshape() after applying flatten() to the flattened array:

  x_data = np.array( [np.array(cv2.imread(imagePath[i])) for i in range(len(imagePath))] )

  pixels = x_data.flatten().reshape(1000, 12288)
  print pixels.shape
like image 161
Ray Avatar answered Oct 24 '22 16:10

Ray