for end loop variable not saving

2 Ansichten (letzte 30 Tage)
C.G.
C.G. am 6 Mär. 2020
Bearbeitet: Stephen23 am 6 Mär. 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 Kommentare
Stephen23
Stephen23 am 6 Mär. 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.
C.G.
C.G. am 6 Mär. 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 6 Mär. 2020
Bearbeitet: Stephen23 am 6 Mär. 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 Kommentare
C.G.
C.G. am 6 Mär. 2020
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 am 6 Mär. 2020
Bearbeitet: Stephen23 am 6 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by