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-I4ItiooG-1637755673389)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/6da18c73436b45c2a9f0b0090cbca7b1.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5s26ix61-1637755673391)(attachment:image.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5s26ix61-1637755673391)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/eb6bc9b11b624039a579f83c2feebb81.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9lwfjjn0-1637755673394)(attachment:image.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9lwfjjn0-1637755673394)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/00045eaf6f1f48bb89e0e05fba556891.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UTKrGdcP-1637755673395)(attachment:image.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UTKrGdcP-1637755673395)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/4cfe698770894d57825feec9d40f195e.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
# 获取视频对象
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-cfAq4FYP-1637755673397)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/d39564a16bb04d60833ce55bf82f381b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cDP566f3-1637755673397)(attachment:image.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cDP566f3-1637755673397)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/7152d4c4134e49f592b5467c6f4f30dc.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
# 读取视频
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)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y2og246l-1637755673399)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/44f0784d882343b495e89b927b8722bd.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_14,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
3.MeanShift实现视频追踪
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ojrfzjny-1637755673399)(attachment:image.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ojrfzjny-1637755673399)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/39d3a1e15b734255a28af7477e78bb28.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
# 获取视频
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)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rdo1qxJ8-1637755673400)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/f8aaba7c2aee40929131af95558904ee.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
# 获取视频
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-pasHYwxz-1637755673400)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/85fab059908e44d3b3819d06edc82957.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AZPV71Mc-1637755673402)(attachment:image.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AZPV71Mc-1637755673402)(attachment:image.png)]](https://ucc.alicdn.com/images/user-upload-01/8d8cd9deab1c46b5a99a9123429da614.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAV1NLSDA5Mjk=,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)