1

I am using this function (inside a class) to save an array of images into a video. Despite the program running with no errors, the video I got, in the end, is not what was expected. It has a stripy effect even though when I print the images individually everything is ok.

def saveVideo(self):
        import cv2
        out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), self.fps, (160, 122), 0)
        for i in range(0, len(self.images)):
            out.write(self.images[i])
        out.release()

The video I get

Update: What happens is that the image in the video looks like the original image stretched, flipped, and with the black bars in the middle. There must be a problem with the video shape but I cannot manage to solve it.

5
  • Can you print self.images[i].shape?
    – Micka
    Commented Nov 22, 2021 at 14:59
  • @Micka it is (122, 160)
    – Raquel
    Commented Nov 22, 2021 at 15:46
  • If I switch the 160 with the 122 in the VideoWriter then the video file is corrupted and I cannot open it.
    – Raquel
    Commented Nov 22, 2021 at 15:47
  • not enough code to reproduce. please review minimal reproducible example. Commented Nov 22, 2021 at 18:28
  • How many channels does your image have?
    – Micka
    Commented Nov 22, 2021 at 22:11

2 Answers 2

2

Have you tried casting the images to uint8? Try:

np.uint8(self.images[i])

0
0

I'd say try

fourcc = cv2.VideoWriter_fourcc(*'XVID')

instead of

cv2.VideoWriter_fourcc(*'mp4v')

so like this

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter("output.mp4", fourcc, self.fps, (160, 122), 0)
5
  • It didn't work :/
    – Raquel
    Commented Nov 22, 2021 at 15:45
  • Damn. How are you calling self.fps? Is fps in function important to the project? You could try calling it with integers in cv2.VideoWriter(). Like: out = cv2.VideoWriter("output.mp4", fourcc, 60.0, (160, 122), 0)
    – MaxSSD
    Commented Nov 22, 2021 at 16:47
  • tried that and did not work either. I really don't understand why
    – Raquel
    Commented Nov 22, 2021 at 17:07
  • I'm out of options than :( Unless you comment the whole code haha
    – MaxSSD
    Commented Nov 22, 2021 at 17:18
  • I can't share the whole code but I realized I have the same problem if I use random data: import cv2 data = np.random.randint(0, 255, size=[1000, 122, 160]) out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 8, (160, 122), 0) for i in range(0, len(data)): out.write(data[i]) out.release()
    – Raquel
    Commented Nov 22, 2021 at 17:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.