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.视频文件的读取
# 获取视频对象
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.视频文件的保存
# 读取视频
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("保存成功!")
保存成功!
总结
3.MeanShift实现视频追踪
# 获取视频
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算法实现视频追踪
# 获取视频
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()
总结