Juan David
Juan David

Reputation: 2797

flatten array of arrays into a vector of arrays

I'm generating matrix representations of images with height*width size, and I need to transform them in a vector of pixels. To generate the images, I'm using the following instruction

np.array([[np.random.randint(0, 255, 3) for dummy_row in range(height)] for dummy_col in range(width)])

e.g., (2x2) image

array([[[132, 235,  40],
        [234,   1, 160]],
       [[ 69, 108, 218],
        [198, 179, 165]]])

I tried to use flatten(), but is not creating a one-dimension array of pixels, but is pilling all the values together

array([132, 235,  40, 234,   1, 160,  69, 108, 218, 198, 179, 165])

when I'm requiring

array([132, 235,  40], [234,   1, 160],  [69, 108, 218], [198, 179, 165]])

is there a built-in function to get this output?

Upvotes: 0

Views: 97

Answers (1)

norok2
norok2

Reputation: 26906

Just use:

arr.reshape(-1, n_channels)

or similar (where arr is the NumPy array containing the image data).

Upvotes: 1

Related Questions