How to read and process multiple images to calculate its pixels and tabulate the results?

11 views (last 30 days)
I have a few thousand rail images to read, cropped and calculate its pixels. I have the following codes but i want it to run on multiple images and to get the results of the pixels for each image as well to speed up this process. Any help is appreciated thanks!! :)
clc; %Clear the command window
close all; %Close all Figures
imtool close all; %Close all imtool figures
clear; %Erase all existing variable
workspace; %Make sure the workspace is showing
fontsize = 20;
[file,path] = uigetfile('*');
if isequal(file,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path,file)]);
end
Img=imread(fullfile(path,file));
cropped_Img = Img(290:945,1:1919,:);
%Converting rgb to grayscale
grayscale = rgb2gray(cropped_Img);
grayscale2 = histeq(grayscale);
%Converting grayscale to binary
binary=imbinarize(grayscale2);
%Calculate black and white pixels
numwhitepixels = sum(binary(:));
numblackpixels = sum(~binary(:));
totalpixels = numwhitepixels + numblackpixels;
Percentageofblack = (numblackpixels/totalpixels)*100;

Answers (3)

yanqi liu
yanqi liu on 1 Nov 2021
clc; %Clear the command window
close all; %Close all Figures
imtool close all; %Close all imtool figures
clear; %Erase all existing variable
workspace; %Make sure the workspace is showing
fontsize = 20;
% 读取文件
[filename, pathname, ~] = uigetfile( ...
{ '*.bmp;*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*', 'all (*.*)'}, ...
'selected', ...
'MultiSelect', 'on', ...
'');
filePath = [];
if isequal(filename, 0) || isequal(pathname, 0)
return;
end
if iscell(filename)
for i = 1 : length(filename)
filePath{i} = fullfile(pathname, filename{i});
end
else
filePath{1} = fullfile(pathname, filename);
end
for i = 1 : length(filePath)
Img=imread(filePath{i});
cropped_Img = Img(290:945,1:1919,:);
%Converting rgb to grayscale
grayscale = rgb2gray(cropped_Img);
grayscale2 = histeq(grayscale);
%Converting grayscale to binary
binary=imbinarize(grayscale2);
%Calculate black and white pixels
numwhitepixels(i) = sum(binary(:));
numblackpixels(i) = sum(~binary(:));
totalpixels(i) = numwhitepixels(i) + numblackpixels(i);
Percentageofblack(i) = (numblackpixels(i)/totalpixels(i))*100;
end
numwhitepixels
numblackpixels
totalpixels
Percentageofblack

Rishabh Singh
Rishabh Singh on 9 Nov 2021
The solution provided by @yanqi liu should extend your code for multiple images. As for performance improvement you could use "png" extension for your image dataset.
tic; imread('jpg.jpg'); toc;
% Elapsed time is 0.588476 seconds.
tic; imread('tif.tif'); toc;
% Elapsed time is 0.730959 seconds.
tic; imread('png.png'); toc;
% Elapsed time is 0.315704 seconds.
The above timing are for image of dimension [5500x5500] image.
Hope this helps.

Image Analyst
Image Analyst on 9 Nov 2021
You can speed it up by getting rid of the call to imhisteq(). There is no need to histogram equalize the image. It should not affect how the binary image is computed. Histogram equalization is really a mostly worthless function that is almost never needed in image analysis. (I challenge anyone to prove me wrong or give a counter example). It's basically a non-linear version of imadjust() but gives unnatural looking images. Thresholding is based on the histogram and all that does is move the bins around leaving the same overall shape and so the threshold will be the same.
You can also do the counts faster by using nnz instead of summing up all the pixels in the entire image. Instead of
%Calculate black and white pixels
numwhitepixels = sum(binary(:));
numblackpixels = sum(~binary(:));
totalpixels = numwhitepixels + numblackpixels;
do this:
%Calculate black and white pixels
numwhitepixels = nnz(binary);
totalpixels = numel(binary);
numblackpixels = totalpixels - numwhitepixels;
To process a sequence of images, see the FAQ:

Community Treasure Hunt

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

Start Hunting!

Translated by