Storing image properties in loop
4 views (last 30 days)
Show older comments
Mathew Smith
on 12 Sep 2022
Commented: Mathew Smith
on 12 Sep 2022
Hi,
I would like to ask you to help me to store image name and map in for-end loop. Here they are in square brackets [X, mapX] but how to store for each image name the properties in different letter?
a = dir(fullfile(yourfolder, 'img_??.jpg'));
for i = 1:length(a)
imgname(i) = char(a(i.name)
[X, mapX] = rgb2ind(imread(imgname),256);
end ;
Best regards
Michal
0 Comments
Accepted Answer
Image Analyst
on 12 Sep 2022
@Mathew Smith do it this more robust way:
imageFolder = pwd;
fileListing = dir(fullfile(imageFolder, 'img_*.jpg'));
if isempty(fileListing)
warningMessage = sprintf('No jpg image files found in %d.\n', imageFolder);
uiwait(warndlg(warningMessage));
return;
end
allFileNames = {fileListing.name}'
numFiles = numel(allFileNames)
counter = 0;
ca = cell(numFiles, 2); % Preallocate space.
for k = 1 : numFiles
thisFileName = fullfile(imageFolder, allFileNames{k});
fprintf('Processing file #%d of %d : "%s"\n', k, numFiles, thisFileName);
[rgbImage, embeddedColorMap] = imread(thisFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% If it's not RGB, skip it.
if numberOfColorChannels < 3
fprintf(' Skipping "%s" because it is not true color RGB.\n', thisFileName);
continue;
end
[indexedImage, computedColorMap] = rgb2ind(rgbImage,256);
% Save these in a cell array
counter = counter + 1;
ca{counter, 1} = indexedImage;
ca{counter, 2} = computedColorMap;
end
% Crop in case we skipped any
if counter < numFiles
ca = ca(1 : counter);
end
More Answers (0)
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!