Counting Total Number of Pixels by Color in a Segmented Image

9 visualizaciones (últimos 30 días)
Hello I have mutiple segmented images that I would like to count the number of white, red, and blue pixels in each image using a for loop. I would like to store the number of red, white, and blue pixels associated with each image in an array along with the filename of each image. I can do each image manually, but I would like to make the process faster. I count the black pixels to make sure that the sum total of pixels assigned is close to 90,000, since each image is 300x300 and some pixels might be hard to distguinsh. Any help would be greatly appreicated. Thanks
rgbImage = imread('BioM Edge 576 Gen 5 Infl Rnge Start Segmented.png');
%rgbImage = imread('BioM Edge 576 Gen 10 Infl Rnge Start Segmented.png');
%rgbImage = imread('BioM Edge 576 Gen 15 Infl Rnge Start Segmented.png');
%rgbImage = imread('BioM Edge 576 Gen 20 Infl Rnge Start Segmented.png');
%rgbImage = imread('BioM Edge 576 Gen 25 Infl Rnge Start Segmented.png');
%rgbImage = imread('BioM Edge 576 Gen 30 Infl Rnge Start Segmented.png');
%%
BluePixels = rgbImage(:,:,1) == 0 & rgbImage(:,:,2) == 0 & rgbImage(:,:,3) == 255;
numBluePixels = sum(BluePixels(:));
%%
RedPixels = rgbImage(:,:,1) == 255 & rgbImage(:,:,2) == 0 & rgbImage(:,:,3) == 0;
numRedPixels = sum(RedPixels(:));
%%
WhitePixels = rgbImage(:,:,1) == 255 & rgbImage(:,:,2) == 255 & rgbImage(:,:,3) == 255;
numWhitePixels = sum(WhitePixels(:));
%%
BlackPixels = rgbImage(:,:,1) == 0 & rgbImage(:,:,2) == 0 & rgbImage(:,:,3) == 0;
numBlackPixels = sum(BlackPixels(:));

Respuesta aceptada

Image Analyst
Image Analyst el 1 de Jun. de 2020
Editada: Image Analyst el 1 de Jun. de 2020
In the for loop over all image files, just index the variables:
numBluePixels(k) = sum(BluePixels(:));
etc.
To check that the sum of blue, red, white, and black pixels is to within some tolerance of the total number of pixels, just sum them up at the bottom of the loop
tolerance = 0.05 * numel(BluePixels); % 5% of the total number of pixels.
if numBluePixels(k) + numWhitePixels(k) + numRedPixels(k) + numBlackPixels(k) > numel(BluePixels) - tolerance
% The sum is close enough to 90,000
else
% The sum is not close enough to 90,000 because some pixels were not classified as one of those colors.
end
  5 comentarios
Image Analyst
Image Analyst el 2 de Jun. de 2020
Vance, try this more robust, expanded version:
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 = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
folder = pwd; % or c:\wherever you want
filePattern = fullfile(folder, '*.png');
fileList = dir('*.png');
numberOfImageFiles = size(fileList,1);
numberAnalyzed = 0;
% Make array Pixel_Totals to hold all the sums
Pixel_Totals = zeros(numberOfImageFiles,4);
for k = 1:numberOfImageFiles
thisFileName = fullfile(fileList(k).folder, fileList(k).name);
fprintf('Processing %s\n', thisFileName);
rgbImage = imread(thisFileName);
if size(rgbImage, 3) ~= 3
% Skip gray scale or binary images.
fprintf('Skipping %s\n because it is not a true color RGB image.\n', thisFileName);
continue;
end
subplot(1, 2, 1);
imshow(rgbImage);
axis('on', 'image');
title(fileList(k).name, 'FontSize', fontSize, 'Interpreter', 'none');
BluePixels = rgbImage(:,:,1) == 0 & rgbImage(:,:,2) == 0 & rgbImage(:,:,3) == 255;
numBluePixels(k) = sum(BluePixels(:));
RedPixels = rgbImage(:,:,1) == 255 & rgbImage(:,:,2) == 0 & rgbImage(:,:,3) == 0;
numRedPixels(k) = sum(RedPixels(:));
WhitePixels = rgbImage(:,:,1) == 255 & rgbImage(:,:,2) == 255 & rgbImage(:,:,3) == 255;
numWhitePixels(k) = sum(WhitePixels(:));
BlackPixels = rgbImage(:,:,1) == 0 & rgbImage(:,:,2) == 0 & rgbImage(:,:,3) == 0;
numBlackPixels(k) = sum(BlackPixels(:));
Pixel_Totals(k,:) = [numRedPixels(k), numWhitePixels(k), numBluePixels(k), numBlackPixels(k)];
fprintf(' Class Counts: %d %d %d %d\n', numRedPixels(k), numWhitePixels(k), numBluePixels(k), numBlackPixels(k));
numberAnalyzed = numberAnalyzed + 1;
% Display as a bar chart.
subplot(1, 2, 2);
bar(Pixel_Totals(k,:));
grid on;
drawnow;
end
Pixel_Totals
fprintf('Done analyzing %d RGB images out of %d total image files (RGB & gray scale).\n', numberAnalyzed, numberOfImageFiles);
% Plot the mean counts
subplot(1, 2, 2);
meanClassCounts = mean(Pixel_Totals, 1)
bar(meanClassCounts);
grid on;
caption = sprintf('Mean Class Counts\n(Averaged over %d RGB Images)', numberAnalyzed);
title(caption, 'FontSize', fontSize);
xlabel('Color Class', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
Vance Blake
Vance Blake el 2 de Jun. de 2020
Wow enhanced is a bit of an understatement this^ is why you're an MVP on here. I finished up manually counting them a couple of hours ago, but I am really glad to have this version for the next batch. Thank you so much for your help! A lot of your other codes have been instructive for me.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by