Hi, I try to crop a polygonal from video and write it. My cropping operation is successful.
But when I try to write it, there is no any message on console, so I think it writes successfully. But as output, I just see a 55kb .avi file. When I click it nothing happens. Where is my mistake? I had written video in past with this format.
import cv2
import numpy as np
import glob
img_array = []
cap = cv2.VideoCapture('M30.mp4')
while cap.isOpened():
ret,image = cap.read()
if image is None:
break
height, width = image.shape[:2]
mask = np.zeros((height, width), dtype=np.uint8)
points = np.array([[[305,80],[-100,493],[1123,513],[897,80],[700,80],[613,80]]])
cv2.fillPoly(mask, points, (255))
res = cv2.bitwise_and(image,image,mask = mask)
rect = cv2.boundingRect(points) # returns (x,y,w,h) of the rect
cropped = res[rect[1]: rect[1] + rect[3], rect[0]: rect[0] + rect[2]]
cv2.imshow('res',res)
cv2.waitKey(1)
size = (width ,height)
img_array.append(res)
#out = cv2.VideoWriter('hwyeni.mp4',cv2.VideoWriter_fourcc(-), 24, size)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 24.0, (640,480))
for i in range(len(img_array)):
out.write(img_array[i])
out.release()