opencv笔记-1

演示代码:

import cv2
import pylab
import matplotlib.pyplot as plt
import numpy
#导入对应的包
cap = cv2.VideoCapture(0)#取得对应的摄像头,里面的数字表示不同的设备
fourcc = cv2.VideoWriter_fourcc(*'mp4v')#采用mp4方式才存储,为"m","p","4","v"
vw = cv2.VideoWriter('output.mp4', fourcc, 30, (1920, 1080))# outmp4是文件名字,fourcc是对应方式,30是帧速,1920 1080是分辨率
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print('can not recive frame,Exiting')
        break
    vw.write(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
cap = cv2.VideoCapture('F://python//movies//1.mp4')     # 读取视频
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))-100, int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print(fps, size)
while cap.isOpened():               # 当视频被打开时:
    ret, frame = cap.read()         # 读取视频,读取到的某一帧存储到frame,若是读取成功,ret为True,反之为False
    if ret:                         # 若是读取成功
        cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
        cv2.resizeWindow('frame', size)
        cv2.imshow('frame', frame)  # 显示读取到的这一帧画面
        key = cv2.waitKey(fps)       # 等待一段时间,并且检测键盘输入
        if key == ord('q'):         # 若是键盘输入'q',则退出,释放视频
            cap.release()           # 释放视频
            break
    else:
        cap.release()
cv2.destroyAllWindows()             # 关闭所有窗口
cv2.namedWindow('video', cv2.WINDOW_NORMAL)
cv2.resizeWindow('video', 500, 500)
cap = cv2.VideoCapture('F://python//movies//1.mp4')
fps = int(cap.get(cv2.CAP_PROP_FPS))
size = (int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)))
print('帧速率:', fps)# 帧速率: 30
print('大小:', size) # 大小: (512, 512)
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    cv2.imshow('video', frame)
    key = cv2.waitKey(0)
    if key == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
img = cv2.imread(r'1.jpg')
while True:
    cv2.imshow('img', img)
    key = cv2.waitKey(0)
    if key == ord('q'):
        break
    elif key == ord('s'):
        cv2.imwrite('./123.png', img)
cv2.destroyAllWindows()
cv2.namedWindow("new", cv2.WINDOW_NORMAL)
cv2.resizeWindow('new', 1920, 1080)
cv2.imshow('new', 0)
key = cv2.waitKey(0)
while key != ord('q'):
    key = cv2.waitKey(0)
if key == ord('q'):
    print(123)
    cv2.destroyAllWindows()

imageimage

posted @ 2022-09-02 17:43  云鲸啊  阅读(27)  评论(0)    收藏  举报