How to convert number of pixels from multiple binary images into a single plot numerically?

2 次查看(过去 30 天)
I have binary images and I want to plot the number of black pixels from each images in numerical order of the filenames. Let's say that the numerical order acts as a timestamp for the progression of the total pixels
Thanks in advance

采纳的回答

Image Analyst
Image Analyst 2020-4-23
Try this:
clc; % Clear the command window.
fprintf('Beginning to run %s.m.\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
clear global;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
% Specify the folder where the files live.
myFolder = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
%------------------------------------------------------------------------------------------
% Can sort this in different ways:
% % Sort list by oldest to newest.
% dateNums = [theFiles.datenum];
% [dateNums, sortOrder] = sort(dateNums, 'ascend');
% theFiles = theFiles(sortOrder);
% Sort in alphanumeric order of the base file names
allFileNames = {theFiles.name};
[allFileNames, sortOrder] = sort(allFileNames);
theFiles = theFiles(sortOrder);
%------------------------------------------------------------------------------------------
% Process all files in a loop.
whitePixelCount = zeros(1, length(theFiles));
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
originalImage = imread(fullFileName);
subplot(1, 2, 1);
imshow(originalImage); % Display image.
caption = sprintf('Original Image : "%s"\n%d of %d', baseFileName, k, length(theFiles));
title(caption, 'FontSize', fontSize);
binaryImage = imbinarize(originalImage(:,:,1));
subplot(1, 2, 2);
imshow(binaryImage); % Display image.
caption = sprintf('Binary Image : %d of %d', k, length(theFiles));
title(caption, 'FontSize', fontSize);
% Count the number of white pixels.
whitePixelCount(k) = nnz(binaryImage);
drawnow; % Force display to update immediately.
end
hFig = figure;
bar(whitePixelCount, 1);
xlabel('Image Number', 'FontSize', fontSize);
ylabel('Count of White Pixels', 'FontSize', fontSize);
title('Count of White Pixels in Binarized Images', 'FontSize', fontSize);
grid on;
hFig.WindowState = 'maximized';
fprintf('Done running %s.m.\n', mfilename);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by