为了账号安全,请及时绑定邮箱和手机立即绑定

将 uint32 元组转换为 uint8 的 3d numpy 数组的最佳方法

将 uint32 元组转换为 uint8 的 3d numpy 数组的最佳方法

潇湘沐 2023-07-05 17:55:20
我从外部 api 获得了以 uint32 元组形式呈现的图像,其中每个 uint32 由 4 个 uint8 组成,其中包含 r、g、b 和 alpha(始终 = 0),我需要将其转换为格式与我相同的 3d numpy 数组可以从imageio.imread. 问题是,当我使用numpy.view颜色顺序时,颜色顺序是相反的。这是我编写的代码,可以正常工作,但我想知道是否有更好的转换方法。        frame_buffer_tuple = ... # here we have tuple of width*height length         framebuffer = np.asarray(frame_buffer_tuple).astype(dtype='uint32').view(dtype='uint8').reshape((height, width, 4)) # here I have height x width x 4 numpy array but with inverted colors (red instead of blue) and alpha I don't need         framebuffer = np.stack((framebuffer[:, :, 2], framebuffer[:, :, 1], framebuffer[:, :, 0]), axis=2) # here I've got correct numpy height x width x 3 array我更关心执行时间,然后是内存,但由于我可以拥有 7680 × 4320 图像,因此两者可能都很重要。谢谢!
查看完整描述

2 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

你只需要反转最后一个维度吗?尝试

framebuffer = framebuffer [:, :, :2:-1] # drops alpha


查看完整回答
反对 回复 2023-07-05
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

实际上有效的是:

framebuffer = np.asarray(frame_buffer_tuple).astype('uint32').view('uint8').reshape((height, width, 4))[:, :, 2::-1]

我暂时将其标记为解决方案,直到有人提出解决该问题的方法。


查看完整回答
反对 回复 2023-07-05
  • 2 回答
  • 0 关注
  • 137 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信