Jumping from one random frame to another random frame with continuity.

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 comentarios

"...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?
Thanks for pointing out. I didn't make myself clear before. Can you please go through my question again? I have edited it for better understanding.

Iniciar sesión para comentar.

Respuestas (2)

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 comentarios

Thanks. Actually, I want to jump from one frame to another but I want to cover the frames in the journey. Can you please go through my edited question again?
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
Prachi Sharma el 7 de Ag. de 2018
Editada: Prachi Sharma el 7 de Ag. de 2018
Here's the trick in this, if we are going from 1 to 10 then the reverse process need not go back till 1, it might go from 10 to any other random number say, 5 and then from 5 to another random number say, 12. And during this, I need the video to run.
P.S- I am trying to make a changed version of Instagram's boomerang, where in spite of completing the video and then coming back to the start of the video, you can move back and forth in the video several times.
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.
Ok, I'm gonna try this and I'm gonna try to save the corresponding frames into a folder somewhere and then turn them into a video. I'll keep posting my further doubts here.
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.
Hi there, can you please explain what's happening in the second line of your code. I could not understand videoPlayer = vision.VideoPlayer('Position', [100 100 [frameSize(2), frameSize(1)]+30]); this part.
Also, in this part frame = FRAMES(startFrame); are you calling any function named FRAME? Sorry, for the delay.
Till now, I have read the video, read its number of frames and have saved its frames in a folder.
vidobj=VideoReader('scloud.mp4');
%frames=vidobj.Numberofframes;
Frame_No=vidobj.NumberOfFrames;
%for i=1:Frame_No
for i=1:1:Frame_No
imwrite(read(vidobj,i),strcat('C:\Users\Prachi Sharma\Documents\MATLAB\Cloudframes\',num2str(i),'.png'))
end
Now, from here on wards, how should I proceed??
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.
I think I am still confused. Here is the picture of what I have written in the code so far, can you please take a look where am I going wrong?
https://drive.google.com/file/d/1_-Dor_A1RqVYI43ggoI-A1Zhb1qf7Q3p/view?usp=sharing
@Prachi Sharma: please upload your code here by clicking the paperclip button.
I couldn't see the attach or attach or photo or link from my desktop, its having some issue may be. I did attach it from my phone though, is it visible now?

Iniciar sesión para comentar.

Here is the code. I am new to this, so please don't mind my mistakes.

1 comentario

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.

Iniciar sesión para comentar.

Preguntada:

el 7 de Ag. de 2018

Comentada:

el 10 de Ag. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by