You can basically put it into a loop over video frames where you get a frame and then do (untested, but see attached demo):
grayImage = rgb2gray(thisFrame);
mask = ~imbinarize(grayImage);
mask = imfill(mask, 'holes');
props = regionprops(mask, 'Centroid')
xCenter(frameIndex) = props.Centroid(1);
yCenter(frameIndex) = props.Centroid(2);
Then to find distance tracked
distanceTraveled = sqrt(deltaX .^ 2 + deltaY .^ 2);
totalDistance = sum(distanceTraveled)
That said, there are already many animal tracking packages out there that have this all worked out already. We use Noldus Ethovision in my company. It's very sophisticated, and does way more than simple tracking of centroid. For example, you can tell if the animal is still moving/alive even if it's centroid has not changed. And it can track several animals at once.
Here are some links on animal tracking software. If I were to pick one, I’d start with one of the noldus packages because they’re made by a company whose main business is animal tracking and it seems very professional. Most of the others seem like research projects – some seem to be just source code that you have to compile. Since noldus offers so many tracking programs, we should contact the company to make sure we try the most appropriate one.
Variety of powerful packages
Perhaps the most comprehensive solution. Professional global company dedicated to animal and human behavior and motion analysis. Founded by an animal behavior scientist 33 years ago. Has over 10,000 clients. Can do much more than just position tracking – like behavior, etc. Has a free trial software download. Packages start at $5850 : https://www.noldus.com/ethovision-xt
Looks like it can do the most of any of the other software I ran across.
idTracker is a videotracking software that keeps the correct identity of each individual during the whole video.
It works for many animal species including mice, insects (Drosophila, ants) and fish (zebrafish, medaka, stickleback). Claims to handle animal occlusion/overlapping better than other software.
Written in MATLAB but is compiled so you don’t need MATLAB.
Ctrax is an MATLAB-BASED open-source, freely available, machine vision program for estimating the positions and orientations of many walking flies, maintaining their individual identities over long periods of time. It was designed to allow high-throughput, quantitative analysis of behavior in freely moving flies. Our primary goal in this project is to provide quantitative behavior analysis tools to the neuroethology community; thus, we've endeavored to make the system adaptable to other labs' setups.
Microsoft Research has released free software that scientists or students can use to analyze animal behavior captured on video, potentially offering insights that could, among other things, aid threatened species. The Redmond, Wash., software giant’s research division will formally announce ZooTracer on Wednesday. It’s the result of a partnership between MSR’s Computational Ecology and Environmental Sciences division and the Computer Vision group, both based in Cambridge, England.
The mission of the first group is to create computer models to help answer scientific questions about the planet and environment
The big idea here is that the process can be automated. ZooTracer’s algorithms can extract meaning from video by tracking and analyzing the movements of animals, be they bees, frogs or elephants.
For example, MSR is using it to analyze what flower color patterns lure bees and under what conditions, information that can advance our understanding of evolutionary selection pressures.
But it’s possible the tool could also be used to track bee movements in an effort to help unravel the mystery of colony collapse disorder.
Developed by a Univ. Zurich professor. Bemovi is an R package that allows to extract abundance, behaviour and morphology of individual organisms from video sequences. The package relies on R - the statistical computing environment and ImageJ, as well as the ParticleTracker plug-in developed for ImageJ. I believe the software is free.
wrMTrck plugin is based on Mtrack2 by Nico Stuurman, which is based on MultiTracker by Jeffrey Kuhn, which is based on Object_trackerby Wayne Rasband. wrMTrck will identify the objects in each frame, and then determine which objects in successive frames are closest together. If theses are within a user-defined distance (the maximum velocity of the objects) and have similar area (maxAreaChange) they are assembled into tracks. When multiple objects are within the distance determined by the maximum velocity, the closest object is selected and the object is flagged in the output. wrMTrck adds the following functionality to MTrack2
We created CowLog, because we wanted a free easy to use program for coding digital video. CowLog is open source software and it is therefore free to use and modify.
CowLog is lightweight software, which tracks the time code from video files, and records the time with the coded behavior to a data file. The program reads common digital video formats and works in all major operating systems: Linux, Windows and Mac OS X.
ToxTrac is a free Windows program optimized for tracking animals. It uses an advanced tracking algorithm that is robust; very fast; and that can handle one or several animals in one or several environments. The program provides useful statistics as output. ToxTrac can be used for fish, insects, rodents, etc.
Tracktor is an OpenCV based object tracking software. The software is able to perform single-object tracking in noisy environments or multi-object tracking in uniform environments while maintaining individual identities.
Tracktor is command based (i.e. there is no graphical user interface (GUI)) but anyone with basic coding/scripting skills such as R users can readily use it.
DeepLabCut is a toolbox for markerless pose estimation of animals performing various tasks. Read a short development and application summary below. As long as you can see (label) what you want to track, you can use this toolbox, as it is animal and object agnostic. “open source Deep Learning software that works really good once you get used to it."