Object tracking in Mathematica

I’ve been playing with ImageFeatureTrack in Mathematica over the last few days. My interest is in helping me and my students track the beads on a swinging beaded chain (something we worked on quite a bit last summer). I just wanted to get a few of my initial results down here.

First, the raw footage:

Now the Mathematica commands:

all=Import["path-to-avi-file", {"Frames", All}];
tracks=ImageFeatureTrack[all];
Manipulate[HighlightImage[all[[i]], tracks[[i]]], {i, 1, Length[all], 1];

And here’s the movie that makes (Mathematica just makes a movie showing the slider going forward and back). I tried to export the whole thing as an avi running at the same speed as the above one but I ran into memory problems.

Here’s an animated gif of just the tracked points:

just the tracked points from the original video

just the tracked points from the original video

And, here’s the traces of those particles (done with ListLinePlot[Transpose[track]]):

traces of all the tracked weights

traces of all the tracked weights

The second command (the ImageFeatureTrack one) allows you to tell it what to track. However, if you don’t give it any coordinates (as I didn’t above) it finds features that it thinks are worth tracking. It takes a while (~2 minutes) but the results are pretty cool.

Here’s one more example where I dangled the camera and shook it around:

I’m pretty impressed, though I’m not sure this does any better than the very cool (and free!) tracker software. Maybe others can chime in about that. I know for sure my students really struggled to get tracker to grab all our data last summer, but I’ll let them weigh in on whether they think this is better.

Some comment starters for you:

  1. This is cool, would it work with … ?
  2. This is dumb, I could have done it better with …
  3. This is confusing, can you explain ___ better?
  4. I worked in the lab last summer and this is great because …
  5. I worked in the lab last summer. I have spent months trying to forget that horror. Thanks for dredging it all back up.
About these ads

About Andy "SuperFly" Rundquist

Professor of physics at Hamline University in St. Paul, MN
This entry was posted in mathematica, physics. Bookmark the permalink.

14 Responses to Object tracking in Mathematica

  1. sampterson says:

    Hey Andy, cool blog post.

    As a student who worked on this project last year, my question for the ImageFeatureTrack is how well does it work? Using Tracker to analyze a 40 second video would take a good portion of a morning and was quite user intensive. (and not too fun either) If this is faster I would definitely prefer it over Tracker. It would get rid of transposing the data from Tracker to Mathematica, which would also be helpful.

    Have you tried tracking a more complicated motion?

    • Andy "SuperFly" Rundquist says:

      that’s the most complicated one I’ve tried yet, but I’ll keep working on it. A 40 second video would really take a while, but, on the other hand, it’s the computer’s time, not yours, so hopefully more could get done in the mean time.

  2. Andrew LeBeau says:

    As a member of the team that worked on the summer project, I remember vividly the horror of trying to continuously monitor tracking each individual bead frame by frame. This Mathematica feature from the video seems to solve that problem almost entirely by itself. The first question that popped into my head is why it tracked multiple points on one bead, and what can be done to prevent the software from doing that. Would it be as simple as putting in the initial coordinates of the beads? I am also curious how consistent it is at picking certain points, what I mean by that is how easy would it be to setup two cameras and analyze both data sets and merge them? If it can easily do that, then this is the way to go in the future. However the ImageFeatureTrack is will certainly save on a lot of stress and generate more time to pursuing interesting things instead of spending three hours on one bead.

  3. Pingback: Finding grains | SuperFly Physics

  4. Pingback: Tracking Objects with Mathematica | Teaching Physics

  5. Lu says:

    “Here’s an animated gif of just the tracked points”
    What command was used to create the gif?

    • Andy "SuperFly" Rundquist says:

      Something like Export[“file path to gif with .gif as extension”, list-of-frames]

      On Wed, Feb 4, 2015 at 6:45 AM, SuperFly Physics wrote:

      >

      • Lu says:

        Thank you for your reply.

        I am a student in university and a beginner of Mathematica. Before Mathematica, I learned FORTRAN. Some features in Mathematica are amazing. Some time ago, I received a task of tracking one point during a vibration of a plank and computing the amplitude of the vibration.

        Your blog gave me a lot of inspirations. A video of the vibration was taken and I managed to track the key point and get the coordinate of the tracking point. The task is almost finished.

        I find it is interesting to make a animation of just the tracked points. I just do not know how to remove other features from the frames imported from the video, remaining the tracked points only? Or not editing the frames but just creating new frames with the coordinates? Could you give some hints?

      • Andy "SuperFly" Rundquist says:

        If you have the coordinates of the points in a list called “trajectory” then you could do something like: frames=Table[Graphics[Point[trajectory[[i]]], PlotRange->{{xmin, xmax}, {ymin, ymax}}], {i, Length[trajectory]}]; ListAnimate[frames] (* makes sure it looks right *) Export[“path to file.gif”, frames]

        On Thu, Feb 5, 2015 at 6:37 AM, SuperFly Physics wrote:

        >

      • Lu says:

        Many thanks and best regards.

  6. Erika says:

    First of all, thanks for this great tutorial! I work in a neurophysics lab and I am using this technique to track jaw movements of mice.

    I was wondering if you perhaps know a way to combine the trajectory animation with the listline plot command. In other words, how can I create an animation of the tracking that leaves it’s trace behind, so you can see the movement path progression over time? Sort of as if the tracking dot was “drawing” as it moved? Thanks again!

    • Andy "SuperFly" Rundquist says:

      Yeah, you can do something like this:
      Manipulate[Show[ListLinePlot[here-you-grab-the-data-up-to-that-time-step], frame-of-the-movie-you-want-to-show], {i, 1, Length[frames], 1}]

      I’m happy to help out if you can’t get that to work.

      • Erika says:

        Wow, thanks for the reply!
        I still can’t seem to get this to work. I believe it’s a problem with the way I am defining “frames”:
        So far, here is what I have:

        all = Import[“file”, {“Frames”, All}];

        tracks = ImageFeatureTrack[all, MaxFeatures -> 1];

        Manipulate[HighlightImage[all[[i]], tracks[[i]]], {i, 1, Length[all], 1}];

        frames = Table[Graphics[Point[tracks[[i]]],PlotRange -> {{xmin, xmax}, {ymin, ymax}}], {i,Length[tracks]}];

        Manipulate[Show[ListLinePlot[tracks], frames], {i, 1, Length[frames], 1}]

  7. Andy "SuperFly" Rundquist says:

    I think what you want is something like Manipulate[Show[ListLinePlot[tracks[[All,;;i]]], frames[[i]]],{i, … or something like that to pick out the part of tracks up to that frame to be drawn

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s