Selecting and loading files from multiple subfolders using uigetdir

25 次查看(过去 30 天)
Hello everyone!
So, I pieced together a code that allows me to automatically segment images containing cells, in order to analyze them in bulk. So far, I am able to take multiple images in one given directory (in one subfolder) and analyze them in one shot. What I would like to do though is extend it so I can do multiple subfolders (all contained in one parent folder), have each subfolder analyzed separately (I guess it could be in a 'for' loop or something) and, at the end, pool up all the data in one graph.
Right now my folder hierarchy is a bit like: Donor ==> donor_800_ROI1, donor_800_ROI2, ... ==> Channel 2, Channel 3 ==> [the actual images]. I would like to select the Donor folder using uigetdir, then it goes through ROI1, separates Channel 2 from Channel 3, then add together all images of Channel 2 in one variable (image), all images from Channel 3 in a second variable, analyze, etc. then move on to ROI2, rince and repeat. Ideally I would want one variable per channel per ROI, I will pool them together for the graph at the very end of my code.
The code to search files and load them from one subfolder, so far, is the following:
ch2 = uigetdir('D:\Images\','Choose the folder containing the images to be analyzed.'); % Here I select Channel 2 of one ROI
if ch2 == 0
return
else
ima_CH2 = loadImages(ch2); % Function listed below for reference
end
ch3 = uigetdir('D:\Images\','Choose the folder containing the images to be analyzed.'); % Select Channel 3 of the same ROI
if ch3 == 0
return
else
ima_CH3 = loadImages(ch3); % Function listed below for reference
end
% My code for analysis goes here afterwards, I don't think it is relevant
% to my question right now; if it is I will come back to complete it.
function CTRL = loadImages(ctrlPath)
% Loads all images in a subfolder and adds them up into one single variable
% (image matrix).
ds = imageDatastore(ctrlPath);
NbCTRL = length(ds.Files);
CTRL = zeros(512,1024,NbCTRL);
for images = 1:NbCTRL
CTRL(:,:,images) = imread(ds.Files{images});
end
CTRL = sum(CTRL,3);
end
Thanks in advance!
LP
P.S.: For what it's worth, I work on R2018a, so I don't know if functions involved here were subsequently changed from that version.
EDIT: I just remembered I have another function to load images, that's slightly different, it might be worth putting it here to see which is more efficient. Do note however that I take data from two separate microscopes, one gives me .tiff files and the other .czi files. The part already mentioned up here is for the .tiff files, here is the one I made for the .czi files:
function [NbIma, images] = loadimages(FilenameFolder)
Files = dir(fullfile(FilenameFolder,'800_ROI1_IM*.czi')); % Excitation wavelength here!
NbIma = length(Files);
images = zeros(512,512,NbIma);
for iter = 1:NbIma
FileName = [FilenameFolder,'\',Files(iter).name];
data = bfopen(FileName);
imdata = data{1}; % Extracts cell array of data of the image
images(:,:,iter) = imdata{1};
end
end
I don't know which of the two 'loadimages' function is the more efficient one, but ideally if one can work with both .tiff and .czi files it would be awesome. Although both will be analyzed separately, with different thresholds, etc.

回答(1 个)

yanqi liu
yanqi liu 2021-9-28
sir,may be use the follow function
function file_list = get_all_files(input_folder)
dir_data = dir(input_folder);
dir_index = [dir_data.isdir];
file_list = {dir_data(~dir_index).name}';
if ~isempty(file_list)
file_list = cellfun(@(x) fullfile(input_folder,x),...
file_list,'UniformOutput',false);
end
sub_dirs = {dir_data(dir_index).name};
valid_index = ~ismember(sub_dirs,{'.','..'});
for k = find(valid_index)
next_dir = fullfile(input_folder,sub_dirs{k});
file_list = [file_list; get_all_files(next_dir)];
end

类别

Help CenterFile Exchange 中查找有关 Import, Export, and Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by