6

所以,这应该是对这个线程的评论,但它显然已经关闭,所以就这样吧。正如这里所建议的那样,我一直在非常成功地使用 matplotlib 和 numpy 和 mencoder。从那以后,我将Voki Codder 缓冲区用于标准输入解决方案,这大大加快了整个过程。问题是,我在命令的 -format="bgra" 部分找不到任何文档。这意味着字节是从右到左蓝绿红阿尔法,右。它们必须是 uint32 还是其他。问题是我正在绘制浮点数的颜色图,所以我试图将它们转换为灰度,但我得到了很多奇怪的模式,这让我坚信我做错了什么。我编写了这个函数来在一个范围内从浮点数转换为 uint32。但结果出乎我的意料,我是在做一些非常愚蠢的事情吗?

def grayscale(x, min, max):
  return np.uint32((x-min)/(max-min)*0xffffff)
4

1 回答 1

5

我认为您对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()
于 2011-08-12T19:07:33.337 回答