OpenCV目標檢測Meanshif和Camshift算法解析
學習目標
在本章中,將學習用于跟蹤視頻中對象的Meanshift和Camshift算法
Meanshift
Meanshift背后的原理很簡單,假設有點的集合(它可以是像素分布,例如直方圖反投影)。 給定一個小窗口(可能是一個圓形),必須將該窗口移動到最大像素密度(或最大點數(shù))的區(qū)域。如下圖所示:

初始窗口以藍色圓圈顯示,名稱為“C1”。其原始中心以藍色矩形標記,名稱為“C1_o”。但是,如果找到該窗口內(nèi)點的質(zhì)心,則會得到點“C1_r”(標記為藍色小圓圈),它是窗口的真實質(zhì)心。當然,二者是不匹配的。因此,移動窗口使新窗口的圓與上一個質(zhì)心匹配。再次找到新的質(zhì)心。很可能不會匹配。因此,再次移動它,并繼續(xù)迭代,以使窗口的中心及其質(zhì)心落在同一位置(或在很小的期望誤差內(nèi))。因此,最終獲得的是一個具有最大像素分布的窗口。它帶有一個綠色圓圈,名為“C2”。正如您在圖像中看到的,它具有最大的點數(shù)。整個過程在下面的靜態(tài)圖像上演示:

因此,通常會傳遞直方圖反投影圖像和初始目標位置。當對象移動時,顯然該移動會反映在直方圖反投影圖像中。因此,Meanshift算法就是將窗口移動到最大密度的新位置的算法。

OpenCV中的Meanshift
要在OpenCV中使用Meanshift,首先需要設置目標,找到其直方圖,以便可以將目標反投影到每幀上以計算均值偏移。我們還需要提供窗口的初始位置。對于直方圖,此處僅考慮色相(Hue)。另外,為避免由于光線不足而產(chǎn)生錯誤的值,可以使用cv2.inRange()函數(shù)丟棄光線不足的值。 使用的視頻中的三幀如下:
import cv2
import numpy as np
video_file = 'slow_traffic_small.mp4'
cap = cv2.VideoCapture(video_file)
# take first frame of the video
ret, frame = cap.read()
# setup initial location of window
x, y, w, h = 300, 200, 100, 50 # simply hardcoded the values
track_window = (x, y, w, h)
# setup the roi for tracking
roi = frame[y:y+h, x:x+w]
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])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
# setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = (cv2.TERM_CRITERIA_EPS|cv2.TERM_CRITERIA_COUNT, 10, 1)
while True:
ret, frame = cap.read()
if ret == True:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
# apply meansift to get the new location
ret, track_window = cv2.meanShift(dst, track_window, term_crit)
# draw it on image
x, y, w, h = track_window
img2 = cv2.rectangle(frame, (x, y), (x+w, y+h), 255, 2)
cv2.imshow('img2', img2)
cv2.waitKey(0)
else:
cv2.destroyAllWindows()
break

Camshift


度為止。

OpenCV中的Camshift
它與Meanshift相似,但是返回一個旋轉(zhuǎn)的矩形和box參數(shù)(用于在下一次迭代中作為搜索窗口傳遞)。
import cv2
import numpy as np
video_file = 'slow_traffic_small.mp4'
cap = cv2.VideoCapture(video_file)
# take first frame of the video
ret, frame = cap.read()
# setup initial location of window
x, y, w, h = 300, 200, 100, 50 # simply hardcoded the values
track_window = (x, y, w, h)
# setup the roi for tracking
roi = frame[y:y+h, x:x+w]
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])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
# setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = (cv2.TERM_CRITERIA_EPS|cv2.TERM_CRITERIA_COUNT, 10, 1)
while True:
ret, frame = cap.read()
if ret == True:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
# apply meansift to get the new location
ret, track_window = cv2.CamShift(dst, track_window, term_crit)
# draw it on image
pts = cv2.boxPoints(ret) # find four points of the box
pts = np.int0(pts)
img2 = cv2.polylines(frame, [pts], True, 255, 2)
cv2.imshow('img2', img2)
cv2.waitKey(0)
else:
cv2.destroyAllWindows()
break
三幀的結(jié)果如下:

附加資源
- French Wikipedia page on Camshift
- Bradski, G.R., "Real time face and object tracking as a component of a perceptual user interface," Applications of Computer Vision, 1998. WACV '98. Proceedings., Fourth IEEE Workshop on , vol., no., pp.214,219, 19-21 Oct 1998
以上就是OpenCV目標檢測Meanshif和Camshift算法解析的詳細內(nèi)容,更多關(guān)于OpenCV Meanshif Camshift算法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Python+Pygame實現(xiàn)經(jīng)典賽車游戲
這篇文章主要為大家分享了一個基于Python和Pygame實現(xiàn)的賽車小游戲,文中的示例代碼講解詳細,對我們學習Python有一定幫助,需要的可以參考一下2022-04-04
Python實現(xiàn)簡單的學生信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了Python實現(xiàn)簡單的學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

