cam shift演算法
阿新 • • 發佈:2018-12-05
import cv2
import numpy as np
import matplotlib.pyplot as plt
import time
xs,ys,ws,hs = 0,0,0,0 #selection.x selection.y
xo,yo=0,0 #origin.x origin.y
selectObject = False
trackObject = 0 #追蹤目標,0代表沒有,1代表有,-1代表需要更新目標
#滑鼠移動時會進入這個函式
def onMouse(event, x, y, flags, prams):
global xs,ys,ws, hs,selectObject,xo,yo,trackObject
#實時更新滑鼠捕捉矩形大小,設定左上角為起點座標
if selectObject == True:
xs = min(x, xo)
ys = min(y, yo)
ws = abs(x-xo)
hs = abs(y-yo)
if event == cv2.EVENT_LBUTTONDOWN: #按下
xo,yo = x, y #按下時的origin座標
xs,ys,ws,hs= x, y, 0, 0 #矩形大小初始化
selectObject = True #標記開始選中
elif event == cv2.EVENT_LBUTTONUP: #鬆開
selectObject = False
if((ws>0) and (hs>0)):
trackObject = -1 #需要追蹤目標
cap = cv2.VideoCapture("C:\\Users\\Shine\\Desktop\\split.mkv" )
#cap = cv2.VideoCapture(0)
ret,frame = cap.read()
cv2.namedWindow('origin') #新建一個視窗
cv2.setMouseCallback('origin',onMouse)
# 設定終止條件,滿足誤差、迭代10次或者至少移動1次
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
while(True):
time_start=time.time()
#frame = cv2.imread("C:\\Users\\Shine\\Desktop\\4.jpg")
if trackObject != 0:
ret,frame = cap.read()
#hsv的影象顏色/2
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #BGR2HSV
#cv2.imshow('hsv',hsv)
#Hue代表顏色(0-180:RGB角度對應顏色),Saturation代表顏色濃度(0-255:白色-彩色),Value代表亮度(0-255:黑色-彩色)
#CV_BGR2HSV 在轉換影象的時候是將 H / 2 ---> H
#影象中色相H的取值範圍為 0-360 ,所以利用opencv轉換之後得到的H的範圍為 0-180
mask = cv2.inRange(hsv, np.array((0., 30.,10.)), np.array((180.,256.,255.))) #閾值分割
#mask = cv2.inRange(hsv, np.array((0., 0.,0.)), np.array((255.,255.,255.))) #閾值分割
#cv2.imshow('mask',mask)
if trackObject == -1:
track_window=(xs,ys,ws,hs) #更新捕捉視窗的座標
maskroi = mask[ys:ys+hs, xs:xs+ws] #在mask影象中擷取track_window
hsv_roi = hsv[ys:ys+hs, xs:xs+ws] #在hsv影象中擷取track_window
print(hsv_roi) #輸出selectObject的HSV值
#cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate ]]) #返回hist
roi_hist = cv2.calcHist([hsv_roi],
[0], #計算直方圖的通道,這裡使用顏色計算直方圖,所以就直接使用第一個通道;
maskroi,
[180], #直方圖分成180份
[0.,180.])#表示直方圖中需要統計的各個畫素的值,[0.0, 180.0]表示直方圖能表示RGB所有顏色。
#作直方圖原圖(因為沒有歸一化,最後的值會很大)
# roi_hist_f=roi_hist.flatten() #value轉成list
# plt.bar(range(0,len(roi_hist_f)*2,2), roi_hist_f) #顯示方便,這裡x軸*2,間隔也*2
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX) #線性歸一化
roi_hist_f=roi_hist.flatten() #value轉成list
plt.bar(range(0,len(roi_hist_f)*2,2), roi_hist_f) #顯示方便,這裡x軸*2,間隔也*2
plt.show()
trackObject = 1
#計算反向投影,即計算輸入影象中和選中部分直方圖極值相同的區域,並該區域標記
dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
dst &= mask #過濾掉原圖中色彩不鮮明的部分
cv2.imshow('dst2',dst)
#arg:輸入影象;追蹤目標初始矩形區域;演算法結束條件
ret, track_window = cv2.CamShift(dst, track_window, term_crit)
pts = cv2.boxPoints(ret) #生成最小外接矩形
pts = np.int0(pts) #座標值變成整數
#畫線標記 arg:原圖,頂點座標,閉合曲線,BGR畫線顏色,線寬
cv2.polylines(frame,[pts],True,(255,255,0),2)
#如果正在標選區域,需要將之前標記時反色區域再反回來
if selectObject == True and ws>0 and hs>0:
cv2.imshow('selectObject',frame[ys:ys+hs,xs:xs+ws])
cv2.bitwise_not(frame[ys:ys+hs,xs:xs+ws],frame[ys:ys+hs,xs:xs+ws])
time_end=time.time()
needtime=round(time_end-time_start,3)
imgText = cv2.putText(frame, str(needtime)+'ms', (50, 50),cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 255, 255), 2)
cv2.imshow('origin',frame)
if cv2.waitKey(10)==27:
break
cv2.destroyAllWindows()