【机器视觉】OpenCV-Python 视频基本操作

简介: 【机器视觉】OpenCV-Python 视频基本操作
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
# 设置兼容中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
D:\Anaconda\AZWZ\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:
D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.NOIJJG62EMASZI6NYURL6JBKM4EVBGM7.gfortran-win_amd64.dll
D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll
  warnings.warn("loaded more than 1 DLL from .libs:\n%s" %

1.视频文件的读取

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I4ItiooG-1637755673389)(attachment:image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5s26ix61-1637755673391)(attachment:image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9lwfjjn0-1637755673394)(attachment:image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UTKrGdcP-1637755673395)(attachment:image.png)]

# 获取视频对象
cap = cv.VideoCapture('viedo/DOG.wmv')
# 判断是否获取成功
print(cap.isOpened())
True


while(cap.isOpened()):
    # 获取每一帧的图像
    ret,frame = cap.read()
    # 获取成功显示图像
    if ret == True:
        cv.imshow('frame',frame)
    # 每一帧间隔25ms 当视频播放完毕,且按下q键时,会弹出循环
    if cv.waitKey(25) & 0xFF == ord('q'):
        break
# 释放视频对象
cap.release()
cv.destroyAllWindows()

2.视频文件的保存

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cfAq4FYP-1637755673397)(attachment:image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cDP566f3-1637755673397)(attachment:image.png)]

# 读取视频
cap = cv.VideoCapture('viedo/DOG.wmv')
# 获取视频的宽和高
width = int(cap.get(3))
height = int(cap.get(4))
print("宽:",width,",高:",height)
宽: 444 ,高: 480


# 创建保存视频的对象,设置编码格式,帧率,图像的宽高等
out = cv.VideoWriter('viedo/outpy.avi',cv.VideoWriter_fourcc('M','J','P','G'),10,(width,height))
# 
while(True):
    ret,frame = cap.read()
    if ret==True:
        # 将每一帧图像写入一个文件中
        out.write(frame)
    else:
        # 读取完毕
        break
# 释放资源
cap.release()
out.release()
cv.destroyAllWindows()
print("保存成功!")
保存成功!

总结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y2og246l-1637755673399)(attachment:image.png)]

3.MeanShift实现视频追踪

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ojrfzjny-1637755673399)(attachment:image.png)]

# 获取视频
cap = cv.VideoCapture('viedo/DOG.wmv')
# 获取第一帧图像
ret,frame = cap.read()
# 指定第一帧图像上的目标位置 (行,高,列,宽)
r,h,c,w = 197,141,0,208
track_window = (c,r,w,h)
# 指定目标的感兴趣区域
roi = frame[r:r+h,c:c+w]
# 计算直方图
# 转换色彩空间(HSV)
hsv_roi = cv.cvtColor(roi,cv.COLOR_BGR2HSV)
# 计算直方图
roi_hist = cv.calcHist([hsv_roi],[0],None,[180],[0,180])
# 归一化 min_max归一化
cv.normalize(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)
array([[2.3043747e+00],
       [3.1423289e-01],
       [5.2372152e-01],
       [9.4269872e-01],
       [3.1423289e-01],
       [9.6888477e-01],
       [4.6611214e+00],
       [1.3878620e+00],
       [4.5563769e+00],
       [4.3730745e+00],
       [1.4507086e+01],
       [5.4205173e+01],
       [8.2669441e+01],
       [6.8764633e+01],
       [6.6434074e+01],
       [7.7222733e+01],
       [2.5500000e+02],
       [4.2971348e+01],
       [1.5790203e+01],
       [2.3043747e+00],
       [1.5711645e+00],
       [2.6186076e-01],
       [1.0474430e-01],
       [1.3093038e-01],
       [7.8558221e-02],
       [0.0000000e+00],
       [1.3093038e-01],
       [5.2372150e-02],
       [1.0474430e-01],
       [2.6186075e-02],
       [2.6186076e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.0948860e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.0948860e-01],
       [0.0000000e+00],
       [3.1423289e-01],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.0948860e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.0948860e-01],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [1.7806531e+00],
       [8.3795440e-01],
       [6.2846577e-01],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [4.1897721e+00],
       [0.0000000e+00],
       [1.7806531e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.1521873e+00],
       [3.4827480e+00],
       [0.0000000e+00],
       [7.8034506e+00],
       [1.1521873e+00],
       [7.5939620e-01],
       [1.6759088e+00],
       [0.0000000e+00],
       [7.9605670e+00],
       [1.1521873e+00],
       [8.4842882e+00],
       [2.6186075e-02],
       [1.3093038e-01],
       [9.4269872e-01],
       [1.0474430e-01],
       [0.0000000e+00],
       [1.4926063e+00],
       [0.0000000e+00],
       [6.2846577e-01],
       [2.6186075e-02],
       [8.3795440e-01],
       [6.5465188e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [6.0227972e-01],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.4614911e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [8.3795440e-01],
       [0.0000000e+00],
       [2.0948860e-01],
       [5.2372152e-01],
       [5.2372152e-01],
       [1.0474430e-01],
       [4.1897720e-01],
       [5.2372152e-01],
       [1.0474430e-01],
       [2.6709797e+00],
       [1.0736290e+00],
       [8.3795440e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.3093038e-01],
       [1.3093038e-01],
       [1.3878620e+00],
       [4.1897720e-01],
       [1.2569315e+00],
       [3.1423289e-01],
       [2.0948860e-01]], dtype=float32)



# 目标追踪
# 设置窗口搜索终止条件,最大迭代次数,窗口中心飘移最小值
term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT,10,1)
while(True):
    ret,frame = cap.read()
    if ret == True:
        # 计算直方图的反向投影
        hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
        dst = cv.calcBackProject([hsv],[0],roi_hist,[0,180],1)
        # 进行meanshift目标追踪
        ret,track_window = cv.meanShift(dst,track_window,term_crit)
        # 将追踪的位置绘制在视频中
        x,y,w,h = track_window
        res = cv.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
        cv.imshow('frame',res)
        # 终止
        if cv.waitKey(60) & 0xFF == ord('q'):
            break
    else:
        break
# 释放资源
cap.release()
cv.destroyAllWindows()

4.CamShift算法实现视频追踪

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rdo1qxJ8-1637755673400)(attachment:image.png)]

# 获取视频
cap = cv.VideoCapture('viedo/DOG.wmv')
# 获取第一帧图像
ret,frame = cap.read()
# 指定第一帧图像上的目标位置 (行,高,列,宽)
r,h,c,w = 197,141,0,208
track_window = (c,r,w,h)
# 指定目标的感兴趣区域
roi = frame[r:r+h,c:c+w]
# 计算直方图
# 转换色彩空间(HSV)
hsv_roi = cv.cvtColor(roi,cv.COLOR_BGR2HSV)
# 计算直方图
roi_hist = cv.calcHist([hsv_roi],[0],None,[180],[0,180])
# 归一化 min_max归一化
cv.normalize(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)
array([[2.3043747e+00],
       [3.1423289e-01],
       [5.2372152e-01],
       [9.4269872e-01],
       [3.1423289e-01],
       [9.6888477e-01],
       [4.6611214e+00],
       [1.3878620e+00],
       [4.5563769e+00],
       [4.3730745e+00],
       [1.4507086e+01],
       [5.4205173e+01],
       [8.2669441e+01],
       [6.8764633e+01],
       [6.6434074e+01],
       [7.7222733e+01],
       [2.5500000e+02],
       [4.2971348e+01],
       [1.5790203e+01],
       [2.3043747e+00],
       [1.5711645e+00],
       [2.6186076e-01],
       [1.0474430e-01],
       [1.3093038e-01],
       [7.8558221e-02],
       [0.0000000e+00],
       [1.3093038e-01],
       [5.2372150e-02],
       [1.0474430e-01],
       [2.6186075e-02],
       [2.6186076e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.0948860e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.0948860e-01],
       [0.0000000e+00],
       [3.1423289e-01],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.0948860e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.0948860e-01],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [1.7806531e+00],
       [8.3795440e-01],
       [6.2846577e-01],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [4.1897721e+00],
       [0.0000000e+00],
       [1.7806531e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.1521873e+00],
       [3.4827480e+00],
       [0.0000000e+00],
       [7.8034506e+00],
       [1.1521873e+00],
       [7.5939620e-01],
       [1.6759088e+00],
       [0.0000000e+00],
       [7.9605670e+00],
       [1.1521873e+00],
       [8.4842882e+00],
       [2.6186075e-02],
       [1.3093038e-01],
       [9.4269872e-01],
       [1.0474430e-01],
       [0.0000000e+00],
       [1.4926063e+00],
       [0.0000000e+00],
       [6.2846577e-01],
       [2.6186075e-02],
       [8.3795440e-01],
       [6.5465188e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [6.0227972e-01],
       [0.0000000e+00],
       [1.0474430e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [2.4614911e+00],
       [0.0000000e+00],
       [1.0474430e-01],
       [8.3795440e-01],
       [0.0000000e+00],
       [2.0948860e-01],
       [5.2372152e-01],
       [5.2372152e-01],
       [1.0474430e-01],
       [4.1897720e-01],
       [5.2372152e-01],
       [1.0474430e-01],
       [2.6709797e+00],
       [1.0736290e+00],
       [8.3795440e-01],
       [0.0000000e+00],
       [0.0000000e+00],
       [0.0000000e+00],
       [1.3093038e-01],
       [1.3093038e-01],
       [1.3878620e+00],
       [4.1897720e-01],
       [1.2569315e+00],
       [3.1423289e-01],
       [2.0948860e-01]], dtype=float32)



# 目标追踪
# 设置窗口搜索终止条件,最大迭代次数,窗口中心飘移最小值
term_crit = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT,10,1)
while(True):
    ret,frame = cap.read()
    if ret == True:
        # 计算直方图的反向投影
        hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
        dst = cv.calcBackProject([hsv],[0],roi_hist,[0,180],1)
        # 进行meanshift目标追踪
        ret,track_window = cv.CamShift(dst,track_window,term_crit)
        # 将追踪的位置绘制在视频中
        pts = cv.boxPoints(ret)
        pts = np.int0(pts)
        res = cv.polylines(frame,[pts],True,255,2)
        cv.imshow('frame',res)
        # 终止
        if cv.waitKey(60) & 0xFF == ord('q'):
            break
    else:
        break
# 释放资源
cap.release()
cv.destroyAllWindows()

总结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pasHYwxz-1637755673400)(attachment:image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AZPV71Mc-1637755673402)(attachment:image.png)]

目录
相关文章
|
18天前
|
前端开发 搜索推荐 编译器
【01】python开发之实例开发讲解-如何获取影视网站中经过保护后的视频-用python如何下载无法下载的视频资源含m3u8-python插件之dlp-举例几种-详解优雅草央千澈
【01】python开发之实例开发讲解-如何获取影视网站中经过保护后的视频-用python如何下载无法下载的视频资源含m3u8-python插件之dlp-举例几种-详解优雅草央千澈
【01】python开发之实例开发讲解-如何获取影视网站中经过保护后的视频-用python如何下载无法下载的视频资源含m3u8-python插件之dlp-举例几种-详解优雅草央千澈
|
3月前
|
计算机视觉
Opencv学习笔记(八):如何通过cv2读取视频和摄像头来进行人脸检测(jetson nano)
如何使用OpenCV库通过cv2模块读取视频和摄像头进行人脸检测,并提供了相应的代码示例。
162 1
|
3月前
|
计算机视觉 Python
Python实用记录(一):如何将不同类型视频按关键帧提取并保存图片,实现图片裁剪功能
这篇文章介绍了如何使用Python和OpenCV库从不同格式的视频文件中按关键帧提取图片,并展示了图片裁剪的方法。
119 0
|
2月前
|
监控 数据挖掘 数据安全/隐私保护
Python脚本:自动化下载视频的日志记录
Python脚本:自动化下载视频的日志记录
|
2月前
|
数据采集 JSON 数据格式
深入解析:使用Python爬取Bilibili视频
本文介绍了如何使用Python编写脚本自动化下载Bilibili视频。通过requests等库获取视频和音频URL,使用ffmpeg合并音视频文件,最终实现高效下载。注意遵守网站爬虫政策和法律法规。
444 4
|
3月前
|
机器学习/深度学习 编解码 Python
python将照片集变成视频
`shigen`是一位坚持更新文章的博客作者,记录成长历程,分享认知见解,留住生活感动。他利用Python库`Pillow`和`MoviePy`开发了一个工具,能够批量处理照片并生成高质量视频。该工具支持多种分辨率、自定义播放时间和照片方向,并能自动调整照片比例以实现居中对齐。通过简单的代码实现了照片视频化的需求,适合强迫症患者使用。**与shigen一起,每天不一样!**个人IP:shigen。
67 9
python将照片集变成视频
|
3月前
|
数据安全/隐私保护 流计算 开发者
python知识点100篇系列(18)-解析m3u8文件的下载视频
【10月更文挑战第6天】m3u8是苹果公司推出的一种视频播放标准,采用UTF-8编码,主要用于记录视频的网络地址。HLS(Http Live Streaming)是苹果公司提出的一种基于HTTP的流媒体传输协议,通过m3u8索引文件按序访问ts文件,实现音视频播放。本文介绍了如何通过浏览器找到m3u8文件,解析m3u8文件获取ts文件地址,下载ts文件并解密(如有必要),最后使用ffmpeg合并ts文件为mp4文件。
|
3月前
|
编解码 关系型数据库 计算机视觉
Opencv学习笔记(十一):opencv通过mp4保存为H.264视频
本文介绍了如何在OpenCV中通过使用cisco开源的openh264库来解决不支持H.264编码的问题,并提供了完整的代码示例。
265 0
Opencv学习笔记(十一):opencv通过mp4保存为H.264视频
|
4月前
|
编解码 Python
Python如何给视频添加音频和字幕
Python如何给视频添加音频和字幕
|
4月前
|
编解码 Python Windows
python有没有包 可以检测 这个视频是否可以播放
python有没有包 可以检测 这个视频是否可以播放