OpenCV目標(biāo)檢測Meanshif和Camshift算法解析
學(xué)習(xí)目標(biāo)
在本章中,將學(xué)習(xí)用于跟蹤視頻中對象的Meanshift和Camshift算法
Meanshift
Meanshift背后的原理很簡單,假設(shè)有點的集合(它可以是像素分布,例如直方圖反投影)。 給定一個小窗口(可能是一個圓形),必須將該窗口移動到最大像素密度(或最大點數(shù))的區(qū)域。如下圖所示:
初始窗口以藍(lán)色圓圈顯示,名稱為“C1”。其原始中心以藍(lán)色矩形標(biāo)記,名稱為“C1_o”。但是,如果找到該窗口內(nèi)點的質(zhì)心,則會得到點“C1_r”(標(biāo)記為藍(lán)色小圓圈),它是窗口的真實質(zhì)心。當(dāng)然,二者是不匹配的。因此,移動窗口使新窗口的圓與上一個質(zhì)心匹配。再次找到新的質(zhì)心。很可能不會匹配。因此,再次移動它,并繼續(xù)迭代,以使窗口的中心及其質(zhì)心落在同一位置(或在很小的期望誤差內(nèi))。因此,最終獲得的是一個具有最大像素分布的窗口。它帶有一個綠色圓圈,名為“C2”。正如您在圖像中看到的,它具有最大的點數(shù)。整個過程在下面的靜態(tài)圖像上演示:
因此,通常會傳遞直方圖反投影圖像和初始目標(biāo)位置。當(dāng)對象移動時,顯然該移動會反映在直方圖反投影圖像中。因此,Meanshift算法就是將窗口移動到最大密度的新位置的算法。
OpenCV中的Meanshift
要在OpenCV中使用Meanshift,首先需要設(shè)置目標(biāo),找到其直方圖,以便可以將目標(biāo)反投影到每幀上以計算均值偏移。我們還需要提供窗口的初始位置。對于直方圖,此處僅考慮色相(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目標(biāo)檢測Meanshif和Camshift算法解析的詳細(xì)內(nèi)容,更多關(guān)于OpenCV Meanshif Camshift算法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python中數(shù)組和矩陣乘法及使用總結(jié)(推薦)
這篇文章主要介紹了python中數(shù)組和矩陣乘法及使用總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05python機(jī)器學(xué)習(xí)deepchecks庫訓(xùn)練檢查模型特點探索
這篇文章主要介紹了python機(jī)器學(xué)習(xí)deepchecks庫的訓(xùn)練檢查模型特點實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python tensorflow實現(xiàn)mnist手寫數(shù)字識別示例【非卷積與卷積實現(xiàn)】
這篇文章主要介紹了Python tensorflow實現(xiàn)mnist手寫數(shù)字識別,結(jié)合實例形式分析了基于tensorflow模塊使用非卷積與卷積算法實現(xiàn)手寫數(shù)字識別的具體操作技巧,需要的朋友可以參考下2019-12-12Python中if __name__ == ''__main__''作用解析
這篇文章主要介紹了Python中if __name__ == '__main__'作用解析,這斷代碼在Python中非常常見,它有作用?本文就解析了它的作用,需要的朋友可以參考下2015-06-06