视频操作

时间:2013-05-31 13:34:46

标签: python opencv numpy video-processing

几天前,我从未使用OpenCV或进行任何视频处理。我被要求根据一些用户输入计算覆盖视频,并构建一个新的视频,其中包含以AVI格式下载的叠加层。从本质上讲,目标是使用一个表单作为输入3个图像(图标,屏幕截图#1,屏幕截图#1)和3个文本输入,并用它们覆盖原始视频。这是一个link to the video。当视频运行时,您会注意到iPhone开头的中心图标被拉伸和拉动。我一直在迭代地测试OpenCV方法,一帧一帧地分解视频并为每一个做一些事情,然后重建(显然这可能是使用OpenCV成功重建视频的唯一方法,但无论如何)。 this视频是我覆盖了一个彩色圆圈的视频,它可以来回移动。

# the method I've been using
import cv2 as cv
import numpy as np
cap = cv.VideoCapture('the_vid.avi')
flag, frame = cap.read()
width = np.size(frame,1)
height = np.size(frame,0)
writer = cv.VideoWriter('output.avi', cv.VideoWriter_fourcc('I','4','2','0'), fps=35, (width,height), 1)

while True:
    flag, frame = cap.read()
    if flag == 0:
        break
    x = width/2
    y = height/2
    # add a line or circle or something
                    origin radius
    cv.circle(frame, (x,y), 20, (0,0,255), -1)
    # write our new frame
    writer.write(frame)

现在我们得到了这个非常大的未压缩AVI文件的输出,可以使用ffmpeg压缩

ffmpeg -i output.avi -vcodec msmpeg4v2 compressed_output.avi

好的,这就是我用来重建这个视频的方法,从那个方法来看,我没有看到可以采取静态图像并像前90帧左右那样展开它。我看到的唯一另一种可能是做下面的事情。如果你能告诉我是否有办法实现这个非常棒的伪代码,我认为这将是非常困难的:

# example for the first image and first few seconds of video only

first_image = cv.imread('user_uploaded_icon.png')
flag, first_frame = cap.read()

# section of the frame that contains the original icon
the_section = algorithm_to_get_array_of_original_icon_in_first_frame(first_frame)
rows, cols = the_section.shape

# somehow find the array within the first image that is the same size as the_section
# containing JUST the icon
icon = array_of_icon(first_image)

# build a blank image with the size of the original icon in the current frame

blank_image = np.zeros((rows,cols,3),np.uint8)

for i in xrange(row):
    for j in xrange(col):
        blank_image[i,j] = icon[i,j]

看起来它可能不起作用的事实是first_frame中的the_section将被拉伸到与静态图像不同的维度......所以我不确定是否有任何可行的方法来处理它。我非常感谢您提前提供帮助。

0 个答案:

没有答案
相关问题