1

I am trying to track ants, it is really hard because ants change its form and I am using grayscale video. It means, mosse and camshit didnt work on my project.

I have to track 1 special one(not a random), because I need to take its position and make some analysis. So I need a way to select one ant and track it forever, I tried everything, but nothing worked.

Well, I can set the the ant position manually, or by mouse, it is not a problem, the problem is how to keep tracking the same ant after set its first position.

I am using knn filter on my code to make it easier, because I will have all my ants white, and the background in black.

It is my code:

import numpy as np
import cv2

class ColourTracker:
def __init__(self):
    cv2.namedWindow("Background")
    cv2.namedWindow("Frame")

    self.capture = cv2.VideoCapture('video.avi')
    self.knn = cv2.createBackgroundSubtractorKNN()

def run(self):
    while True:
        f, orig_img = self.capture.read()

        if( not f ):
            break;

        fore = self.knn.apply( orig_img )
        back = self.knn.getBackgroundImage()
        kernel = np.ones((5,5),np.uint8)
        fore = cv2.erode( fore, kernel )
        fore = cv2.dilate( fore, kernel )
       # fore = fore[r:r+h, c:c+w]
        image, contours, hiearchy = cv2.findContours( fore, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE )



        maximumArea = 0
        bestContour = None
        for contour in contours:
            currentArea = cv2.contourArea(contour)
            if currentArea > maximumArea:
                bestContour = contour
                maximumArea = currentArea

                x,y,w,h = cv2.boundingRect(bestContour)
                cv2.rectangle(orig_img, (x,y),(x+w,y+h), (0,0,255), 3)        

        cv2.imshow( "Background", back )
        cv2.imshow( "Frame", orig_img )

        k = cv2.waitKey(24) & 0xff 
        print k
        if k == 27:
            break
if __name__ == "__main__":
colour_tracker = ColourTracker()
colour_tracker.run()
Tes3awy
  • 2,166
  • 5
  • 29
  • 51
mmt
  • 53
  • 8
  • 1
    Visual tracking is a very hard problem and is still heavily researched. As your question is phrased now, it is too broad and impossible to answer. Your best bet is to look into different type of trackers and then try to figure out which is most suitable for your task. – Hannes Ovrén Jul 25 '15 at 08:30
  • How does your video look like? – FooTheBar Jul 25 '15 at 13:32
  • Hey @FooBar All these white points are ants, so I want to tracking only one of them for each round, it means, i want to take information for more than one, but not at the same time. I can run the code and take information from one, and them run again to track another one. http://tinypic.com/r/j0egs0/8 – mmt Jul 25 '15 at 21:56

0 Answers0