0

我正在尝试从 python 中的 jpeg 图像构建视频。

这是我的代码。

    codec = highgui.CV_FOURCC('F', 'L', 'V', '1')
    fps = 25
    colored = 1
    size = (float(width), float(height))
    video_writer = highgui.cvCreateVideoWriter(
                'out.mpg', codec, fps, cv.cvSize(1440, 553), True)


    pictures = os.listdir(folder)
    for picture in pictures:
        picture = '%s/%s' % (folder, picture)
        highgui.cvWriteFrame(self.video_writer, picture)

高度、宽度和文件夹已在其他地方定义。当我运行代码时,我收到以下错误:

Output #0, mpeg, to 'out.mpg':
    Stream #0.0: Video: flv, yuv420p, 1440x553, q=2-31, 50964 kb/s, 90k tbn, 25 tbc
[mpeg @ 0x29425c0] VBV buffer size not set, muxing may fail
Traceback (most recent call last):
  File "./ips.py", line 109, in <module>
    main()
  File "./ips.py", line 62, in main
    video.build(configs['maps']['folder'])
  File "/home/martin/Formspring/maraca-locator-94947aa/encoder.py", line 34, in build
    highgui.cvWriteFrame(self.video_writer, picture)
TypeError: %%typemap(in) IplImage * : could not convert to CvMat

我不知道我做错了什么,知道是什么原因造成的吗?有没有首选的构建框架的方法?

4

1 回答 1

1

cvWriteFrame()需要一个有效的IplImage*,而你似乎没有。

考虑以下代码:

pictures = os.listdir(folder)
    for picture in pictures:
        picture = '%s/%s' % (folder, picture)
        highgui.cvWriteFrame(self.video_writer, picture)

listdir()返回一个列表,其中包含由 . 给定的目录中的条目名称folder。之后,您声明picture要在此列表上进行迭代,因此它只存储一个条目。这就是您感到困惑的地方:此时,picture只是一个 string

调用前cvWriteFrame()需要先从磁盘加载图片数据并查看是否加载成功:

pictures = os.listdir(folder)
    for picture in pictures:
        picture = '%s/%s' % (folder, picture)
        img = cv.LoadImage(picture)
        if not img:
            print "!!! Could not load image " + picture 
            break

        highgui.cvWriteFrame(self.video_writer, img)
于 2012-04-17T13:05:05.507 回答