for end loop variable not saving

6 views (last 30 days)
C.G.
C.G. on 6 Mar 2020
Edited: Stephen23 on 6 Mar 2020
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
C.G.
C.G. on 6 Mar 2020
when i run this section of code, both numFrames and k become 500, the correct number
%% 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
However when I then run this section, they convert to 1
% Convert each frame to grayscale
numFrames = size(obj,2);
for k = numFrames:-1:1
grobj(:,:,k) = rgb2gray(mov(k).cdata);
end

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 6 Mar 2020
Edited: Stephen23 on 6 Mar 2020
Your code calculates numFrames using two different methods.
  1. method gives 500 (apparently correct),
  2. 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
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!