Clear Filters
Clear Filters

How can I find the elapsed time between two images from video file with matlab.

8 views (last 30 days)
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.

Accepted Answer

Sivsankar
Sivsankar on 5 Jun 2023
Edited: Sivsankar on 5 Jun 2023
To find the elapsed time between two images from a video file in MATLAB, you will need to follow these steps:
  1. Read in the video file using the VideoReader function.
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 Comments
bykado
bykado on 5 Jun 2023
For the time elapsed between the start and end;
If I click with the mouse when the drop falls on the surface, can we find the elapsed time by clicking with the mouse when the drop disappears?
Walter Roberson
Walter Roberson on 5 Jun 2023
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.

Sign in to comment.

More Answers (3)

Walter Roberson
Walter Roberson on 5 Jun 2023
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 Comments
Walter Roberson
Walter Roberson on 5 Jun 2023
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.

Sign in to comment.


bykado
bykado on 6 Jun 2023
Edited: bykado on 6 Jun 2023
I've come a long way with these codes. However, I got stuck at one point. I didn't work hard. I want to plot a graph after 10 measurements, but it doesn't work. The graphics window opens, but immediately the video starts playing in that window. I can't get the chart. The video freezes in the previous window. In fact, although the video freezes in the first window, the video does not appear in the graphic pop-up window. when i click on the first frozen window (when active) the video plays in that window this time. How can i solve this problem. I am using MATLAB R2015a version. Thanks...
from a single video file
% Video dosyasını aç
video = VideoReader('C:\som\droptest.mp4');
% İlk tıklama zamanını ve süreyi saklamak için değişkenler
t1 = 0;
dt = 0;
sayac = 0;
% Videoyu oynatın
figure('Name', 'Damla Testi Video');
while hasFrame(video)
frame = readFrame(video);
imshow(frame);
drawnow;
% Mouse tıklama işlevi
set(gcf, 'WindowButtonDownFcn', @mmouseClickCallback);
% Geçen süre tıklamadan sonra videonun devam etmesini sağlamak için kontrol
if dt > 0
pause(dt);
dt = 0;
end
end
function:
% Mouse tıklama işlevi
function mmouseClickCallback(~,~)
global t1 dt sayac sonuclar
if sayac >= 10
sayac=0;
end
if t1 == 0
% İlk tıklamada süreci başlatın
t1 = tic;
% İkinci tıklama için mesaj göster
title('Süre sayılıyor...');
% eski sayacı sıfırla
dt = 0;
disp('Geçen süre sayılıyor...');
else
% İkinci tıklamada süreci durdur
dt = toc(t1);
sayac=sayac+1;
% Geçen süreyi ekrana yazdır
disp([num2str(sayac),' Ölçüm: Geçen süre: ', num2str(dt), ' sn']);
% Sonuçları vektörüne ekle
sonuclar = [sonuclar dt];
% Son 10 ölçümü kontrol et ve ortalamasını hesapla
if sayac >= 10
% Son 10 ölçümü ekrana yazdır
disp('Son 10 ölçüm:');
disp(sonuclar);
% Ortalamayı hesapla
ort = mean(sonuclar);
disp(['Ortalama: ', num2str(ort), ' sn']);
%Graph
figure('Name', 'Damla Testi Süre Grafiği');
x=1:10;
y=sonuclar;
plot(x,y)
title('Süre grafigi');
xlabel('Ölçümler');
ylabel('Süreler');
% Sayaçı sıfırla ve sonuclar vektörünü temizle
sayac = 0;
sonuclar = [];
end
% Sayaçları sıfırlayın
t1 = 0;
dt = 0;
% Mouse tıklama işlevini kapatın
set(gcf, 'WindowButtonDownFcn', []);
end
end
  2 Comments
Walter Roberson
Walter Roberson on 6 Jun 2023
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 ??
bykado
bykado on 6 Jun 2023
Edited: bykado on 6 Jun 2023
Hi. Namely; I will drop at least 10 drops of water on different points of the fabric at certain intervals and take a video. I'd like to find the average time for each of these 10 drops to be absorbed by the fabric and plot their graph at the same time. Since each drop is independent, when each drop disappears, I save the time and reset the counter for the new drop. A single drop appears in the sample video. That's why you're confused.

Sign in to comment.


bykado
bykado on 8 Jun 2023
Graphics problem solved. only one problem remained. Error occurs when closing video window while playing video. How can i prevent it. Matlab R2015a
ERROR CODE:
Error using imshow>validateParent (line 348)
HAX must be a valid axes handle.
Error in imshow (line 253)
validateParent(specific_args.Parent)
Error in damlatest2 (line 20)
imshow(frame, 'Parent', axesHandle);
video oynatma kodu : % Video dosyasını aç
video = VideoReader('C:\som\droptest.mp4');
% İlk tıklama zamanını ve süreyi saklamak için değişkenler
t1 = 0;
dt = 0;
sayac = 0;
sonuclar = [];
% Videoyu oynatmak için bir figure oluştur
figure('Name', 'Damla Testi Video');
% Videonun oynatılacağı bir axes oluştur
axesHandle = axes('Parent', gcf);
while hasFrame(video)
frame = readFrame(video);
% Görüntüyü axes üzerinde göster
imshow(frame, 'Parent', axesHandle);
drawnow;
% Mouse tıklama işlevini kontrol et
set(gcf, 'WindowButtonDownFcn', @mmouseClickCallback);
% Geçen süre tıklamadan sonra videonun devam etmesini sağlamak için kontrol
if dt > 0
pause(dt);
dt = 0;
end
end
% Mouse tıklama işlevi
function mmmouseClickCallback(~,~)
global t1 dt sayac sonuclar
if sayac >= 10
sayac = 0;
end
if t1 == 0
% İlk tıklamada süreci başlatın
t1 = tic;
% İkinci tıklama için mesaj göster
disp('Geçen süre sayılıyor...');
else
% İkinci tıklamada süreci durdur
dt = toc(t1);
sayac = sayac + 1;
% Geçen süreyi ekrana yazdır
disp([num2str(sayac),' Ölçüm: Geçen süre: ', num2str(dt), ' sn']);
% Sonuçları vektörüne ekle
sonuclar = [sonuclar dt];
% Son 10 ölçümü kontrol et ve ortalamasını hesapla
if sayac >= 10
% Son 10 ölçümü ekrana yazdır
disp('Son 10 ölçüm:');
disp(sonuclar);
% Ortalamayı hesapla
ort = mean(sonuclar);
disp(['Ortalama: ', num2str(ort), ' sn']);
% Grafik çizimi
figure('Name', 'Damla Testi Süre Grafiği');
x = 1:10;
y = sonuclar(1:10);
plot(x, y);
title('Süre grafiği');
xlabel('Ölçümler');
ylabel('Süreler');
grid;
% Sayaçları sıfırla ve sonuclar vektörünü temizle
sayac = 0;
dt = 0;
sonuclar = [];
end
% Sayaçları sıfırlayın
t1 = 0;
dt = 0;
% Mouse tıklama işlevini kapatın
set(gcf, 'WindowButtonDownFcn', []);
end
end
% Pencere kapatma işlevi
function closeFigureCallback(~,~)
global t1 dt sayac sonuclar
% Sayaçları sıfırlayın
t1 = 0;
dt = 0;
sayac = 0;
sonuclar = [];
% Pencereyi kapat
close(gcf);
end

Community Treasure Hunt

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

Start Hunting!