基于Python两种跟踪算法
2016-10-25 10:27
92 查看
忘记出处了,备份在此留着以后研究一下
def camshift(self,prob):
count = 0
#设置meanshift迭代终止条件
flag_mean = True
prob_search = prob[self.ystart:self,self.xstart.xend]
#计算质心
M00 = sum(prob_search)
S = int(1.75 * sqrt(M00/256))
for i in xrange(Height):
for j in xrange(Width):
M01+=(i-Height/2)*prob_search[i,j]
M10+=(j-Width/2)*prob_search[i,j]
xc = M10/M00
yc = M01/M00
#如果被跟踪物体没有了,则不显示方框,将窗口坐标调整到图像中心
if M00 <= 100:
self.flag_win = False
self.xstart = (prob.shape[1]/2-Width/2)
self.xend = (self.xstart+Width)
self.ystart = (prob.shape[0]/2-Height/2)
self.yend = (self.ystart+Height)
else:
self.flag_win = True
#计算目标区域中心,作为 KALMAN 观测值
self.xcore = (self.xstart+xc+self.xstart+Width)/2
self.ycore = (self.ystart+yc+self.ystart+Height)/2
#kalman 滤波
xx,yy = self.Kalman(self.pre_xcore,self.pre_ycore,self.pre_xspeed,self.pre.yspeed)
#更新中心坐标
self.xstart = self.xcore1-(Width)/2
self.xend = self.xcore1-(Width)/2
self.ystart = self.ycore1-(Height)/2
self.yend = self.ycore1+(Height)/2
def Kalman(self,pre_xcore0,pre_ycore0,pre_xspeed0,pre_yspeed0):
eps = 1e-5
#状态方程
pre_Xk = np.matrix([pre_xcore0,pre_ycore0,pre_xspeed0,pre_yspeed0])
pre_Xk = pre_Xk.T
Fk = np.matrix([1,0,self.Dt,0],[0,1,0,self.Dt],[0,0,1,0],[0,0,0,1])
Bm = np.matrix('0;0;;0;0')
Uk1 = np.random.normal(0,0,1)
Xk = Fk*pre_Xk+Bm*Uk1
Xkx = Xk[0][0]]
Xky = Xk[1][0]
#观测方程
Vk = np.matrix('1;1')*eps
Hk = np.matrix('1,0,0,0;0,1,0,0')
Zk = Hk*Xk+Vk
#信息融合
deltaX = self.xcore-Zk[0][0]
deltaY = self.ycore-Zk[1][0]
kal_Xcore = Xkx+deltaX
kal_Ycore = Xky+deltaY
kx = int(round(kal_Xcore[0,0]))
return kx,ky
def camshift(self,prob):
count = 0
#设置meanshift迭代终止条件
flag_mean = True
prob_search = prob[self.ystart:self,self.xstart.xend]
#计算质心
M00 = sum(prob_search)
S = int(1.75 * sqrt(M00/256))
for i in xrange(Height):
for j in xrange(Width):
M01+=(i-Height/2)*prob_search[i,j]
M10+=(j-Width/2)*prob_search[i,j]
xc = M10/M00
yc = M01/M00
#如果被跟踪物体没有了,则不显示方框,将窗口坐标调整到图像中心
if M00 <= 100:
self.flag_win = False
self.xstart = (prob.shape[1]/2-Width/2)
self.xend = (self.xstart+Width)
self.ystart = (prob.shape[0]/2-Height/2)
self.yend = (self.ystart+Height)
else:
self.flag_win = True
#计算目标区域中心,作为 KALMAN 观测值
self.xcore = (self.xstart+xc+self.xstart+Width)/2
self.ycore = (self.ystart+yc+self.ystart+Height)/2
#kalman 滤波
xx,yy = self.Kalman(self.pre_xcore,self.pre_ycore,self.pre_xspeed,self.pre.yspeed)
#更新中心坐标
self.xstart = self.xcore1-(Width)/2
self.xend = self.xcore1-(Width)/2
self.ystart = self.ycore1-(Height)/2
self.yend = self.ycore1+(Height)/2
def Kalman(self,pre_xcore0,pre_ycore0,pre_xspeed0,pre_yspeed0):
eps = 1e-5
#状态方程
pre_Xk = np.matrix([pre_xcore0,pre_ycore0,pre_xspeed0,pre_yspeed0])
pre_Xk = pre_Xk.T
Fk = np.matrix([1,0,self.Dt,0],[0,1,0,self.Dt],[0,0,1,0],[0,0,0,1])
Bm = np.matrix('0;0;;0;0')
Uk1 = np.random.normal(0,0,1)
Xk = Fk*pre_Xk+Bm*Uk1
Xkx = Xk[0][0]]
Xky = Xk[1][0]
#观测方程
Vk = np.matrix('1;1')*eps
Hk = np.matrix('1,0,0,0;0,1,0,0')
Zk = Hk*Xk+Vk
#信息融合
deltaX = self.xcore-Zk[0][0]
deltaY = self.ycore-Zk[1][0]
kal_Xcore = Xkx+deltaX
kal_Ycore = Xky+deltaY
kx = int(round(kal_Xcore[0,0]))
return kx,ky
相关文章推荐
- Python-基于物品的协同过滤算法(附两种算法的对比)
- 两种Python基于OpenCV的固定位置半透明水印去除方案
- 《Flask Web开发——基于Python的Web应用开发实践》一字一句上机实践(上)
- Python 使用单链表实现堆栈 (基于class, 包含迭代器)
- python3.5基于django 开发博客
- 基于Python对象引用、可变性和垃圾回收详解
- [置顶] Webdriver(selenium2)基于Python脚本实现登录填写日志(分步讲解)
- 基于 Python Matplotlib 模块的高质量图形输出
- 基于Python os模块常用命令介绍
- Python创建普通菜单示例【基于win32ui模块】
- 基于python网络编程实现支持购物、转账、存取钱、定时计算利息的信用卡系统
- Windows下安装python版的Word2Vec工具包gensim教程(基于Anaconda)
- 基于python的hadoop开源框架
- 【机器学习算法实现】主成分分析(PCA)——基于python+numpy
- 《推荐系统》基于用户和Item的协同过滤算法的分析与实现(Python)
- 基于python的一个运维自动化的项目(进度更新)【已开源】 推荐
- 基于 Python 的数据结构与算法分析学习记录(6-7)—— 树的遍历
- Python:使用基于事件驱动的SAX解析XML
- python 读取并显示图片的两种方法
- python遍历文件夹——两种遍历本地文件记录文件夹个数、文件数及文件大小的方法