Introduction
On this submit, I’ll analyze a video of a container filling with liquid. Now, which may not sound like probably the most thrilling factor on this planet, however think about, for instance, you are a high quality management engineer answerable for guaranteeing that the speed of filling bottles is constant. You must decide if this price is fixed or varies over time. To reply this query, you’d must course of every particular person body from the video file. However the place would you begin?
Creating the Segmentation Operate
With the purpose of measuring the liquid inside the container, I wanted a segmentation perform that may separate the liquid from the background. To make sure I develop a sturdy algorithm, I export just a few consultant frames to create and check a segmentation perform.
As soon as I am glad with my thresholding outcomes, I export this segmentation perform from the app as a “.m” file named “liquidMask”.
Test the Segmentation Operate
v = VideoReader(“liquidVideo.mp4”)
whereas hasFrame(v) % Loop by way of all frames
img = readFrame(v); % Learn a body
bw = liquidMask(img); % Apply the customized segmentation perform
montage({img,bw},‘BorderSize’,[20,20],‘BackgroundColor’,[0.5,0.5,0.5]);
The ensuing segmented video frames look nice! Additional morphological operations to raised section the area of curiosity are attainable, however to get a fast instance up and working they aren’t wanted.
Analyzing Liquid Proportion
Now I can transfer on to the ultimate half: calculating the proportion of the container that’s full of water at every body.
To get began, I must rewind the video to the start utilizing the CurrentTime property of the VideoReader.
Subsequent, I initialize two variables to retailer values for every body: the proportion of the container stuffed, and the time stamp.
percents = zeros(nFrames,1);
occasions = zeros(nFrames,1);
Now I’m prepared to make use of a for-loop to learn by way of the video, section every picture, and calculate the proportion of the container stuffed at every body. The share calculation is finished by counting the variety of pixels which can be full of liquid and dividing it by the entire variety of pixels.
for i = 1:nFrames % Loop by way of all frames
img = readFrame(v); % Learn a body
bw = liquidMask(img); % Create the binary masks utilizing the customized perform
liquidPart = nnz(bw); % Calculate the variety of liquid (true) pixels
perc = liquidPart/numel(bw); % Calculate proportion full of liquid
time = v.CurrentTime; % Mark the present time
percents(i) = perc; % Save the fill-percentage worth
occasions(i) = time; % Save the time stamp
Now that the info has been gathered, I plot the proportion of the container stuffed versus time to see how rapidly the liquid fills the container.
title(‘Proportion of Container Stuffed with Liquid vs Time’)
ylabel(‘Proportion Crammed’)
Take a look at that! The fill price is just not constant. From the plot, I see that the movement slowed down round 8 seconds, elevated at 15 seconds, and slowed once more at 20 seconds.
___
Thanks, Isaac!