MLS - Course 3 - Week 1 - Reshaping of image

Divide by 255 so that all values are in the range 0 - 1

original_img = original_img / 255

Reshape the image into an m x 3 matrix where m = number of pixels

(in this case m = 128 x 128 = 16384)

Each row will contain the Red, Green and Blue pixel values

This gives us our dataset matrix X_img that we will use K-Means on.

X_img = np.reshape(original_img, (original_img.shape[0] * original_img.shape[1], 3))
print(X_img)

Can someone explain me how the original image was reshaped?
I am a bit confused with the dimensions…

Hi!

The original image is of shape:
(128, 128, 3)
(x, y, rgb_values)

The image was reshaped into a shape of:
(16384, 3)
(xy, rgb_values)

From the numpy documentation this is done using C-like order by default.
See the documentation below.
https://numpy.org/doc/stable/reference/generated/numpy.reshape.html