MATLAB: How to get accurate timestamps for frames acquired while writing to disk

callbackdiskframeframesacquiredfcnImage Acquisition Toolboxloggingmemorytimestamps

I am trying to write multiple minutes worth of frames to disk while also getting accurate timestamps for each frame. Right now I am getting the timestamps from the FramesAcquiredFcn callback for every frame and storing them in a global variable. After all of the frames have been logged to disk, the number of timestamps do not match the number of frames acquired or the number of frames in the video file. How can I get the correct number of timestamps that are still accurate?

Best Answer

When using the FramesAcquiredFcn callback to get the timestamps for each frame, they may not be accurate. Using something like "datetime('now')" will record the time when the callback event is executed, not when the frame is acquired. To get both accurate timestamps and frames logged to disk, you can set the LoggingMode to "disk&memory":
v = videoinput(adaptorname);
v.LoggingMode = 'disk&memory';
Then, the memory logging part can be used in conjunction with "getdata" to retrieve the timestamps in the callback. The timestamps can be stored and accessed using the UserData property.
function framesAcquiredCallback(obj, event)
[~, ts] = getdata(obj);
obj.UserData = [obj.UserData ts];
end
And to retrieve the timestamps in the main script/function:
timestamps = v.UserData;