当前位置:   article > 正文

图像处理之opencv保存视频图片_opencv保存视频的图片

opencv保存视频的图片

OpenCV: OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。

python安装opencv库: pip install opencv-python
python导入opencv库: impoort cv2

1、读取视频

  • 视频可以理解为快速展示的图片,我们在读取视频时得到的也是图片数据

1.1、从本地捕获视频

cap = cv2.VideoCapture('./test_video.mp4') #capture有捕获的意思
  • 1

1.2、展示图片

import matplotlib.pyplot as plt
flag,frame = cap.read()#frame有一桢的意思
print (flag)
plt.imshow(frame)
  • 1
  • 2
  • 3
  • 4

展示结果:
在这里插入图片描述

1.3、释放资源

  • 每次展示完图片后需要释放资源

2、展示整个视频

import cv2
cap = cv2.VideoCapture('./vod.mp4')
flag, frame = cap.read()

while True:
    if flag == False:
        break
    cv2.imshow('video', frame)
    if ord('q') == cv2.waitKey(10):#输入‘q’键结束,等待每秒显示10张图片
        break
    flag, frame = cap.read()
#资源释放
cv2.destroyAllWindows()
cap.release()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

3、保存视频图片

import cv2
cap = cv2.VideoCapture('./vod.mp4')
flag, frame = cap.read()

index = 1
while True:
    if flag == False:
        break
    cv2.imshow('video', frame)#以图片展示视频
    #将视频中的图片保存到本地
    cv2.imwrite('./images/%s.jpg'%index, frame)
    if ord('q') == cv2.waitKey(24):#输入‘q’键退出,每秒展示24张图片
        break
    flag, frame = cap.read()
    index += 1
#资源释放
cv2.destroyAllWindows()
cap.release()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这里插入图片描述

4、把视频变为黑白色图片

import cv2
cap = cv2.VideoCapture('./vod.mp4')

flag, frame = cap.read()
index = 1
while True:
    if flag == False:
        break
    gray = cv2.cvtColor(frame, code=cv2.COLOR_BGR2GRAY)
    cv2.imshow('video', gray)#以图片展示视频
    #将视频中的图片保存到本地
    cv2.imwrite('./images/%s.jpg'%index, gray)
    if ord('q') == cv2.waitKey(24):#输入‘q’键退出,每秒展示24张图片
        break
    flag, frame = cap.read()
    index += 1
#资源释放
cv2.destroyAllWindows()
cap.release()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/268904
推荐阅读
相关标签
  

闽ICP备14008679号