1

Problem:

I'm having issues correctly rendering/playing a video created by OpenCV's videowriter.

Details:

Python does not spit out any errors and sucessfully runs through/creates the video. When I try to play the video with VLC, it looks like VLC is trying to play an empty video (it looks like it has time length 00:00). Furthermore, the video size is a mere 5kbs. I've used this function with other NumPy image arrays, and they have worked.

My NumPy Array Preparation Code:

for i in range(len(frames)-1):
    fig = PIV.runPIV(frames[i].astype(np.int32),frames[i+1].astype(np.int32),dt = self.context.attrs.dt)
    agg = fig.canvas.switch_backends(FigureCanvasAgg)
    agg.draw()  #This produces a successful image. Simple Quiver plot.

    image = np.fromstring(agg.tostring_rgb(),dtype = np.uint8)
    image.shape = agg.get_width_height() + (3,)
    images.append(image)
self._render(images)

Images Array:

[[[191 191 191]
  [191 191 191]
  [191 191 191]
  ...,

  [191 191 191]
  [191 191 191]
  [191 191 191]]]

Note:

This array has a shape of (640,480,3) which was what other arrays previously that worked similarly had. The only difference between this and other past successes is that before I loaded images and did processing on them before rendering, but this stack is from matplotlib.pyplot with a switched backend to FigureCanvasAgg; however, when I save the image prior to its NumPy conversion, the image looks perfectly fine.

My OpenCV VideoWriter Code:

def writeMovie(self, fileName, fps=20):
    isColor = len(self[0].shape) == 3
    writer = cv2.VideoWriter(fileName, 
                             fps=fps,
                             fourcc=cv2.cv.CV_FOURCC(*"PIM1"),
                             frameSize=self[0].shape[0:2], 
                             isColor=isColor)
    for image in self:
        if image.dtype == np.bool:
            image = image.astype(np.uint8)*255
        writer.write(image)

I will gladly provide more information to the best of my abilities to clarify and help answer this question. Any advice/suggestions will be greatly appreciated.

4

2 回答 2

0

When the video seems to be empty, that's because the images you want to put into the video don't fit the video parameters. I had an equivalent problem in C++ and my problem was the input size and the codec.

I'm a neophyte in python, but I think you might have the same problem. Maybe you should try to print frameSize and fourcc to see if theses parameters are correct ( shouldn't it be self[0].shape[0:1] ? (Again, I don't know much about python, so I might be wrong on this point))

于 2013-08-04T09:31:03.580 回答
0

Not sure what the problem is still, but the way I got around it was saving each image as a file and then concatenating all the files with ffmpeg.

于 2013-08-19T00:00:01.503 回答