cvWriteFrame抛出IplImage *:无法在python中转换为CvMat错误

时间:2012-04-17 00:00:11

标签: python opencv

我正在尝试使用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

我不知道我做错了什么,知道造成这种情况的原因是什么?有没有一种建立框架的首选方法?

1 个答案:

答案 0 :(得分: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只是一个字符串

在调用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)