OpenCV教程3

读取摄像头视频内容

# -*- conding = utf-8 -*-
# @Time: 2021/3/26 20:22
# @Author: tdl
# @file: test10.py
# @sortware: PyCharm

import cv2
import numpy as np

# 视频导入
# cap=cv2.VideoCapture('out.mp4')
#摄像头
cap=cv2.VideoCapture(0)

while(True):
    ret,frame=cap.read()
    if ret!=True:
        break
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  #将BGR改为RGB
    cv2.imshow('frame',frame)
    # print(cap.get(5))#帧速
    # print(cap.get(3))#帧的宽
    # print(cap.get(4))#帧的高
    if cv2.waitKey(1) & 0xff == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

写入视频

# -*- conding = utf-8 -*-
# @Time: 2021/3/27 13:03
# @Author: tdl
# @file: test11.py
# @sortware: PyCharm

import cv2
import numpy as np

#显示中文
def zh_ch(string):
	return string.encode("gbk").decode(errors="ignore")

cap=cv2.VideoCapture('out.mp4')
fps = cap.get(5)
width = cap.get(3)
height = cap.get(4)

# cap=cv2.VideoCapture(0)
# DIVX MJPG X264等编码格式
fourcc=cv2.VideoWriter_fourcc(*'MJPG')
out=cv2.VideoWriter('1.avi',fourcc,fps,(int(width),int(height)))
while (True):
    ret,frame=cap.read()
    src = cv2.resize(frame, (int(width // 2), int(height // 2)), interpolation=cv2.INTER_CUBIC)#窗口大小
    if ret==True:
        frame=cv2.flip(frame,1) #水平翻转
        out.write(frame)#写入视频
        cv2.imshow(zh_ch('frame'),src)
    else:
        break
    if cv2.waitKey(1)&0xff==ord('q'):
        break

out.release()
cap.release()
cv2.destroyAllWindows()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值