How to keep track of different contours in opencv python

匿名 (未验证) 提交于 2019-12-03 02:54:01

问题:

I am trying to track objects with opencv in python from recorded video. I want to give a unique object nr to each visible object when it appears. For example, one object enters the screen and gets nr1, then a second joins the first and gets nr2, then the first object leaves the screen but the second is still visible and still gets object nr2 and not 1 (being the only object). I can't find any information on how to do this online. Any help (including code) is appreciated.

The code I have written so far for getting the contours and drawing object numbers:

cap = cv2.VideoCapture("video.mov") while True:     flag, frame = cap.read()     cv2.drawContours(frame, contours, -1, (255,0,0) ,1)     for i in range(len(contours)):         cnt = contours[i]         cnt_nr = i+1         x,y,w,h = cv2.boundingRect(cnt)         cv2.putText(frame, str(cnt_nr), ((x+w)/2,(y+h)/2), cv2.FONT_HERSHEY_PLAIN, 1.8, (0,0,0))     cv2.imshow("Tracked frame",frame)     k = cv2.waitKey(0)     if k == 27:          cv2.destroyAllWindows()         break 

回答1:

What kind of objects are you trying to track? If it's easy to distinguish them you can try to collect some features of objects and check whether object with similar features appeared earlier. It's hard to tell what kind of features will be the best in your situation, but you may try following:

  • contour size, area and length (or ratio: area/length or some other)
  • convex hull of object and it length (same as above - you may try ratio as well)
  • object colour (average colour) - if lighting can change consider using only H channel from HSV color space
  • some more complicated - "sum" of edges inside object (use some edge detector on object and just calculate sum of the result image)

Other solution is to use more powerfull tool designed for such task - object tracker. In one of my projects i'm using TLD tracker and it works fine, another option is to use CMT tracker, which might be better for you, because it's written in Python. Note that for tracking multiple objects you will need multiple tracker objects (or find tracker which is capable of tracking multiple different objects).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!