美文网首页
cv2.error: OpenCV(4.6.0) :-1: er

cv2.error: OpenCV(4.6.0) :-1: er

作者: 小黄不头秃 | 来源:发表于2022-11-06 00:52 被阅读0次

    我的代码:

    cap = cv2.VideoCapture("1.mp4") # 获取视频对象
    w = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # 注意这里必须是整型
    h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    
    out = cv2.VideoWriter('./2.mp4', cv2.VideoWriter_fourcc("M","P","4","V"), 30.0,(w,h),True)
    while True:
        ret, img = cap.read()
        if ret:
            out.write(img)
        else:
            break
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    
    

    报错内容:

    cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'VideoWriter'
    Overload resolution failed:

    • Can't parse 'frameSize'. Sequence item with index 0 has a wrong type
    • VideoWriter() missing required argument 'frameSize' (pos 5)
    • VideoWriter() missing required argument 'params' (pos 5)
    • VideoWriter() missing required argument 'frameSize' (pos 5)

    或者是:

    cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'VideoWriter'
    Overload resolution failed:

    • Can't parse 'frameSize'. Sequence item with index 0 has a wrong type
    • Argument 'fourcc' is required to be an integer
    • Can't parse 'frameSize'. Sequence item with index 0 has a wrong type
    • VideoWriter() missing required argument 'params' (pos 6)

    问题:
    因为帧的宽度和长度必须是整型的。而我们使用CAP_PROP_FRAME_WIDTH获取的值得类型是float。

    解决办法:
    将float类型转换为int整型。
    例如:

    cap = cv2.VideoCapture("1.mp4") # 获取视频对象
    w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # 注意这里必须是整型
    h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    
    out = cv2.VideoWriter('./2.mp4', cv2.VideoWriter_fourcc("M","P","4","V"), 30.0,(w,h),True)
    while True:
        ret, img = cap.read()
        if ret:
            out.write(img)
        else:
            break
    cap.release()
    out.release()
    cv2.destroyAllWindows()
    

    相关文章

      网友评论

          本文标题:cv2.error: OpenCV(4.6.0) :-1: er

          本文链接:https://www.haomeiwen.com/subject/grrmtdtx.html