2012-04-17 60 views
0

我想從python中的jpeg圖像構建視頻。cvWriteFrame拋出IplImage *:無法轉換爲Python中的CvMat錯誤

這是我的代碼。

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

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)