简体   繁体   中英

Merge individual frame to video file using Opencv

I am trying to stack a individual frame to a video file using Opencv. I want to combine two different code together to make the individual frame. Following code help me extract the individual frame,

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('file_data.mp4',fourcc,20 (1920,1080),False)
while True:
    ret, frame=cap.read()
    mask = object_detector.apply(frame)
    _, mask  = cv2.threshold(mask,254,255,cv2.THRESH_BINARY)       
    contours,_ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    res = cv2.bitwise_and(frame,frame,mask=mask)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area>1000:   
            #print("Area of contour:", area)
            cv2.drawContours(frame, [cnt], -1, (0,255,0),2)
            cv2.imwrite("file%d.jpg"%count, frame)
            out.write(frame)
    if cv2.waitKey(1) and 0xFF == ord('q'):
        break

I tried storing the individual frame in array, but it didn't work. It doesn't show any error, but pc crash.

fps = 20, ,width = 1920,height = 1080

Thanks to Rotem. The issue has been solved using the VideoWriter from Opencv. The working code is given below.

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('file_data.mp4',fourcc,20,(1920,1080), True)
while True:
ret, frame=cap.read()
if ret == True:
    # frame[frame<=thresholds]=0
    mask = object_detector.apply(frame)
    _, mask  = cv2.threshold(mask,254,255,cv2.THRESH_BINARY)       
    contours,_ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    res = cv2.bitwise_and(frame,frame,mask=mask)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area>1000:    
            cv2.drawContours(frame, [cnt], -1, (0,255,0),2)
            out.write(frame)
    break
if cv2.waitKey(20) == ord('q'):
    break

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM