Matlab skipping frame during video analysis
19 views (last 30 days)
Show older comments
Hi, I am currently trying to detect the opening and closing of a LED in videos of my experiments. I did a code that seems to work well, except that I discovered that Matlab seems to be skipping a frame and reading the next one 2 times instead. Indeed, when reading my second video cam2.mp4 (it might happen in other videos too, but that is the one I discovered), Matlab reads the frame 867 as the frame 868. More precisely :
- When I run my code as it is, namely with my FOR loop ranging from 1 to numFrames, Matlab said the LED is opened at frame 867 of cam 2 (gives TRUE), but it is not.
- However, when I run my code with my FOR loop ranging from 3 (and over) to numFrames, Matlab said the LED is not opened at frame 867 of cam 2, but only from frame 868, as it should.
- If I then run my code from frame 1, pause it at the very beginning of frame 867, imshow frame i (which is frame 867), Matlab shows me the image of frame 868. However, if I immediately imshow frame i again, without doing anything else, it then shows me the right frame 867.
I think it is not normal that 1) my code gives different output if I only change the starting point of the FOR loop, and 2) Matlab shows me different images when asked twice in a row to show the same.
Can you explain to me what is going on? I don't have too much of a background in informatics.
0 Comments
Answers (1)
Jaimin
on 24 Sep 2024
Hi @Jaril
In the given code, the “read” function is used to retrieve frames from the video. To resolve described issue, I recommend using the “hasFrame” and “readFrame” functions instead.
I have included a code snippet for better understanding. You will need to update the “detectLED_CCTV.m” file.
function vecdetect = detectLED_CCTV(vid, rect)
n = vid.NumFrames;
vecdetect = false(n, 1);
i = 0;
while hasFrame(vid) % Loop through each frame of the video
i = i + 1;
rgb = imcrop(readFrame(vid), rect); % Crop the current frame to the region of interest
bin = createMaskLED_cctv2(rgb);
sumpix = sum(sum(bin));
if sumpix > 10
vecdetect(i) = true;
end
end
end
For more information about “VideoReader” you can refer following MathWorks Documentation
I hope this will be helpful.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!