How to add color to the specified blob in a binary image according to the aspect ratio

2 views (last 30 days)
For example, images with an aspect ratio greater than 2 add red, and images with an aspect ratio less than 2 add green.
I can't find a function about adding color to an area in between. Here is my graph, I want to make the circle red and the bar in the middle green.

Accepted Answer

Image Analyst
Image Analyst on 25 Jan 2022
Edited: Image Analyst on 25 Jan 2022
Try this (untested):
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
grayImage = imread('blobs.png');
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
subplot(3, 2, 1);
imshow(grayImage);
title('Gray Image', 'FontSize', fontSize);
axis('on', 'image')
% Binarize the image.
mask = grayImage > 200;
% Delete those blobs that are not completely in the field of view.
mask = imclearborder(mask);
subplot(3, 2, 2);
imshow(mask);
title('Mask Image', 'FontSize', fontSize);
% Label each blob.
[labeledImage, numBlobs] = bwlabel(mask);
props = regionprops(mask, 'MajorAxisLength', 'MinorAxisLength')
aspectRatios = [props.MajorAxisLength] ./ [props.MinorAxisLength]
roundBlobsLabels = aspectRatios <= 2;
roundBlobs = ismember(labeledImage, find(roundBlobsLabels));
subplot(3, 2, 3);
imshow(roundBlobs);
title('Round Blobs', 'FontSize', fontSize);
axis('on', 'image')
wormBlobsLabels = aspectRatios > 2;
wormBlobs = ismember(labeledImage, find(wormBlobsLabels));
wormBlobs = mask & ~roundBlobs;
subplot(3, 2, 4);
imshow(wormBlobs);
title('Worms', 'FontSize', fontSize);
axis('on', 'image')
black = zeros(size(roundBlobs), 'uint8');
rgbImage = cat(3, uint8(255 * wormBlobs), uint8(255 * roundBlobs), black);
subplot(3, 2, 5:6);
imshow(rgbImage);
title('Composite : Worms = red, Round = Green', 'FontSize', fontSize);
  7 Comments
Jiawei Liu
Jiawei Liu on 26 Jan 2022
This is the final result I want, you removed the two circles on the left.This is a binary image, and I want to label the bacteria in the middle green and the red blood cells on the sides red. I have tried using area calculation, but the irregular circle area cannot be calculated. So I asked: Is it possible to use the aspect ratio to distinguish bacteria from red blood cells. You deleted the circle on the left. Not the result I want.
Image Analyst
Image Analyst on 26 Jan 2022
When I calculated the aspect ratio, the slivers on the edges did not have the aspect ratio like you'd expect for a circle, because they're not a circle. They had high aspect ratio as expected. It's extremely common in image analysis to get rid of blobs touching the border because you do not have the entire blob so any measurements you make from a partial blob will not be the same as if you had the complete blob available. If you really want to identify blobs from the boundary as circular or not, it's possible but a lot more complicated, so it's generally not done. It's easier just to take some more sample images and throw out partial blobs.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!