2
$\begingroup$

I am trying to recreate the following physics experiment to analyze chaotic motion:

https://www.researchgate.net/publication/275890532_Tracking_the_Motion_of_a_Double_Pendulum_Using_Mathematica

I have written my program to import a video and use mathematica to create a list of the frames of the video which I will then use to analyze the pixels of each frame as they did in the experiment.

This is my code thus far:

(* First set the directory to the location of the video clip *)
SetDirectory["/Users/user/Desktop"];

(* Select the video clip you would like to analyze *)
videoName = "IMG_4777.mov";
video = Import[videoName]
(* Out: {1, 2, 3, ..., 1308, 1309, 1310} *)

(* Note the value of the last frame, set lastFrame to that value *)
firstFrame = 1;
lastFrame = 1310;
step = 1;

(* Import the video again this time as an ImageList with the correct range *)
(* Please be aware this step can take a long time, you can up the 'step' value to speed it 
   up but you will loose frames. *)
video = Import[videoName, {"ImageList", Range[firstFrame, lastFrame, step]}];

(* Please be aware this step can also take a long time. *)
(* Here we are creating a table of images from the imported video *)
tableStep = 1;
images = Table[ImageData[video[[i]]], {i, 1, Length[video], tableStep}];

image = images[1]; (<- This is where I am having a problem)

As you can see on line 15 I am attempting to get the first frame so I can analyze its pixels.

Please note that the video clip I am using is about 30 seconds in length (1 trial of the experiment).

My problem: I am having trouble accessing the images 'table' (assuming I have set it up correctly)

I want to be able to iterate through each frame and analyze the pixels and export the resulting positions to an excel sheet.

I am not sure what syntax I should be using or how best to manipulate 'ImageData'

$\endgroup$
  • 1
    $\begingroup$ I think you want images[[1]]. $\endgroup$ – Carl Lange Apr 2 at 21:30
  • $\begingroup$ @CarlLange image = images[[1]] gives me an error: Part specification images[[1]] is longer than depth of object $\endgroup$ – happyhippie924 Apr 2 at 21:41
  • $\begingroup$ Are your variables turning blue after you run the final line of image = images[[1]]? Your code (with the improvement made by Carl Lange) should be correct. When I try a similar procedure on only 6 frames of a video, I'm finding that Mathematica crashes and all the variables are created. I get the same error message and in my case it is because images is cleared so you can't take part 1 of it. $\endgroup$ – MassDefect Apr 2 at 22:21
  • 1
    $\begingroup$ Have you tried looking at the Dimensions of images? That error is a clue that the data has a dimension that you don't expect. $\endgroup$ – Carl Lange Apr 2 at 22:43
  • $\begingroup$ Try importing the image frames one at a time, operate on the one, and then get the next. Something like frames = Import[videoName, {"ImageList", i}] and then increment i to get the next frame. $\endgroup$ – bill s Apr 3 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.