I need to show the area of objects found using bwconncomp for successive frames

2 views (last 30 days)
I have a series of frames, and I am counting the number of objects in these frames. However, I also need to know the area of the object(s) throughout the frames. The objects are being counted correctly, and using region props I am finding the area, however I am unsure of what the output is showing. When I just used 'stats = regionprops(cc,area);' the output was a 1x154 structure. There are 154 objects counted in the last frame, so I assume this is giving me the area of the different objects in the last frame. Therefore I also used 'areaOfObject(k) = stats.Area;' to attempt to get the area of the different objects in the different frames. This returned a 1x99 double (there are 99 frames). There is only one value for the area here for each frame, and so at first I thought this was giving me the total area of objects in each frame, however the values are considerably smaller than the values of areas from each frame added together. Can anyone suggest what the output given here is, and how I can get the different areas of objects in each frame? I will eventually need to plot the area pf objects vs the frames. I have attached the code below; the majority is dealing with the frames in order to be analysed so can be ignored, but I have left it in for relevance.
folder = fullfile('Folder1\Folder2\Frames');
filePattern = fullfile(folder, '*.jpg');
fileList = dir(filePattern);
numberOfFiles = numel(fileList);
subtractionFileName = 'subtractexample1.jpg';
if ~isfile(subtractionFileName)
errorMessage = sprintf('Subtraction image not found:\n%s', subtractionFileName);
uiwait(errordlg(errorMessage));
return;
end
backgroundImage = imread(subtractionFileName);
for k = 1:numberOfFiles
baseFileName = fileList(k).name;
if contains(baseFileName, 'subtract', 'IgnoreCase', true)
continue;
end
fullFileName = fullfile(fileList(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
thisImage = imread(fullFileName);
subtractionImage{k} = backgroundImage - thisImage;
imshow(subtractionImage{k}, [])
caption = sprintf('Image #%d of %d : %s', k, numberOfFiles, baseFileName);
title(caption, 'Interpreter', 'none');
drawnow;
end
for k = 1:numberOfFiles
graySubtractedImage=rgb2gray(subtractionImage{k});
binaryImage = graySubtractedImage>114;
binaryImage = imclearborder(binaryImage);
binaryimage = bwareaopen(binaryImage,75);
cc = bwconncomp(binaryimage);
blobCountsPerFrame(k) = cc.NumObjects;
stats = regionprops(cc, "Area");
areaOfObject(k) = stats.Area;
imshow(binaryImage);
drawnow;
end

Accepted Answer

Walter Roberson
Walter Roberson on 16 Aug 2022
Edited: Walter Roberson on 16 Aug 2022
areaOfObject{k} = [stats.Area];
You have multiple objects per frame, so stats is a nonscalar structure. stats.Area would then be structure expansion, giving one output for each element of stats . You need to collect them all together into a vector. Then because you have a vector, you cannot assign it to a scalar location areaOfObject(k) so you need to use a cell array for that.
I recommend that you also collect Centroid information, as the only way to follow objects from frame to frame is by looking to see which ones are where.
There is an object tracking demo in Computer Vision toolbox that tracks centroids, and I think also handles velocity estimation to predict where the object will be next (this helps distinguish multiple moving objects.)
  4 Comments
Andrew Palmer
Andrew Palmer on 16 Aug 2022
Thanks again. Everything there works and thats exactly what I need. And yes, I am now tracking the centroids too which is definitely going to be useful, so thank you for that suggestion too.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!