基于Meanshift的鼠标交互式运动目标跟踪与检测
- 运行环境Win 10、python 3.6.3、Opencv 3.4.2
运行环境Win 10、python 3.6.3、Opencv 3.4.2
import numpy as np
import cv2
# 用于描述矩形对角线坐标(ix,iy)、(tx,ty)
ix,iy=-1,-1 # 初始坐标
tx,ty=-1,-1 # 终点坐标
switch_ok = False # 矩形ROI提取完成标志
drawing=False # 绘图标志
video_src=0 # 提取摄像头
#video_src='images/slow_traffic_small.mp4' # 提取视频
sTep1 = True
# 创建回调函数
def check_onmouse(event,x,y,flags,param):
global ix,iy,drawing,img2,frame,tx,ty,switch_ok
# 当按下左键是返回起始位置坐标
if event==cv2.EVENT_LBUTTONDOWN: #左键点击事件
drawing = True
ix,iy=x,y
# 当鼠标左键按下并移动是绘制图形。 event 可以查看移动, flag 查看是否按下
elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON: #判断滑动与左键拖拽
if drawing==True:
img2 = frame
cv2.rectangle(img2,(ix,iy),(x,y),(0,255,0),-1)
# 当鼠标松开停止绘画。
elif event==cv2.EVENT_LBUTTONUP:
drawing = False
switch_ok = True
tx,ty = x,y
#右键点击事件:重新绘制ROI
elif event==cv2.EVENT_FLAG_RBUTTON:
switch_ok = False
sTep1 = True
cap = cv2.VideoCapture(video_src) # 开启摄像头
cv2.namedWindow('camshift') # 创建名为 camshift的窗口
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
while(1):
if drawing == False:
ret,frame = cap.read() # 读取视频
img2 = frame
cv2.setMouseCallback('camshift', check_onmouse) # 在窗口上增加回调函数
if switch_ok == True: #关键点信息选择完毕
if sTep1 == True:
#截获鼠标点击ROI点
roi = frame[ix:tx,iy:ty]
r,h,c,w = ix,tx-ix,iy,ty-iy
track_window = (c,r,w,h)
# 获取标记区域图像特征
b,g,r,_=np.uint8(cv2.mean(roi)) #取掩模标记区域颜色的平均值,并显示出来
bgd = np.zeros(roi.shape,dtype=np.uint8)
bgd[:,:,:] = [b,g,r]
hsv_roi_Type = cv2.cvtColor(bgd, cv2.COLOR_BGR2HSV)
'''
if sTep1 == True:
方案1:只能提取人皮肤图像信息
#截获鼠标点击ROI点
roi = frame[ix:tx,iy:ty]
r,h,c,w = ix,tx-ix,iy,ty-iy
track_window = (c,r,w,h)
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
#roi_hist = cv2.calcHist([hsv_roi],[0],mask,[20],[0,20])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
print('c,r,w,h = ',c,r,w,h)
print('ix,iy,tx,ty = ',ix,iy,tx,ty)
sTep1 = False
'''
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((hsv_roi_Type[0][0][0]-20, 60.,32.)), np.array((hsv_roi_Type[0][0][0]+20.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
print('所选取的关键框边角坐标 = (',ix,',',iy,')、(',tx,',',ty,')')
sTep1 = False
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
# apply meanshift to get the new location
# 依据直方图反投影信息,得到新的图像密度中心,利用meanshift计算前后两个密度中心的相对关系 apply meanshift to get the new location
ret, track_window = cv2.meanShift(dst, track_window, term_crit)
#ret, track_window = cv2.CamShift(dst, track_window, term_crit) ---- 法二 使用cv2.CamShift,可以随图像变化动态改变选择框
# Draw it on image
x,y,w,h = track_window #得到新采样窗口位置
img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2)
cv2.imshow('camshift',img2)
k = cv2.waitKey(60) & 0xff
if k == 27 :
break
else:
cv2.imwrite(chr(k)+".jpg",img2)
cv2.destroyAllWindows()
cap.release()