视频处理

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()