2010-07-30 71 views
4

我已經獲得OpenCV與Python的合作,我甚至可以通過我的攝像頭檢測到一張臉。然而,我真正想要做的是看到移動,並在移動中找到重點。 camshift樣本接近我想要的,但我不想選擇要跟蹤視頻的哪一部分。可以預測下一幀的獎勵點數。如何跟蹤使用OpenCV和Python的blob

下面的代碼我目前:

#!/usr/bin/env python 

import cv 

def is_rect_nonzero(r): 
    (_,_,w,h) = r 
    return (w > 0) and (h > 0) 

class CamShiftDemo: 

    def __init__(self): 
     self.capture = cv.CaptureFromCAM(0) 
     cv.NamedWindow("CamShiftDemo", 1) 
     self.storage = cv.CreateMemStorage(0) 
     self.cascade = cv.Load("/usr/local/share/opencv/haarcascades/haarcascade_mcs_upperbody.xml") 
     self.last_rect = ((0, 0), (0, 0)) 

    def run(self): 
     hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0,180)], 1) 
     backproject_mode = False 
     i = 0 
     while True: 
      i = (i + 1) % 12 

      frame = cv.QueryFrame(self.capture) 

      if i == 0: 
       found = cv.HaarDetectObjects(frame, self.cascade, self.storage, 1.2, 2, 0, (20, 20)) 
       for p in found: 
        # print p 
        self.last_rect = (p[0][0], p[0][1]), (p[0][2], p[0][3]) 
        print self.last_rect 

      cv.Rectangle(frame, self.last_rect[0], self.last_rect[1], cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0) 
      cv.ShowImage("CamShiftDemo", frame) 

      c = cv.WaitKey(7) % 0x100 
      if c == 27: 
       break 

if __name__=="__main__": 
    demo = CamShiftDemo() 
    demo.run() 
+1

只是遵循其煤泥線索的解決方案:d – rownage 2010-07-30 18:44:51

+0

即使是一個C++的例子是有益的,因爲該文檔是非常難以遵循。 – 2010-07-30 19:41:23

+1

另請參閱simpler'ized:http://stackoverflow.com/questions/3374828/how-do-i-track-motion-using-opencv-in-python – 2010-07-30 19:53:35

回答