Problem with writing video [closed]

asked 2020-05-16 19:27:02 -0600

hernancrespo gravatar image

updated 2020-05-17 08:23:31 -0600

Hi, I try to crop a polygonal from video and write it. My cropping operation is successful. result

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.

EDIT: Solution is

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]]

    height2, width2 = res.shape[:2]

    img_array.append(res)

cap.release()

cv2.destroyAllWindows()

#out = cv2.VideoWriter('hwyeni.mp4',cv2.VideoWriter_fourcc(-), 24, size)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output2.avi', fourcc, 30.0, (800,480))
for i in range(len(img_array)):
    if img_array[i] is None:
       break
    out.write(img_array[i])

out.release()
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-26 14:10:03.038208

Comments

you're trying to write a 640x480 video, but your cropped images have a different (arbitrary) size ?

please check the return value from out.write() (python users never do....)

berak gravatar imageberak ( 2020-05-16 21:10:51 -0600 )edit

Thanks a lot!!

hernancrespo gravatar imagehernancrespo ( 2020-05-17 08:23:37 -0600 )edit