如何用Python中更新的numpy数组制作视频 [英] How to make video from an updating numpy array in Python

查看:375
本文介绍了如何用Python中更新的numpy数组制作视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,可修改类型为float的2D numpy数组water_depth的元素.该阵列包含每个像素的水深,范围通常在0到1.5m之间.我想用这个变化的数组制作视频:每个迭代都可以是视频中的一帧.我只发现此链接解释了类似的问题,并建议使用cv2 VideoWriter.问题是我的numpy数组是一个浮点数,而不是整数.这是否意味着我需要在每次迭代中对数组进行某种预处理?

I have a loop that modifies elements of a 2D numpy array water_depth with type float. The array contains water depth for each pixel and the range is usually between 0 to 1.5m. I would like to make a video out of this changing array: each iteration can be a frame in the video. I only found this link explaining a similar question and suggests using cv2 VideoWriter. The problem is that my numpy array is a float, not integer. Does this mean that I need to do some sort of pre-processing on my array in each iteration?

import numpy as np

water_depth = np.zeros((500,700), dtype=float)

for i in range(1000):
    random_locations = np.random.random_integers(200,450, size=(200, 2))
    for item in random_locations:
        water_depth[item[0], item[1]] += 0.1
        #add this array to the video

推荐答案

请注意,使用OpenCV进行视频I/O有时会很棘手.该库并不是围绕支持这些类型的操作而构建的,它们只是作为一个不错的东西而包含在内.通常,OpenCV将基于ffmpeg支持而构建,根据您的系统,您是否具有与其他人相同的编解码器来读取/写入视频,在某种程度上是任意的.如此说来,这里是一个示例,您可以确切了解可以进行的预处理:

Note that working with OpenCV for video I/O purposes can sometimes be tricky. The library isn't built around supporting these kinds of operations, they're just included as a nicety. Typically, OpenCV will be built off ffmpeg support, and whether or not you have the same codecs to read/write videos as another person is somewhat arbitrary depending on your system. With that said, here's an example so you get the idea of exactly what preprocessing you might do:

import numpy as np
import cv2

# initialize water image
height = 500
width = 700
water_depth = np.zeros((height, width), dtype=float)

# initialize video writer
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
fps = 30
video_filename = 'output.avi'
out = cv2.VideoWriter(video_filename, fourcc, fps, (width, height))

# new frame after each addition of water
for i in range(10):
    random_locations = np.random.random_integers(200,450, size=(200, 2))
    for item in random_locations:
        water_depth[item[0], item[1]] += 0.1
        #add this array to the video
        gray = cv2.normalize(water_depth, None, 255, 0, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
        gray_3c = cv2.merge([gray, gray, gray])
        out.write(gray_3c)

# close out the video writer
out.release()

请注意,我将迭代次数更改为10次而不是1000次,只是为了确保它可以工作.在这里normalize(..., 255, 0, ...)缩放图像,使最大值为255(白色),最小值为0(黑色).这意味着,一开始您所有的随机点开始点缀所有东西时,它们就会变成白色.但是,一旦一个点落在另一个点上,那将是最亮的-是所有其他点的两倍,因此它们将立即变为灰色.如果这不是您想要的,则必须考虑是否具有图像的最大值,并假设图像不会改变亮度.

Note that I changed the number of iterations to 10 instead of 1000 just to make sure it worked. Here normalize(..., 255, 0, ...) scales your image so that the max value is 255 (white) and the min value is 0 (black). This means that when at first all your random points start dotting everything, they turn white. However once one point lands on top of another, that will be the brightest---twice as bright as all other points, so they'll immediately drop to be gray. If that's not what you want, you have to think if you'd have a maximum value that your image might be and assume that your images won't change brightness otherwise.

这篇关于如何用Python中更新的numpy数组制作视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆