1

I am trying to extract the alpha channel of a RGBA matrix in the following format:

[                      [
 [                      [
  [1, 2, 3, 4],   to     [4],
  [5, 6, 7, 8]   ====>   [8]
 ]                      ]
]                      ]

I wanted to know if the code I currently have can be improved in terms of speed:

import numpy
import cv2

image = np.full((10, 10, 4), 0, numpy.uint8)

r, g, b, a = cv2.split(image)

rgb = cv2.merge((r, g, b))
alpha = np.array([np.vstack(e).tolist() for e in a])  # Can this be faster?

With big matrices, the last line can take up to more than 0.1s to execute. I need it to be less than that.

Thank you!

1 Answers1

0

Just reshape it!

a.reshape((sizeX, sizeY, 1))

That should give you the same effect with a simple metadata change.

shash
  • 965
  • 8
  • 16