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

2 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2022-1-25
编辑:Image Analyst 2022-1-25
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 个评论
Jiawei Liu
Jiawei Liu 2022-1-26
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 2022-1-26
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by