【问题标题】:Quantize Float Array to Byte Array in Python在 Python 中将浮点数组量化为字节数组
【发布时间】:2022-01-24 05:37:47
【问题描述】:

我有一个像这样的浮点数组 [0.1,0.4,1.5,2.22,3] 其中最大值始终为 3.0

我想将其量化为 0 到 255 的字节数组 对于我想量化的每个值,例如 (byte)((value / 3.0) * 255.0)

有没有办法在 numpy 中非常快速地做到这一点,而不是在 Python 中迭代每个值并重建一个新的字节数组?

【问题讨论】:

  • 你不想使用 255.0,你想使用 255.9999。否则,您的 255 人将被严重低估。

标签: python arrays byte quantize


【解决方案1】:

使用astype

import numpy as np

value = np.array([0.1, 0.4, 1.5, 2.22, 3])
out = ((value / 3.0) * 255.0).astype(np.ubyte)
print(out)

# Output
array([  8,  34, 127, 188, 255], dtype=uint8)

【讨论】:

  • 谢谢,效果很好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多