Ale*_*x S 6 python numpy mencoder matplotlib video-processing
所以,这应该是对这个帖子的评论,但它显然已经关闭了,所以就这样了.我已经用matplotlib和numpy和mencoder成功地玩了,正如这里所建议的那样.我已经采用了Voki Codder缓冲区到stdin解决方案,这大大加快了整个过程.问题是,我在命令的-format ="bgra"部分找不到任何文档.这意味着字节是从右到左的蓝绿色红色alpha,右边.他们必须是uint32,或其他什么.问题是我正在绘制花车的彩色图,所以我试图将它们转换为灰度,但是我得到了许多奇怪的模式让我坚信我做错了什么.我写了这个函数来从一个范围内的浮点数转换为uint32.但结果并不是我预期的原因,我做了一件非常愚蠢的事情吗?
def grayscale(x, min, max):
return np.uint32((x-min)/(max-min)*0xffffff)
Run Code Online (Sandbox Code Playgroud)
我觉得你对uint32代表什么感到困惑.它是4个uint8整数带.
如果您有浮点数据,并希望以灰度表示它,您不希望将其重新调整为完整的32位范围,您希望将其重新调整为8位范围,并重复该项为红色,绿色和蓝带(然后可能放在一个恒定的alpha波段).
您也可以使用不同的字节顺序.Y8只是一个灰度,8位带,Y16是一个灰度级16位带.(看一下mencoder -rawvideo format=help完整(虽然有些混乱)列表的输出.)
只是为了说明使用numpy查看32位整数作为4位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")
Run Code Online (Sandbox Code Playgroud)
但是,在你的情况下,你可能想要做更像这样的事情:
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()
Run Code Online (Sandbox Code Playgroud)