How can I quantify blobs in multiple images at once?

1 回表示 (過去 30 日間)
Stan Wissink
Stan Wissink 2021 年 12 月 7 日
回答済み: yanqi liu 2021 年 12 月 8 日
Hello all,
Using, color thresholding from the Image Processing Toolbox, I want to analyse the area of different blobs and calculate the total amount of blobs.
This is my script:
originalImage = uigetfile(".bmp");
originalGray = rgb2gray(originalImage);
Binary = createMask(originalImage);
labeledImage = bwlabel(Binary, 8);
Measurements = regionprops(labeledImage, originalGray, 'Area');
numberOfBlobs = size(Blobs, 1)
function [BW,maskedRGBImage] = createMask(RGB)
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 255.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 101.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 102.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
This works fine for analysing 1 image at a time, but since I have hundres of images to process, I want to change my script to handle multiple images (78) at once and - if possible - store the results in one variable.
Thank you for your time and effort!

採用された回答

yanqi liu
yanqi liu 2021 年 12 月 8 日
yes,sir,may be provide the filename information,such as
clc; clear all; close all;
% handle multiple images (78) at once
Measurements = [];
numberOfBlobs = [];
for i = 1 : 78
% use the filename
fi = sprintf('%d.jpg', i);
originalImage = imread(fi);
originalGray = rgb2gray(originalImage);
Binary = createMask(originalImage);
labeledImage = bwlabel(Binary, 8);
Measurements{i} = regionprops(labeledImage, originalGray, 'Area');
numberOfBlobs(i) = size(Blobs, 1);
end
function [BW,maskedRGBImage] = createMask(RGB)
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 255.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 101.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 102.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

その他の回答 (0 件)

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by