Making substacks from .h5 file

3 views (last 30 days)
Akua
Akua on 29 Feb 2024
Answered: Avadhoot on 14 Mar 2024
Hello!
I am trying to write a script to make substacks of brightfield movies I have saved as .h5 files(301 slides), average these substacks, then concatenate them to make a new movie of ~60 slides. I am aware of how to do this within ImageJ, however, I will need process many movies like this, and will be doing continued downstream analysis on these data in MATLAB anyway, so I am hoping to write a script to automate this. I am unsure of how to write a loop to make the substacks. I tried using the h5read command, but it appears to only work for picking out data within a slice. I am fairly new to MATLAB, so any help or suggestions would be much appreciated. TIA!

Answers (1)

Avadhoot
Avadhoot on 14 Mar 2024
Hi @Akua,
I see that you want to write a script to create substacks from a .h5 file and then average and concatenate them. There is no direct function to create substacks from a .h5 file but this can be done using a simple loop and the "h5read" function. You can refer to the below code to get an idea on how to do it.
% Define the path to your .h5 file
filePath = 'your_file_path_here.h5';
datasetName = '/dataset_name_here'; % Adjust this to the name of your dataset in the H5 file
% Read the size of the dataset without loading the data into memory
info = h5info(filePath, datasetName);
dataSize = info.Dataspace.Size; % Assuming the third dimension is frames
% Parameters
totalFrames = dataSize(3);
desiredSubstackFrames = 5; % Number of frames you want in each substack
averageFrames = totalFrames / desiredSubstackFrames; % Calculate how many frames to average
% Preallocate array for averaged substacks
averagedSubstacks = zeros(dataSize(1), dataSize(2), ceil(averageFrames));
% Loop to create substacks, average them, and store the result
for i = 1:desiredSubstackFrames:totalFrames
% Calculate end frame for the current substack
endFrame = min(i + desiredSubstackFrames - 1, totalFrames);
% Read the substack
substack = h5read(filePath, datasetName, [1, 1, i], [dataSize(1), dataSize(2), endFrame-i+1]);
% Average the substack
averagedSubstack = mean(substack, 3);
% Store the averaged substack
averagedSubstacks(:, :, ceil(i/desiredSubstackFrames)) = averagedSubstack;
end
You can modify the filepath and the dataset name as per your requirement. Here "h5read" is used to create substacks from the whole .h5 file.
If you want to save the processed data to a new .h5 file then you can use the below code:
newFilePath = 'processed_movie.h5';
newDatasetName = '/processed_dataset';
h5create(newFilePath, newDatasetName, size(averagedSubstacks), 'Datatype', 'double');
h5write(newFilePath, newDatasetName, averagedSubstacks);
For more information on the "h5read" function, refer to the below documentation link:
I hope it helps.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!