2016-07-08 76 views
1

基本上,此代碼將檢測對象在場景中的運動。當檢測到運動時,紅色矩形框將跟蹤對象的運動。但是,我在代碼中添加了一個新的函數,即幀差分。一般來說,它是閾值。當我運行代碼它說:「cv2.capture」對象不可調用。(cv2,捕捉)對象不可調用

import cv2.cv as cv 

class Target: 

    def __init__(self): 
     self.capture = cv.CaptureFromCAM(0) 
     cv.NamedWindow("Target", 1) 

    def run(self): 
     # Capture first frame to get size 
     ret, current_frame = self.capture() 
     previous_frame = current_frame 

     frame = cv.QueryFrame(self.capture) 
     frame_size = cv.GetSize(frame) 
     color_image = cv.CreateImage(cv.GetSize(frame), 8, 3) 
     grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1) 
     moving_average = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3) 

     first = True 

     while True: 
      current_frame_gray = cv2.cvtColor(current_frame,cv2.COLOR_BGR2GRAY) 
      previous_frame_gray = cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY)  

      frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray) 

      closest_to_left = cv.GetSize(frame)[0] 
      closest_to_right = cv.GetSize(frame)[1] 

      color_image = cv.QueryFrame(self.capture) 

      # Smooth to get rid of false positives 
      cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0) 

      if first: 
       difference = cv.CloneImage(color_image) 
       temp = cv.CloneImage(color_image) 
       cv.ConvertScale(color_image, moving_average, 1.0, 0.0) 
       first = False 
      else: 
       cv.RunningAvg(color_image, moving_average, 0.020, None) 

      # Convert the scale of the moving average. 
      cv.ConvertScale(moving_average, temp, 1.0, 0.0) 

      # Minus the current frame from the moving average. 
      cv.AbsDiff(color_image, temp, difference) 

      # Convert the image to grayscale. 
      cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY) 

      # Convert the image to black and white. 
      cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY) 

      # Dilate and erode to get people blobs 
      cv.Dilate(grey_image, grey_image, None, 18) 
      cv.Erode(grey_image, grey_image, None, 10) 

      storage = cv.CreateMemStorage(0) 
      contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) 
      points = [] 

      while contour: 
       bound_rect = cv.BoundingRect(list(contour)) 
       contour = contour.h_next() 

       pt1 = (bound_rect[0], bound_rect[1]) 
       pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) 
       points.append(pt1) 
       points.append(pt2) 
       cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1) 



      cv.ShowImage("Target", color_image) 
      cv.ShowImage("frame_diff", frame_diff) 


      # Listen for ESC key 
      c = cv.WaitKey(7) % 0x100 
      if c == 27: 
       break 

if __name__=="__main__": 
    t = Target() 
    t.run() 

這是我想從傳輸原始幀差代碼:

import cv2 

cap = cv2.VideoCapture(0) 
ret, current_frame = cap.read() 
previous_frame = current_frame 

while(cap.isOpened()): 
    current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY) 
    previous_frame_gray = cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY)  

    frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray) 

    cv2.imshow('frame diff ',frame_diff)  
    if cv2.waitKey(1) & 0xFF == ord('q'): 
     break 

    previous_frame = current_frame.copy() 
    ret, current_frame = cap.read() 

cap.release() 
cv2.destroyAllWindows() 

回答

0

不能調用capture對象,但你可以通過調用其read方法讀取。更改您的幀返回代碼爲:

ret, current_frame = self.capture.read() 

應該解決此問題。

編輯

的兩行:

ret, current_frame = self.capture.read() 
previous_frame = current_frame 

將工作爲VideoCapture但不CaptureFromCAM。你顯然不需要它們,因爲你可以通過做一個框架:

frame = cv.QueryFrame(self.capture) 
+0

相同。對象沒有屬性'讀' –

+0

@LeeAndroid哦,你使用'CaptureFromCAM'而不是'VideoCapture',所以'frame = cv.QueryFrame(self.capture)'應該沒有前兩行。 –

+0

對象不可迭代。困惑:( –