三、opencv-python 图片读取、显示、视频读取、录制、保存

一、图片的读取与显示

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



打赏

看恩吧
网站不承担任何有关评论的责任
  • 最新评论
  • 总共条评论
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦