Jumping from one random frame to another random frame with continuity.
Show older comments
I have recorded a video and have split it into frames. Let's say that it has a total of 15 frames. Now, I want to go from frame 1 to a randomly chosen frame, say, frame 10 but I want to go sequentially from 1 to 10 (i.e covering frames 2,3,4,5,6,7,8,9 as well in the journey) and then I want to move from frame number 10 to frame number 5 (covering 9,8, 7,6 in the reverse journey) and then say from frame number 5 to frame number 12 ( covering 6, 7, 8,9, 10, 11 in the forward journey). How can I proceed to achieve this?
2 Comments
Stephen23
on 7 Aug 2018
"...and have split it into frames"
1. How are they stored? Are these frame already in MATLAB memory (if so, in what kind of variables?), or are they saved in files on your hardrive (if so, in what kind of files?)?
2. Do you want to allow repetition, or exclude repetition?
3. Do you want to randomly select a subset of all frames? Or should all frames be processed?
Prachi Sharma
on 7 Aug 2018
Answers (2)
Florian Morsch
on 7 Aug 2018
1 vote
Use the video file reader https://de.mathworks.com/help/matlab/import_export/read-video-files.html to get your frames from the video, then use a random number generator https://de.mathworks.com/help/matlab/random-number-generation.html to create a random number.
Now you just have to extract that frame from the video file reader and if you want to display it, give it to a video player object( https://de.mathworks.com/help/vision/ref/vision.videoplayer-system-object.html?s_tid=doc_ta ).
12 Comments
Prachi Sharma
on 7 Aug 2018
Florian Morsch
on 7 Aug 2018
You can use the following:
A = 1:1:10
A =
1 2 3 4 5 6 7 8 9 10
Here you run from 1 to 10 with steps of 1, if you want to reverse it, just go the other way.
A = 10:-1:1
A =
10 9 8 7 6 5 4 3 2 1
With this you just have to select your startframe, the endframe and the steps you want to take (i. e. only use every second frame would be A = 1:2:20, so 1 3 5 7 9 11 13 15 17 19 )
Prachi Sharma
on 7 Aug 2018
Edited: Prachi Sharma
on 7 Aug 2018
Florian Morsch
on 7 Aug 2018
Edited: Florian Morsch
on 7 Aug 2018
Well, then just run in a loop? You might want to take a look at https://de.mathworks.com/help/matlab/matlab_prog/loop-control-statements.html
Somehow you have to declare the frames and the statements. If the numbers are all random, you could use something like:
a = random number
b = random number 2
if(a<b)
A = a:1:b
else if(a>b)
A = a:-1:b
else if(random statement)
%do something
end
with a=5 and b=10 this gives A = 5 6 7 8 9 10. With a=32 and b=28 this gives A = 32 31 30 29 28.
Prachi Sharma
on 7 Aug 2018
Florian Morsch
on 7 Aug 2018
You dont have to save the frames to make them into a video. You can use the video player object directly.
What came to my mind: if you start at 1 and want to go with random directions of the video running forward/backwards, why dont you just add a counter and add or remove a 1? Lets say you have a video with 120 frames you got with the https://de.mathworks.com/help/matlab/import_export/read-video-files.html stored in the variable FRAMES (in the Matlab example its s).
Now you can go for:
startFrame = 1;
videoPlayer = vision.VideoPlayer('Position', [100 100 [frameSize(2), frameSize(1)]+30]);
while(1)
changeDirection = %%random variable which can be 1 or -1
%%change X in every iteration or every 5th iteration or so
startFrame = startFrame + changeDirection;
if(startFrame < 1)
startFrame = 1;
elseif(startFrame > 120)
startFrame = 120;
end
frame = FRAMES(startFrame);
step(videoPlayer,frame);
end
Something like this would increase or decrease your frame with each iteration, running from minimal 1 to maximal frame 120. This code is just to give you a idea. If you really want to run random numbers just increase or decrease the framecounter every X iterations and let it run up/down for the next X iterations.
Prachi Sharma
on 9 Aug 2018
Prachi Sharma
on 9 Aug 2018
Edited: Prachi Sharma
on 9 Aug 2018
Florian Morsch
on 9 Aug 2018
Edited: Florian Morsch
on 9 Aug 2018
As for the first question:
videoPlayer = vision.VideoPlayer('Position', [100 100 [frameSize(2), frameSize(1)]+30]);
initializes a video player object, which you can pass frames to display. Its a Matlab build in video player. frameSize is the width and heigth you want your player window, 100 100 are the start coordinates.
FRAMES is just the variable name, in your case it would be vidobj.
As i said before, if you have all your frames labeled you can give them to the video player object to play them.
step(videoPlayer,yourframe);
gives the next frame to the video player. Just run it in a loop with
yourframe = vidobj(X)
where X is your frame number. Change that number at random to increase or decrease with every iteration.
Prachi Sharma
on 9 Aug 2018
Stephen23
on 9 Aug 2018
@Prachi Sharma: please upload your code here by clicking the paperclip button.
Prachi Sharma
on 9 Aug 2018
Prachi Sharma
on 9 Aug 2018
0 votes
1 Comment
Florian Morsch
on 10 Aug 2018
What i gave you above was a hint in the direction you could go, not a functional code. This will never work like that (and was never intended to), it was simply a example, you will have to improve it.
First your change direction is not a fixed 1 or -1. You will have to run a loop and change that depending on when you want to change the direction of the video.
If you want to save the frames you can do that, but for the video player itself its not needed. You can just pass the frames directly to it.
The loop with while(1) will run forever, you should implement a exit routine.
Finally your Frame_No is the number of frames in vidobj. You cant pass the number to the video player, you will have to pass the vidobj frame wich you want to use. Right now you are passing the number instead of the frame.
Categories
Find more on Video Formats and Interfaces in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!