首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >生成运动2D正弦图案

生成运动2D正弦图案
EN

Stack Overflow用户
提问于 2019-02-20 15:13:21
回答 1查看 1.3K关注 0票数 3

我正在尝试生成一个模式的视频,如下所示,它在python中使用OpenCV水平移动。

我用下面的方式写了它。生成的视频文件没有任何错误,但该文件不能在任何视频播放器中打开

代码语言:javascript
运行
复制
        import cv2
        import numpy as np
        from cv2 import VideoWriter, VideoWriter_fourcc

        video = VideoWriter('_sine_pattern_gen_'+str(60)+'_fps.avi', VideoWriter_fourcc(*'MP42'), 60, (346, 260))

        x = np.arange(346)  # generate 1-D sine wave of required period 
        y = np.sin(2 * np.pi * x / 20) 

        y += max(y) # offset sine wave by the max value to go out of negative range of sine 

        frame = np.array([[y[j] for j in range(346)] for i in range(260)], dtype='uint8') # create 2-D array of sine-wave

        for _ in range(0, 346):
            video.write(frame)
            shifted_frame =  np.roll(frame, 2, axis=1) # roll the columns of the sine wave to get moving effect
            frame = shifted_frame 

        cv2.destroyAllWindows()
        video.release()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-20 22:38:24

VideoWriter需要彩色图像时,这是使用单通道灰度图像时的问题。这可以通过使用标志isColor=False来修复。

此外,由于图像类型为uint8,而y仅为2,因此这将看起来像黑色视频,而不是您显示的图像。通过乘以y[j]*127,可以将y缩放到0-255的整个范围。下面的代码应该可以工作:

代码语言:javascript
运行
复制
import cv2
import numpy as np
from cv2 import VideoWriter, VideoWriter_fourcc

fname = '_sine_pattern_gen_'+str(60)+'_fps.avi'
video = VideoWriter(fname, VideoWriter_fourcc(*'MP42'), 60, (346, 260), isColor=False)

x = np.arange(346)  # generate 1-D sine wave of required period 
y = np.sin(2 * np.pi * x / 20)

y += max(y) # offset sine wave by the max value to go out of negative range of sine 

frame = np.array([[y[j]*127 for j in range(346)] for i in range(260)], dtype=np.uint8) # create 2-D array of sine-wave

for _ in range(0, 346):
    video.write(frame)
    shifted_frame =  np.roll(frame, 2, axis=1) # roll the columns of the sine wave to get moving effect
    frame = shifted_frame 

video.release()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54780756

复制
相关文章

相似问题

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