- Read in the video file using the VideoReader function.
How can I find the elapsed time between two images from video file with matlab.
2 次查看(过去 30 天)
显示 更早的评论
How can I find the elapsed time between two images from video file with matlab.
I drop a drop of water on a surface. I want to find the water absorption time of the surface from the video I recorded with MATLAB. Can you help me? I don't know much about Matlab.
0 个评论
采纳的回答
Sivsankar
2023-6-5
编辑:Sivsankar
2023-6-5
To find the elapsed time between two images from a video file in MATLAB, you will need to follow these steps:
video = VideoReader('your_video_file.mp4');
2. Extract two frames from the video.
frame1 = read(video, frame_number_1);
frame2 = read(video, frame_number_2);
3. Convert the frames to grayscale.
gray1 = rgb2gray(frame1);
gray2 = rgb2gray(frame2);
4. Calculate the absolute difference between the two grayscale frames.
diff = abs(double(gray1) - double(gray2));
5. Threshold the difference image to focus only on the region where the water has been absorbed.
threshold = 50; % adjust as necessary
diff_threshold = diff > threshold;
6. Calculate the total number of pixels in the thresholded difference image.
num_pixels = numel(diff_threshold);
7. Calculate the number of pixels in the thresholded difference image that are white.
num_white_pixels = sum(diff_threshold(:));
8. Calculate the percentage of white pixels.
percentage_white_pixels = num_white_pixels / num_pixels * 100;
9. Calculate the time interval between the two frames using the timestamps of the frames:
time1 = video.Timestamps(frame_number_1);
time2 = video.Timestamps(frame_number_2);
time_interval = time2 - time1;
disp(time_interval);
10.Repeat steps 2-9 for each pair of frames that you want to analyze, and use the percentage of white pixels to estimate the water absorption time of the surface.
Note that this method assumes that the water absorption process appears as a significant increase in brightness in the region in which it occurs. If the water absorption process appears differently in your video, you may need to use a different method to analyze it. Note that you must give all your paths(video) correctly and this should work. Thanks!
4 个评论
Walter Roberson
2023-6-5
There is a File Exchange Contribution named videofig that is very useful for playing videos with customized behaviour -- so you could use it to design an interface that played the video but reacted to WindowButtonPress callback for example.
更多回答(3 个)
Walter Roberson
2023-6-5
Unfortunately, none of the common video formats record absolute time for frames.
If you are using videoreader() then you can use the CurrentTime property https://www.mathworks.com/help/matlab/ref/videoreader.html#busqe2j-3 to find the time offset relative to the beginning of the file. However, there is no recorded start time.
(In some cases, for some video file formats, there is the possibility that someone deliberately recorded the start time in a file header; if so it is not trivial to retrieve; see https://superuser.com/questions/1036704/is-there-something-like-exif-for-video
Note that you should definitely not the creation time or file modification time stored by the file system for the video file; those can get changed fairly easily. If you are lucky, whatever created the files might have recorded the start time as part of the file name -- but there would be variable delays between fetching the time and creating the file and starting to write frames to it, so even if that happened, it is doubtful that the accuracy would be good enough for the kind of calculation that you want to do.
2 个评论
Walter Roberson
2023-6-5
Not sure why but earlier for some reason I thought you were dealing with two different files and needed to synchronize between them.
If you are reading from a single file then you can access the CurrentTime property to find out the relative time associated with the current frame.
bykado
2023-6-6
编辑:bykado
2023-6-6
2 个评论
Walter Roberson
2023-6-6
You did not declare dt as global within your script, so when your function writes to dt that does not affect the dt of the script.
I do not understand why you keep assigning and clearing the callback ??
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!