reading images from different folders
Show older comments
i have five folders,each containing 1000 images .i want to take each images from these folders (ie.first images from all folders and then second images etc)and find the average of these images like(if a,b,c,d,e,are first five images from five folders ,then e =a/10+/10+c/10+d/10+e/10) and stored the resultant images(averaged ) in a resultant forlder
2 Comments
Walter Roberson
on 5 Oct 2023
What is the "first" image of a folder? Do the images have the same name in each of the folders, or is the name in the other folders computable by knowing the name in one of the folders?
I ask because the order of files in a folder is poorly defined.
Mathieu NOE
on 9 Oct 2023
see this post
that might help you
Answers (2)
Mathieu NOE
on 9 Oct 2023
Edited: Mathieu NOE
on 9 Oct 2023
hello
maybe this ?
I created some "input" folders where the images are stored (those folders are supposed to have "in" in their names , change to your definition)
I also assumed :
- tif images (but you can change that as well)
- there is already an existing "out" folder to store the resulting images
my code also uses this Fex submission that you need to download and install in your path :
%NB : data files are not searched inside the main folder (yourpath) but in
% all subfolders (dirnames)
% if S is empty that means no data file (matching the file filter is in the searched subfolder)
%% define path
yourpath = pwd; % or your specific path
list=dir(yourpath); %get info of files/folders in current directory
isfile=~[list.isdir]; %determine index of files vs folders
dirnames={list([list.isdir]).name}; % directories names (including . and ..)
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames))); % remove . and .. directories names from list
% "in" folders
dirnames_in = dirnames(contains(dirnames,'in'));
% output directory
out_dir = fullfile(yourpath,'\out'); % from current directory to sub directory \out
%% Loop on each folder
for ci = 1:length(dirnames_in) %
fileDir = char(dirnames_in(ci)); % current directory name
S = dir(fullfile(fileDir,'*.tif')); % get list of data files in directory according to name structure '*.tif'
S = natsortfiles(S); % sort file names into natural order (what matlab does not) , see FEX :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
%% Loop inside folder
for k = 1:length(S) % read data
tmp{ci,k} = imread(fullfile(fileDir, S(k).name));
end
end
%% do summation along folders
[m,n] = size(tmp);
for k = 1:n % loop over images files
out = double(tmp{1,k}); % first image of k th folder, need to convert to double to avoid saturation during summation process
for ci = 2:m % loop over folders (same image)
out = out + double(tmp{ci,k}); % need to convert to double to avoid saturation during summation process
end
image_avg{k} = uint8(out/m); % here we divide by m to have the average and convert back to int format
figure(k),imagesc(image_avg{k});
axis equal
% save to out folder
filename = ['out_' num2str(k,4) '.tif'];
imwrite(image_avg{k},fullfile(out_dir,filename));
end
1 Comment
Mathieu NOE
on 11 Dec 2023
hello again
do you mind accepting my answer (if it has fullfiled your expectations ) ? tx
Image Analyst
on 11 Dec 2023
0 votes
See attached demo. Adapt as needed.
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!