for end loop variable not saving
2 views (last 30 days)
Show older comments
I am relatively new to coding, and I am trying to track a ball on a pendulum following an example i found online.
However, in one of my for/end loops (last one in code below) the variable isnt saving which is stopping me using the rest of the code. My other for/end loops work perfectly. Is there a way of making this variable save to my workspace?
%% IMPORTING
obj = VideoReader('ball.avi')
%% FRAMES
% Number of frames
vidFrames = read(obj);
% Get individual frames
numFrames = get(obj, 'numberOfFrames');
for k = 1:numFrames
mov(k).cdata = vidFrames(:,:,:,k);
mov(k).colormap = [];
end
% Watch imported video
figure(1), movie(mov, 1, obj.FrameRate),title('Original Movie');
% Show all frames in figure
figure(2), montage(vidFrames(:,:,:,75:90)),title('Montage of Frames');
%% COLOUR TO GREY
% Convert each frame to grayscale
numFrames = size(obj,2);
for k = numFrames:-1:1
grobj(:,:,k) = rgb2gray(mov(k).cdata);
end
%% COMPUTING DIFFERENCES BETWEEN FRAMES
for k = numFrames-1:-1:1
framediffs(:,:,k) = imabsdiff(grobj(:,:,k), grobj(:,:, k+1));
end
figure(3), imshow(framediffs(:,:,100), [])
5 Comments
Stephen23
on 6 Mar 2020
Your code needs to handle the special cases of when numFrames<=1.
Clearly it makes no sense to measure the difference between 2 frames if you have exactly 1 frame.
If you expect that you should actually have >1 frame, then there might be a problem with the video or how it is imported.
Answers (1)
Stephen23
on 6 Mar 2020
Edited: Stephen23
on 6 Mar 2020
Your code calculates numFrames using two different methods.
- method gives 500 (apparently correct),
- method gives 1 (apparently incorrect).
Does that make you think that perhaps the second method is not correct? And that perhaps you should:
- read the documentation for the object type you are actually using to know how to count the frames it contains.
- use the method that counts the frames correctly (hint: the one that works on whatever object is returned by VideoReader, i.e. method 1).
The code at the link you gave imports the video using aviread, which apaprently imports the video as one numeric array (in which case size makes sense). But you imported the video using VideoReader, which loads into an object which is nothing like a numeric matrix (and for which size makes absolutely no sense).
2 Comments
Stephen23
on 6 Mar 2020
Edited: Stephen23
on 6 Mar 2020
"I have tried using aviread, however i get this error message"
I certainly would not recommend using the obsolete aviread. Is there a particular reason why you want to use a function that is outdated and has likely already been removed from MATLAB? It might be possible to find some third-party version (of dubious quality) if you insist on going down that route... but there be dragons!
You could just follow the advice I gave in my answer.
See Also
Categories
Find more on Convert Image Type 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!