使用OpenCV和NumPy提高阵列提取速度

时间:2021-08-16 21:23:49

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

我试图以下列格式提取RGBA矩阵的alpha通道:

[                      [
 [                      [
  [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.

对于大矩阵,最后一行最多可能需要0.1秒才能执行。我需要它不到那个。

Thank you!

1 个解决方案

#1


0  

Just reshape it!

重新塑造它!

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

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

通过简单的元数据更改,这应该会产生相同的效果。

#1


0  

Just reshape it!

重新塑造它!

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

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

通过简单的元数据更改,这应该会产生相同的效果。