for end loop variable not saving

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 commentaires

"for end loop variable not saving"
How are you checking this? Please show the commands you used to check if the loop "works" or not.
Also please show us these values:
numFrames
k
size(framediffs)
C.G.
C.G. le 6 Mar 2020
As said, im very new to this, so I am basing it off error messages alone. How do I show the commands?
My code works and generates the figures correctly until figure 3, because there is no value to the framediffs.
Values are:
numFrames = 1
k = []
framediffs []
Stephen23
Stephen23 le 6 Mar 2020
Modifié(e) : Stephen23 le 6 Mar 2020
Please show us the complete error message. This means all of the red text.
Those values make it clear that your last loop never iterates. Look at the numFrames value.
What do you want your code to do when there is only one frame? (basically the question is: how do you define the difference between one frame?)
Stephen23
Stephen23 le 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.
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

Connectez-vous pour commenter.

Réponses (1)

Stephen23
Stephen23 le 6 Mar 2020
Modifié(e) : Stephen23 le 6 Mar 2020

0 votes

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 commentaires

I have tried using aviread, however i get this error message
Warning: AVIINFO will be removed in a future release. Use VIDEOREADER instead.
Undefined function 'aviread' for input arguments of type 'char'.
Error in Assignment_2_code (line 44)
ball = aviread('ball.avi');
Stephen23
Stephen23 le 6 Mar 2020
Modifié(e) : Stephen23 le 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.

Connectez-vous pour commenter.

Tags

Question posée :

le 6 Mar 2020

Modifié(e) :

le 6 Mar 2020

Community Treasure Hunt

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

Start Hunting!

Translated by