0

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?

1 Answer 1

1

Just use:

arr.reshape(-1, n_channels)

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

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

Comments

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.