Mr.X
Mr.X

Reputation: 13

cannot save video in opencv 3

I am trying to apply Canny filter on a video to generate a video with canny filter in output.mp4 file. I found that the output file is 1kilobyte size although the origional video size is 30 Megabyte

this is the code which i used:

import numpy as np
import cv2
Video = cv2.VideoCapture('video.mp4')
width = int(Video.get(cv2.CAP_PROP_FRAME_WIDTH) + 0.5)
height = int(Video.get(cv2.CAP_PROP_FRAME_HEIGHT) + 0.5)
fourcc = cv2.VideoWriter_fourcc(*'mp4v') 
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (width, height))
while(Video.isOpened()):
ret, frame = Video.read()
if ret==True:
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 100, 100)
    cv2.imshow('frame',edges)
    out.write(edges)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Video.release()
out.release()
cv2.destroyAllWindows()

Upvotes: 1

Views: 1383

Answers (1)

Arash Rohani
Arash Rohani

Reputation: 1014

Because you're writing a gray image you have to pass False for isColor value of VideoWriter.
Just change your out = line to this:

out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (width, height), False)

Also why are you adding 0.5 to width and height when you are going to put int before them anyway!? Note that VideoWriter has to have the same w and h as the frame you're going to put through it, so if you change the w and h without resizing the frame you're writing, VideoWriter won't work (although in your example that 0.5 isn't doing anything because int will just floor it).

Upvotes: 2

Related Questions