Skip to content

Instantly share code, notes, and snippets.

@erjel

erjel/convert_tif_folders.m Secret

Created May 6, 2021
Embed
What would you like to do?
%% Copyright (c) 2021 Eric Jelli MIT License
%% Tested with MATLAB R2019a
% Downloaded BiofilmQ MATLAB files from
% https://drescherlab.org/data/biofilmQ/BiofilmQ.zip
biofilQ_download_folder = 'C:\Users\jelli\src\BiofilmQ';
input_folder = 'C:\Users\jelli\Sample_data';
output_folder = 'C:\Users\jelli\Sample_data_converted';
addpath(genpath(fullfile(biofilQ_download_folder, 'includes')));
filelist = dir(fullfile(input_folder, '**', '*.tif'));
for i = 1:numel(filelist)
file_name = filelist(i).name;
file_path = fullfile(filelist(i).folder, file_name);
options = {};
no_cmd_output = true;
im = double(imread3D(file_path, options, no_cmd_output));
%% Get information for new new_file_name
Nz = size(im, 3);
% implement a regex pattern (or something simmilar to find frame number)
% Here: The files are labeled "imageXXX.tif" where XXX stands for the
% frame number, so the regexp looks for the substring 'image' and
% assumes that all following digits '\d+' are the frame number
look_ahead_pattern = 'image';
pattern = sprintf('(?<=%s)\\d+', look_ahead_pattern);
frame = regexp(file_name, pattern, 'match');
new_file_name = sprintf('image_frame%06d_Nz%d.tif', str2double(frame{1}), Nz);
proj = sum(im, 3);
im(:, :, 2:end+1) = im;
im(:, :, 1) = proj/ max(im(:))*(2^8-1);
% Bonus: recreate the input folder structure in the output folder
root_folder_splitted = split(input_folder, filesep);
root_folder_last = root_folder_splitted{end};
file_path_splitted = split(file_path, filesep);
common_folder_id = find(strcmp(file_path_splitted, root_folder_last), 1);
relative_folder = fullfile(file_path_splitted{common_folder_id+1:end-1});
new_file_folder = fullfile(output_folder, relative_folder);
if ~isfolder(new_file_folder)
mkdir(new_file_folder)
end
fprintf([ ...
'Input file: %s\n', ...
'Output file: %s\n'], ...
file_path, ...
fullfile(new_file_folder, new_file_name) ...
);
imwrite3D(im, fullfile(new_file_folder, new_file_name),options, no_cmd_output);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment