I'm currently struggling with opencv in python 2.7
The aim of the program I'm trying to write is to open a source video with VideoCapture (is a mp4 with H264 codec), apply some filters (background removal, object tracking, stuff like that), display the result in a frame with imshow function, and save the result as another output video.
The output frame is shown as it should, but the problem is that the output saved is an empty .avi (or mp4, or whatever I put as argument of VideoWriter function). Just a nutshell of few Bytes .
So far you could just answer that I'm not using the appropriate combination of codec-format. But the odd thing is that if I change the VideoCapture to stream from webcam (so just changing VideoCaputure("source.mp4") to VideoCapture(webcamindex) and leaving every settings as they are), it works fine !
My program is something like that, I just omitted the functions
import cv2
import numpy as np
#VIDEO INPUT: SWITCHING THE TWO LINES BELOW IT WORKS!
#cap = cv2.VideoCapture(1)
cap = cv2.VideoCapture('input.mp4')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 30, (640,480), True)
while True:
ret, frame = cap.read()
#A BACKGROUND REMOVAL FUNCTION
maskedMotion = justMotion(frame)
#A FUNCTION THAT FINDS CIRCLE THROUGH THE HOUGHCIRCLE FUNCTION
circle = findBall(maskedMotion)
#DRAW THE CIRCLES
if circle!=None and circle.size != 0:
for i in circle[0,:]:
#draw the outer circle
cv2.circle(maskedMotion,(i[0],i[1]),i[2],(0,255,0),2)
#draw the center of the circle
cv2.circle(maskedMotion,(i[0],i[1]),2,(0,0,255),3)
out.write(maskedMotion)
cv2.imshow('Result', maskedMotion)
#ESCAPE SEQUENCE
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Any suggestions?
As suggested by shawshank in a comment, the problem was the resolution. I was trying to save a video output with a smaller resolution than the original source. So I just changed
out = cv2.VideoWriter('output.avi', fourcc, 30, (640,480), True)
for
out = cv2.VideoWriter('output.avi', fourcc, 30, (1920,1080), True)
Hope someone else will find this tip useful :D
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.