我认为您对uint32
代表的含义感到困惑。它是 4 段uint8
整数。
如果您有浮点数据,并且想要以灰度表示,您不想将其重新缩放到完整的 32 位范围,您希望将其重新缩放到 8 位范围,并对红色、绿色重复此操作, 和蓝色波段(然后可能放入恒定的 alpha 波段)。
您也可以只使用不同的字节顺序。Y8
是一个单一的灰度,8 位波段,Y16
是一个单一的,灰度 16 位波段。(查看mencoder -rawvideo format=help
完整(尽管有些混乱)列表的输出。)
只是为了说明使用 numpy 将 32 位整数视为四个 8 位整数波段:
import numpy as np
height, width = 20,20
# Make an array with 4 bands of uint8 integers
image = np.zeros((height, width, 4), dtype=np.uint8)
# Filling a single band (red)
b,g,r,a = image.T
r.fill(255)
# Fill the image with yellow and leave alpha alone
image[...,:3] = (255, 255, 0)
# Then when we want to view it as a single, 32-bit band:
image32bit = image.reshape(-1).view(np.uint32).reshape(height, width)
# (Note that this is a view. In other words, we could change "b" above
# and it would change "image32bit")
但是,在您的情况下,您可能想要做更多这样的事情:
import numpy as np
from videosink import VideoSink
height, width = 20,20
numframes = 1000
data = np.random.random((height, width, numframes))
# Rescale your data into 0-255, 8-bit integers
# (This could be done in-place if you need to conserve memory)
d ata_rescaled = 255.0 / (data.max() - data.min()) * (data - data.min())
data_rescaled = data_rescaled.astype(np.uint8)
# The key here is the "Y8" format. It's 8-bit grayscale.
video = VideoSink((height,width), "test", rate=20, byteorder="Y8")
# Iterate over last axis
for frame in data.T:
video.run(frame.T)
video.close()