Matlab skipping frame during video analysis

20 vues (au cours des 30 derniers jours)
Jaril
Jaril le 19 Sep 2024
Commenté : Jaril le 2 Oct 2024
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 :
  1. 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.
  2. 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.
  3. 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.

Réponses (1)

Jaimin
Jaimin le 24 Sep 2024
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 “VideoReaderyou can refer following MathWorks Documentation
I hope this will be helpful.
  1 commentaire
Jaril
Jaril le 2 Oct 2024
I tried your code and the same discrepancy is still happening: when I pause the code at the 867th step, the frame it is showing is not the same as when I do imshow(read(vid,867)). Here are my additional information on the problem:
  1. vid.FrameRate is not equal to vid.NumFrames/vid.Duration.
  2. When I pause the code at the 867th step, vid.CurrentTime = 34.6624, which is equal to 867/vid.FrameRate.
  3. When I do imshow(read(vid,867)), vid.CurrentTime = 34.6167, which seems to be equal to no simple calculation (for example, vid.duration/vid.NumFrames*867 = 34.6478)
So basically, first Matlab is not giving me the good frame rate, and secondly, Matlab looks at different times when different methods are used to look at the frame 867.
Can you explain to me why is that? It seems like "while hasFrame" use the frame rate of the video, but this frame rate is wrong (or maybe an average, I don't know if MP4 format has consistent frame rate), and "read(vid,867)" use a method that I'm not aware of.
Also, is there a method more reliable than the other (I am trying to synchronize all my cameras with the LED opening detection)?
Thank you for your help! (Or anyone reading this)

Connectez-vous pour commenter.

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by