1、保存摄像头读取的视频
import cv2
cap = cv2.VideoCapture('video/1.mp4')
if cap.isOpened() is False:
print('Camera Error!')
frame_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*"XVID")
output_gray = cv2.VideoWriter('video/video_output.mp4', fourcc, int(fps), (int(frame_width), int(frame_height)), False)
while cap.isOpened():
ret, frame = cap.read()
if ret is True:
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
output_gray.write(gray_frame)
cv2.imshow('gray', gray_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
output_gray.release()
cv2.destroyAllWindows()
2、读取图片
import cv2
import matplotlib.pyplot as plt
img_path = 'image/liudehua.jpg'
img = cv2.imread(img_path)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
3、