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
Stephen23
2020년 3월 6일
"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.
2020년 3월 6일
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
2020년 3월 6일
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.
2020년 3월 6일
답변 (1개)
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
C.G.
2020년 3월 6일
"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.
카테고리
도움말 센터 및 File Exchange에서 HDL-Optimized Algorithm Design에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!