简体   繁体   English

使用过滤器在 OpenCV 中保存视频

[英]Saving Video in OpenCV with filters

I am trying to save a video using open CV, the idea is to alternate the video frames from colored to gray scale for few seconds.我正在尝试使用开放的 CV 保存视频,想法是将视频帧从彩色到灰度交替几秒钟。 When I do the saving, the video saves the colored frames only.当我进行保存时,视频仅保存彩色帧。

import cv2

def saving(cap):
    width=cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height=cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    fps= cap.get(cv2.CAP_PROP_FPS)
    fourcc=cv2.VideoWriter_fourcc(*'XVID')
    out=cv2.VideoWriter('Project11.mp4',fourcc,fps,(int(width),int(height)))
    return (out)


def first4seconds(video):
    c=saving(video)
    while(video.isOpened()):
        print(video.get(cv2.CAP_PROP_POS_MSEC))
        ret, frame = video.read()
        if ret==True:
            if cv2.waitKey(25) & 0xFF == ord('q'):
                    break
        else:
            break

    if(500<=int(video.get(cv2.CAP_PROP_POS_MSEC))<1000 or 2000<=int(video.get(cv2.CAP_PROP_POS_MSEC))<3000):
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            c.write(gray)
            cv2.imshow('frame',gray)
    else:
        c.write(frame)
        cv2.imshow('frame',frame)


video.release()
cv2.destroyAllWindows()



cap = cv2.VideoCapture('test.mp4')
first4seconds(cap)
if(500<=int(video.get(cv2.CAP_PROP_POS_MSEC))<1000 or 2000<=int(video.get(cv2.CAP_PROP_POS_MSEC))<3000):
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            **gray_2=cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)** 
            **cv2.imshow('frame',gray_2)**
            **c.write(gray_2)**
else:
    c.write(frame)
    cv2.imshow('frame',frame)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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