我正在使用覆盆子pi上的kinect进行对象跟踪。 我混合了2个代码,因为我需要用kinect找到几乎对象,然后在跟踪灰色对象的过程之后使用OpenCV过滤器来设置灰色! 但我不能!请帮帮我
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种颜色的对象:黑色和白色 黑色差不多--- 我想将此代码混合到对象跟踪。但很重要。
# 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 :(得分:0)
代码中的逻辑似乎是正确的。不过,我注意到了一些实现错误。
首先,您应该在from processing import *
tool = "paintbrush"
tool = "shapechanger"
red_color = 0
green_color = 0
blue_color = 0
def setup():
size(400,400)
draw_red_button()
def draw_red_button():
set_red()
fill(red_color, green_color, blue_color)
rect(0, 380, 20, 20)
def red_button_pressed():
if mouse.pressed and (mouse.x>0 and
mouse.x<20 and
mouse.y>380 and
mouse.y<400):
return True
else:
return False
def draw():
if red_button_pressed():
set_red()
else:
fill(red_color, green_color, blue_color)
paint()
stroke(red_color, green_color, blue_color)
def paint():
if mouse.pressed:
ellipse(mouse.x, mouse.y, 20,20)
def set_red():
global red_color
global green_color
global blue_color
red_color = 255
green_color = 0
blue_color = 0
run()
之后缩进块。您还应该添加对while True
的调用,以便OpenCV不会卡在waitKey()
:
imshow()
最后,您应该将下一个块(while True:
depth = getDepthMap()
blur = cv2.GaussianBlur(depth, (5, 5), 0)
cv2.imshow('image', blur)
cv2.waitKey(1)
)的输入与前一个块的输出(mask
)相关联:
blur