Color objects with areas larger than a set value

7 views (last 30 days)
I 1960 images like this. I am trying to identify the regions where the areas have gotten larger than a set value by coloring them in. For example in the fourth Image the bottom cluster of circles would have a large area and that would be colored in. I pasted the code I am trying to implement below. Is there a way to do this using a loop or would a mask be needed first?
video = VideoWriter('Example.avi')
open(video);
for img = 1:1000
file = strcat('bw_frame', num2str(img),'.jpeg');
I = imread(file);
I = imbinarize(I);
[L, n] = bwlabel(I);
stats = regionprops(L, 'Area');
for count = 1:length(n)
val = stats.Area;
if val(count) > 1000
rgb2 = label2rgb(L);
end
end
writeVideo(video,rgb2)
disp(img)
end
close(video);

Accepted Answer

DGM
DGM on 8 Apr 2021
I'm not familiar with the video IO tools, but isolating/coloring the large objects should be easy enough
I = imread('dots.jpg');
I = im2bw(I); % imbinarize isn't in my version
L = bwlabel(I);
stats = regionprops(L, 'Area');
maxarea=700;
hilitecolor=[0.6 0 0.8];
biglab = find([stats.Area]>maxarea)
bigmap = ismember(L,biglab);
outpict = repmat(I.*~bigmap,[1 1 3]) ...
+ bsxfun(@times,bigmap,reshape(hilitecolor,[1 1 3]));
I'm sure there are some efficiency tweaks that can still be made to this, but it works.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!