Change Color of Segmented object based on Solidity

4 views (last 30 days)
Shahil Khan
Shahil Khan on 10 Aug 2021
Commented: Shahil Khan on 11 Aug 2021
I have used regionprops to calculate the Convexhull of each shape in the image shown and then drawn the region in red around it. I want to color each blob differently according to the solidity value and then display the colormap on the figure itself on the side. Essentially, the lower the value, the darker I want it to be.
I have thought about implementing it manually through if statements but then how would I display the colormap on the side?
Much appreciated!
HOW IT LOOKS CURRENTLY:
HOW I WANT IT TO LOOK:
stats = regionprops(mask,{...
'Centroid',...
'MajorAxisLength',...
'MinorAxisLength',...
'Orientation', ...
'Circularity', ...
'ConvexHull', ...
'Solidity', ...
'Eccentricity', ...
'BoundingBox', 'Area' })
figure , imshow(img)
hold on
for k = 1 : size(stats)
tempHull = stats(k).ConvexHull;
tmpSize = size(tempHull);
x = tempHull(:, 1);
y = tempHull(:,2);
tempSolid = round(stats(k).Solidity, 2);
fill(x, y, fillColor);
plot(x, y, 'LineWidth', 1, 'Color', 'r');
text(stats(k).Centroid(1), stats(k).Centroid(2), num2str(tempSolid), 'FontSize', 12);
end
hold off
title("Solidity", 'FontSize', 18);

Accepted Answer

DGM
DGM on 10 Aug 2021
I don't have a good example image, so I'll just use this:
mask = rgb2gray(imread('sources/blobs.png'))>10;
stats = regionprops(mask,{...
'Centroid',...
'MajorAxisLength',...
'MinorAxisLength',...
'Orientation', ...
'Circularity', ...
'ConvexHull', ...
'Solidity', ...
'Eccentricity', ...
'BoundingBox', 'Area' });
% no need to use imshow(); it just complicates colormap handling
hold on
for k = 1 : size(stats)
tempHull = stats(k).ConvexHull;
tmpSize = size(tempHull);
x = tempHull(:, 1);
y = tempHull(:,2);
tempSolid = round(stats(k).Solidity, 2);
% fill color is solidity value
fill(x, y, stats(k).Solidity);
plot(x, y, 'LineWidth', 1, 'Color', 'r');
text(stats(k).Centroid(1), stats(k).Centroid(2), num2str(tempSolid), ...
'FontSize', 12, 'horizontalalignment','center');
end
hold off
set(gca,'ydir','reverse','xtick',[],'ytick',[])
axis equal
box on
xlim([0 size(mask,2)])
ylim([0 size(mask,1)])
colorbar
colormap(summer)
At that point, all you need to do is generate the desired colormap and apply it using colormap() as shown. You might need to tweak things with caxis().

More Answers (0)

Community Treasure Hunt

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

Start Hunting!