一、图片的读取与显示
import cv2 img = cv2.imread('1111.png') cv2.imshow('aa',img) cv2.imwrite('1111-new.png',img) cv2.imwrite('11112.jpg',img) cv2.waitKey(0)
二、摄像头的读取与显示
import cv2 cv2.namedWindow('video',cv2.WINDOW_NORMAL) cv2.resizeWindow('video',640,480) cap = cv2.VideoCapture(0) while cap.isOpened(): ret, frame = cap.read() print(ret) cv2.imshow('video',frame) cv2.resizeWindow('video',640,480) key = cv2.waitKey(1) if (key & 0xFF == ord('q')): break cap.release() cv2.destroyAllWindows()
三、视频的保存
import cv2 cv2.namedWindow('videowrite',cv2.WINDOW_NORMAL) cap = cv2.VideoCapture(0) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) cc = cv2.VideoWriter_fourcc(*"mp4v") out = cv2.VideoWriter('123.mp4',cc,25,(width,height)) while True: ret, frame = cap.read() cv2.imshow('videowrite',frame) out.write(frame) key = cv2.waitKey(1) if (key & 0xFF == ord('q')): break cap.release() out.release() cv2.destroyAllWindows()
四、视频的文件读取
import cv2 cap = cv2.VideoCapture('1.mp4') cv2.namedWindow('showvideo',cv2.WINDOW_NORMAL) while cap.isOpened(): ret, frame = cap.read() if(ret == False): break cv2.imshow('showvideo', frame) key = cv2.waitKey(25) if(key & 0xFF == ord('q')): break cap.release() cv2.destroyAllWindows()
本文为看恩吧原创文章,转载无需和我联系,但请注明来自knsay.com