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