How to add color to the specified blob in a binary image according to the aspect ratio
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jiawei Liu
il 24 Gen 2022
Commentato: Image Analyst
il 26 Gen 2022
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.
0 Commenti
Risposta accettata
Image Analyst
il 25 Gen 2022
Modificato: Image Analyst
il 25 Gen 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 Commenti
Image Analyst
il 26 Gen 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.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!