Python 标记的唯一轮廓(或绘制独立轮廓)
我在python和opencv库中工作 我可以设定相机捕捉的阈值,找到轮廓(不止一个)并绘制。 但我有个问题。我尝试用一个唯一的id或标签来识别这些轮廓。(例如:Id:1,Id:2)来跟踪它们。 我需要这个轮廓使用一个持久的id 目标是画一条线并计算多个轮廓,有时多个近轮廓转化为一个大轮廓 注意:我使用的是深度相机,我的图像是深度阵列 添加一段代码 提前谢谢Python 标记的唯一轮廓(或绘制独立轮廓),python,opencv,label,Python,Opencv,Label,我在python和opencv库中工作 我可以设定相机捕捉的阈值,找到轮廓(不止一个)并绘制。 但我有个问题。我尝试用一个唯一的id或标签来识别这些轮廓。(例如:Id:1,Id:2)来跟踪它们。 我需要这个轮廓使用一个持久的id 目标是画一条线并计算多个轮廓,有时多个近轮廓转化为一个大轮廓 注意:我使用的是深度相机,我的图像是深度阵列 添加一段代码 提前谢谢 countours = cv2.findContours(mask, cv2.RETR_EXTERNAL,
countours = cv2.findContours(mask, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[1]
# only proceed if at least one contour was found
if len(countours) > 0:
# find the largest contour in the mask, then use
# it to compute the minimum enclosing circle and
# centroid
for (i,c) in enumerate(countours):
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
if M["m00"] > 0:
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
centerString = str(center)
x = (int(M["m10"] / M["m00"]))
y = (int(M["m01"] / M["m00"]))
else:
center = int(x), int(y)
if radius > 10:
# draw the circle and centroid on the frame,
cv2.circle(frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
# then update the ponter trail
if self.previous_position:
cv2.line(self.trail, self.previous_position, center,
(255, 255, 255), 2)
cv2.add(self.trail, frame, frame)
print center
self.previous_position = center
if len(countours) < 1:
center = 0
self.trail = numpy.zeros((self.cam_height, self.cam_width, 3),
numpy.uint8)
self.previous_position = None
countours=cv2.findContours(掩码,cv2.RETR\u外部,
cv2.链约简单[1]
#仅当至少找到一个轮廓时才继续
如果len(countours)>0:
#在遮罩中找到最大的轮廓,然后使用
#计算最小包围圈和
#质心
对于枚举(计数)中的(i,c):
((x,y),半径)=cv2.闭合圆(c)
M=cv2.力矩(c)
如果M[“m00”]>0:
中心=(整数(M[“m10”]/M[“m00”]),整数(M[“m01”]/M[“m00”]))
中心字符串=str(中心)
x=(int(M[“m10”]/M[“m00”]))
y=(int(M[“m01”]/M[“m00”]))
其他:
中心=整数(x),整数(y)
如果半径>10:
#在框架上绘制圆和质心,
cv2.圆(帧,(int(x),int(y)),int(半径),
(0, 255, 255), 2)
cv2.圆(框架,中心,5,(0,0,255),-1)
#然后更新ponter轨迹
如果self.previous_位置:
cv2.线(自跟踪、自前位置、中心、,
(255, 255, 255), 2)
添加(self.trail、frame、frame)
打印中心
self.previous_位置=中心
如果len(countours)<1:
中心=0
self.trail=numpy.zero((self.cam_高度,self.cam_宽度,3),
numpy.uint8)
self.previous_位置=无
两个选项。首先,轮廓已经在Python列表中,因此可以使用该列表的索引来枚举它们。事实上,在某种意义上,您已经在使用enumerate(countours)中的(i,c)实现了这一点。您只需使用索引i
在空白图像上使用值i
对每个轮廓进行“着色”,然后通过检查图像就可以知道哪个轮廓是哪个轮廓。另一个可能更好的选择是使用标签来标记二值图像,而不是二值图像的轮廓。另外,预先标记您可能会尝试关闭blob。编辑:我遵循您的建议,但我遇到了新问题:)
标签不是持久的。当我移动两个轮廓时,标签的数量会发生变化。
添加一些图片和我的代码
为True时:
cnt+=1
如果(cnt%10)=0:
now=time.time()
dt=现在-最后一次
fps=10/dt
fps_平滑=(fps_平滑*平滑)+(fps*(1.0-平滑))
最后=现在
c=开发颜色
cad=dev.cad
开发人员等待帧()
c=cv2.CVT颜色(c,cv2.COLOR_rgb2灰色)
深度=偏差深度*偏差深度\刻度*1000
打印深度
#d=cv2.applyColorMap(depth.astype(np.uint8),cv2.COLORMAP\u SUMMER)
打印深度
res=np.float32(开发深度)
depth=255*np.logical_和(depth>=100,depth@A.Garrido)如果它回答了您的问题,您可以将答案标记为已接受。但是,如果您尝试了其中一种方法,并且在实现它们时遇到任何问题,我可以扩展它!您的第一个答案解决了查找和标记轮廓的问题,但是现在,标签取决于宽度和变化(0表示宽度的最大值,顶部图像,1表示较低的值),并根据位置逐帧更改。谢谢。
while True:
cnt += 1
if (cnt % 10) == 0:
now = time.time()
dt = now - last
fps = 10/dt
fps_smooth = (fps_smooth * smoothing) + (fps * (1.0-smoothing))
last = now
c = dev.color
cad = dev.cad
dev.wait_for_frames()
c = cv2.cvtColor(c, cv2.COLOR_RGB2GRAY)
depth = dev.depth * dev.depth_scale * 1000
print depth
#d = cv2.applyColorMap(depth.astype(np.uint8), cv2.COLORMAP_SUMMER)
print depth
res = np.float32(dev.depth)
depth = 255 * np.logical_and(depth >= 100, depth <= 500)
res2 = depth.astype(np.uint8)
kernel = np.ones((3, 3), np.uint8)
#res2 = cv2.blur(res2,(15,15))
res2 = cv2.erode(res2, kernel, iterations=4)
res2 = cv2.dilate(res2, kernel, iterations=8)
im_floodfill = res2.copy()
h, w = res2.shape[:2]
mask2 = np.zeros((h + 2, w + 2), np.uint8)
cv2.floodFill(im_floodfill, mask2, (0, 0), 255)
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
im_out = res2 | im_floodfill_inv
im_out = cv2.blur(im_out,(5,5))
label = cv2.connectedComponentsWithStats(im_out)
n = label[0] - 1
cog = np.delete(label[3], 0, 0)
for i in xrange(n):
# im = cv2.circle(im,(int(cog[i][0]),int(cog[i][1])), 10, (0,0,255), -1)
cv2.putText(im_out, str(i), (int(cog[i][0]), int(cog[i][1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
cv2.imshow("Depth", res)
cv2.imshow("OUT", im_out)
cv2.imshow( "C", res2)