opencv中视频文件的读写

视频文件的读写操作:

import cv2 

videoCapture = cv2.VideoCapture('123'.avi)

fps = videoCapture.get(cv2.CAP_PROP_FPS)

size = int(int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
           int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))

videoWriter = cv2.VideoWriter('out.avi',cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)

success,frame = videoCapture.read()

while success:
    videoWriter.write(frame)
    success,frame = videoCapture.read()

捕获摄像头的帧:捕获10s 电脑摄像头视频

import cv2

cameraCapture = cv2.VideoCapture(0)

fps =30

size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
           int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))

videoWriter = cv2.VideoWriter('out.avi',cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)

success,frame = cameraCapture.read()
numFrameRemaining = 10 * fps -1
while success and numFrameRemaining > 0:
    videoWriter.write(frame)
    success,frame = cameraCapture.read()
    numFrameRemaining -= 1
""""
    一组摄像头或一个多头摄像头的使用方法
"""    
# success0 = cameraCapture0.grab()
# success1 = cameraCapture1.grab()
# if success0 and success1:
#     frame0 = cameraCapture0.retrieve()
#     frame1 = cameraCapture1.retrieve()


cameraCapture.release()

图片显示:

import cv2 
import numpy as np

img = cv2.imread('hcl.jpg')
cv2.imshow('hcl',img)
cv2.waitKey()
cv2.destroyAllWindows()

视频显示:

其实这里面代码是通过图片不断更新实现视频效果,可通过注释的sleep看到效果

import cv2 
import numpy as np
import time

clicked = False

def onMouse(event,x,y,flags,param):
    global clicked
    if event == cv2.EVENT_LBUTTONUP:
        clicked = True

cameraCapture = cv2.VideoCapture(0)
cv2.namedWindow('myWindow')
cv2.setMouseCallback('myWindow',onMouse)

print('showing camera feed.click window or press any key to stop')

success,frame = cameraCapture.read()
while success and cv2.waitKey(1) == -1 and not clicked:
    cv2.imshow('myWindow',frame)
    success,frame = cameraCapture.read()
    #time.sleep(1)
    
cv2.destroyWindow('myWindow')
cameraCapture.release()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值