Home > Mobile >  converting rgb numpy image to list of rgb and corresponding index values
converting rgb numpy image to list of rgb and corresponding index values

Time:02-01

I have a numpy matrix representing rgb image. Its shape is (n,m,3) with n rows, m columns and 3 channels. I want to convert it to list of rgb values along with corresponding indeces.

I can convert to list of rgb values but I am trying to have row and col indeces alongside as well.

We can do something like this for rgb values only.

flat_image = np.reshape(image, [-1,3]) # shape = [mxn, 3]

After also adding row and column number, the shape should be [mxn, 3 2]

so first three columns in the flat image represent rgb, fourth column represents row number from the original image array and fifth column represent col number from the original imagem array.

CodePudding user response:

You can use numpy.indices to construct the row/column indices and then concatenate that with your flat_image

indices = np.indices(image.shape[:-1])

result = np.concatenate([flat_image, indices], axis=-1)
  • Related