Martin
Martin

Reputation: 905

cvWriteFrame throws IplImage * : could not convert to CvMat error in python

I am trying to build a video from jpeg images in python.

This is the code I have.

    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)

with height, width, and folder already defined somewhere else. When I run the code, I get the following error:

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

I have no idea what I am doing wrong, any idea what is causing this ? Is there a preferred way to build a frame ?

Upvotes: 0

Views: 696

Answers (1)

karlphillip
karlphillip

Reputation: 93468

cvWriteFrame() needs a valid IplImage*, and you doesn't seem to have one.

Consider the following code:

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

listdir() returns a list containing the names of the entries in the directory given by folder. After that, you declare picture to iterate on this list, so it stores only one entry. This is where you are getting confused: at this point, picture is just a string.

Before calling cvWriteFrame() you need to load the image data from the disk and check if it was loaded successfully:

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)

Upvotes: 1

Related Questions