Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Problem with writing video

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.

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()

Problem with writing video

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]]
    cv2.imshow('res',res)
    cv2.waitKey(1)
    size = (width ,height)

    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('output.avi', cv2.VideoWriter('output2.avi', fourcc, 24.0, (640,480))
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()