Pixel values from multiple images

Hello Everyone,
I have a bunch of greyscale images. All are resized 50 * 50. All are located in the same folder.
I want to extract the matrix of all pixel values of the images into multipe exel spreadsheets (one sheet for each image).
How could I do it automatically for all images at once?
thanks

 Accepted Answer

% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
xlsFullFileName = fullfile(myFolder, 'Results.xlsx') % Whatever....
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, 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()
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
[~, sheetName, ext] = fileparts(baseFileName);
writematrix(imageArray, xlsFullFileName, sheetName);
end

1 Comment

Thank you for the code.
I am receiving this error:
Error using writematrix (line 191)
Wrong number of arguments. A filename must be provided when supplying additional parameters, and each parameter name must
be followed by a value.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!