2017-06-29 205 views
0

我正在用樹莓派上的kinect進行物體跟蹤工作。 我混合2代碼,因爲我需要找到kinect幾乎對象,然後使用OpenCV過濾器設置灰色後,跟蹤灰色對象的過程! 但我不能!請幫我通過kinect在樹莓派上進行物體跟蹤

import freenect 
import cv2 
import numpy as np 

""" 
Grabs a depth map from the Kinect sensor and creates an image from it. 
""" 
def getDepthMap(): 
depth, timestamp = freenect.sync_get_depth() 

np.clip(depth, 0, 2**10 - 1, depth) 
depth >>= 2 
depth = depth.astype(np.uint8) 

return depth 

while True: 
depth = getDepthMap() 
#text_file = codecs.open("log2.txt", "a","utf-8-sig") 
#text_file.write(str(depth)+'\n') 

depth = getDepthMap() 
blur = cv2.GaussianBlur(depth, (5, 5), 0) 
cv2.imshow('image', blur) 

這個代碼可以告訴我在2 Color對象:黑白 黑色幾乎是--- 我想混這段代碼目標跟蹤。但是icant。

# find contours in the mask and initialize the current 
# (x, y) center of the ball 
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 
    cv2.CHAIN_APPROX_SIMPLE)[-2] 
center = None 

# only proceed if at least one contour was found 
if len(cnts) > 0: 
    # find the largest contour in the mask, then use 
    # it to compute the minimum enclosing circle and 
    # centroid 
    c = max(cnts, key=cv2.contourArea) 
    ((x, y), radius) = cv2.minEnclosingCircle(c) 
    M = cv2.moments(c) 
    center = (int(M["m10"]/M["m00"]), int(M["m01"]/M["m00"])) 

    # only proceed if the radius meets a minimum size 
    if radius > 10: 
     # draw the circle and centroid on the frame, 
     # then update the list of tracked points 
     cv2.circle(frame, (int(x), int(y)), int(radius), 
      (0, 255, 255), 2) 
     cv2.circle(frame, center, 5, (0, 0, 255), -1) 

# update the points queue 
pts.appendleft(center) 

http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/

回答

0

在你的代碼的邏輯似乎是正確的。不過,我注意到一些執行錯誤。

首先,您應該在while True之後縮進塊。你也應該添加一個調用waitKey(),這樣的OpenCV不會停留在imshow()

while True: 
    depth = getDepthMap() 
    blur = cv2.GaussianBlur(depth, (5, 5), 0) 
    cv2.imshow('image', blur) 
    cv2.waitKey(1) 

最後,你應該下一個塊(mask)與前一個的輸出輸入相關聯(blur) :

mask = blur