open cv学习 (十一)视频处理

视频处理

demo1
import cv2
# 打开笔记本内置摄像头
capture = cv2.VideoCapture(0)
# 笔记本内置摄像头被打开
while capture.isOpened():
    # 从摄像头中实时读取视频
    retval, image = capture.read()
    # 在窗口中实时显示读取到的视频
    cv2.imshow("Video", image)
    # 等到用户按下键盘的时间为1ms
    key = cv2.waitKey(1)
    # 如果用户按下空格键
    if key == 32:
        break


capture.release()
cv2.destroyAllWindows()
demo2
import cv2
# 打开笔记本内置摄像头
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# 笔记本内置摄像头被打开
while capture.isOpened():
    # 从摄像头中实时读取视频
    retval, image = capture.read()
    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 在窗口中实时显示读取到的视频
    cv2.imshow("Video", image_gray)
    # 等到用户按下键盘的时间为1ms
    key = cv2.waitKey(1)
    # 如果用户按下空格键
    if key == 32:
        break


capture.release()
cv2.destroyAllWindows()
demo3
import cv2
# 打开笔记本内置摄像头
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# 笔记本内置摄像头被打开
while capture.isOpened():
    # 从摄像头中实时读取视频
    retval, frame = capture.read()
    # 在窗口中实时显示读取到的视频
    cv2.imshow("Video", frame)
    # 等到用户按下键盘的时间为1ms
    key = cv2.waitKey(1)
    # 如果用户按下空格键
    if key == 32:
        capture.release()
        cv2.destroyWindow("Video")
        cv2.imwrite("./copy.png", frame)
        cv2.imshow("img", frame)
        break
cv2.destroyAllWindows()
demo4
import cv2
# 打开视频文件
video = cv2.VideoCapture("公司宣传.avi")

while video.isOpened():
    # 读取视频
    retval, image = video.read()
    # 设置窗口
    cv2.namedWindow("Video", 0)
    cv2.resizeWindow("Video", 432, 300)

    if retval == True:
        cv2.imshow("Video", image)
    else:
        break
    key = cv2.waitKey(1)
    if key == 27:
        break
video.release()
cv2.destroyAllWindows()
demo5
import cv2
# 打开视频文件
video = cv2.VideoCapture("公司宣传.avi")

while video.isOpened():
    # 读取视频
    retval, image = video.read()
    # 设置窗口
    cv2.namedWindow("Gray", 0)
    cv2.resizeWindow("Gray", 432, 300)

    if retval == True:
        img_Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        cv2.imshow("Gray", img_Gray)
    else:
        break
    key = cv2.waitKey(1)
    if key == 27:
        break
video.release()
cv2.destroyAllWindows()
demo6
import cv2
# 打开视频文件
video = cv2.VideoCapture("公司宣传.avi")

while video.isOpened():
    # 读取视频
    retval, image = video.read()
    # 设置窗口
    cv2.namedWindow("Video", 0)
    cv2.resizeWindow("Video", 420, 300)

    if retval == True:
        cv2.imshow("Video", image)
    else:
        break
    key = cv2.waitKey(50)
    if key == 32:
        cv2.waitKey(0)
        continue
    if key == 27:
        break

video.release()
cv2.destroyAllWindows()
demo7
import cv2
# 打开视频文件
video = cv2.VideoCapture("公司宣传.avi")
fps = video.get(cv2.CAP_PROP_FPS)
# 获取视频文件的帧数
frame_Count = video.get(cv2.CAP_PROP_FRAME_COUNT)
# 获取视频文件的帧宽度
frame_Width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
# 获取视频文件的帧高度
frame_Height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("帧速率:", fps)
print("帧数:", frame_Count)
print("帧宽度:", frame_Width)
print("帧高度:", frame_Height)
demo8
import cv2
# 打开视频文件
video = cv2.VideoCapture("output.avi")
fps = video.get(cv2.CAP_PROP_FPS)
frame_Num = 1
while video.isOpened():
    # 读取视频
    retval, frame = video.read()
    # 设置窗口
    cv2.namedWindow("Video", 0)
    cv2.resizeWindow("Video", 420, 300)
    if retval == True:
        cv2.putText(frame, "frame:" + str(frame_Num), (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
        cv2.putText(frame, "second:" + str(round(frame_Num/fps, 2)) + "s", (0, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
        cv2.imshow("Video", frame)
    else:
        break

    key = cv2.waitKey(50)
    frame_Num+=1
    if key == 27:
        break

video.release()
cv2.destroyAllWindows()
demo9
import cv2
# 打开笔记本内置摄像头
capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# 确定视频被保存的编码格式
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')

# 创建VideoWriter类对象
output = cv2.VideoWriter("output.avi", fourcc, 20, (640, 480))

while capture.isOpened():
    retval, frame = capture.read()
    if retval == True:
        output.write(frame)
        cv2.imshow("frame", frame)
    key = cv2.waitKey(1)
    if key == 27:
        break
capture.release()
output.release()
cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

计算机小混子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值