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);