首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用numpy和mencoder直接绘制到电影

使用numpy和mencoder直接绘制到电影
EN

Stack Overflow用户
提问于 2011-08-12 23:12:24
回答 1查看 937关注 0票数 6

所以,这应该是对this thread的评论,但它显然是关闭的,所以它是这样的。我已经很成功地使用matplotlib、numpy和mencoder,正如这里所建议的那样。自那以后,我采用了Voki Codder buffer to stdin solution,这大大加快了整个过程。问题是,我找不到关于该命令的-format="bgra“部分的任何文档。这意味着字节从右到左是蓝色、绿色、红色、alpha、右侧。他们必须是uint32,还是别的什么。问题是我正在绘制浮点数的彩色地图,所以我试图将它们转换为灰度,但我得到了许多奇怪的图案,这让我强烈地认为我做错了什么。我写这个函数是为了在一个范围内从浮点数转换成uint32。但是结果并不是我想要的,我是不是做了什么非常愚蠢的事情?

代码语言:javascript
复制
def grayscale(x, min, max):
  return np.uint32((x-min)/(max-min)*0xffffff)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-13 03:07:33

我认为您对uint32所代表的内容感到困惑。它是4个uint8整数的范围。

如果你有浮点数据,并且想要用灰度表示它,你不想把它重新缩放到一个完整的32位范围,你想把它重新缩放到一个8位的范围,然后对红色、绿色和蓝色带重复这一步(然后可能放在一个恒定的alpha带中)。

你也可以使用不同的字节顺序。Y8只是一个8位灰度波段,而Y16是一个16位灰度波段。(请查看mencoder -rawvideo format=help的输出,以获得完整的(尽管有些令人困惑)清单。)

仅为说明如何使用numpy将32位整数视为四个8位整数带:

代码语言:javascript
复制
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")

然而,在您的例子中,您可能想要做更多这样的事情:

代码语言:javascript
复制
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()
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7042190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档