Counting and measuring size of particles flowing in a video

18 views (last 30 days)
I have mp4 videos of square/rectangular particles moving in and out of the video space and would like to count and measure their size through out the duration of the video. Please could you suggest how to do this?
  1 Comment
Kelechi Ndukwe-Ajala
Kelechi Ndukwe-Ajala on 4 Dec 2023
Edited: Kelechi Ndukwe-Ajala on 4 Dec 2023
I have attached an example video and my code thus far that is able to detect objects in the video but not particularly only crystals in focus.
I have a few aims to achieve:
1) How to detect only crystals within plane of focus?
2) How to differentiate between touching crystals during detection?
3) How to average bounding box data across multiple frames for a detected crystal?
4) How to combine bounding box data from each individual frame into one matrix as an output? Final output needed is the total crystal count and their individual sizes
clear;
clc;
filename = 'Trimmed video.mp4'; % To read the video into MATLAB
implay(filename); % Display the video
videoSource = VideoReader(filename);
% create a detector object
detector = vision.ForegroundDetector('NumTrainingFrames',1000);
% Perform blob analysis
blob = vision.BlobAnalysis('AreaOutputPort',false,'CentroidOutputPort',false,'BoundingBoxOutputPort',true,'MinimumBlobArea',200);
% Insert a border
shapeInserter = vision.ShapeInserter('BorderColor','White');
% Play results. Draw bounding boxes around moving crystals
videoPlayer = vision.VideoPlayer();
framecount=1;
while hasFrame(videoSource)
frame = readFrame(videoSource);
fgMask = detector(frame);
bbox = blob(fgMask);
out = shapeInserter(frame,bbox);
videoPlayer(out);
framecount=framecount+1;
end
release(videoPlayer);

Sign in to comment.

Answers (2)

Pratik
Pratik on 2 Nov 2023
Hi Kelechi,
In my understanding, you want to count and measure the size of square/rectangle particles in a video. To do the same you will have to first read the frames of the video and then process each frame using image processing technique to identify the squares/rectangles and measure their size.
  • To read the frame, you can use “VideoReader” function to get a videoreader object and then can use “readFrame” to read each frame of the video. Refer to the code snippet below for an example
v = VideoReader("video_path"); % path of the video
while hasFrame(v)
frame = readFrame(v);
end
  • Now for each frame you need to use the function “regionprops” which can measure properties such as area, centroid and bounding box, for each object (connected component) in the frame.
  • To count the number of squares/rectangles total number of centroids can be counted and to measure the size “BoundingBox” property can be used. It returns position and size of the smallest box containing the region, returned as a 1-by-(2*Q) vector, where Q is the image dimensionality.
Please refer to the following “VideoReader” documentation for more information about reading frames from video: https://in.mathworks.com/help/matlab/ref/videoreader.html
Please refer to the following “regionprops” documentation for more information about measuring properties of image regions: https://in.mathworks.com/help/images/ref/regionprops.html
Hope this helps!
  1 Comment
Kelechi Ndukwe-Ajala
Kelechi Ndukwe-Ajala on 4 Dec 2023
Edited: Kelechi Ndukwe-Ajala on 4 Dec 2023
Hello Pratik,
Your suggestions greatly helped to get me started. Please see my updated questions in the post

Sign in to comment.


Image Analyst
Image Analyst on 5 Dec 2023
The Computer Vision Toolbox has motion tracking capabilities.

Community Treasure Hunt

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

Start Hunting!