How to calculate median matrix of many .tif files with MATLAB?
8 views (last 30 days)
Show older comments
Hi
I have for example 15 .tif files, some pixels with "NaN". How to find a matrix of the median of the 15 tif files
Is it possible?
to be more precice
The following link to subsample of the data:
input: 15 .tif files x m rows x n columns
output: so I want the out put be 1 median x m rows x n columns
Cheers
11 Comments
Walter Roberson
on 2 Nov 2023
If you have 120 files in the directory then how do you want to select the 15 of them you want to process?
Accepted Answer
Walter Roberson
on 2 Nov 2023
format long g
master_size = [87, 207];
unzip Test.zip
files = dir('*.tif');
filenames = fullfile({files.folder},{files.name});
N = numel(files);
for ii = 1:N
files(ii).data = imresize(imread(filenames{ii}), master_size);
end
datablock = cat(3, files.data);
image_median = median(datablock, 3, 'omitnan');
[min(image_median(:)), max(image_median(:))]
image(image_median)
After that you want to write the median out to a tiff, and you probably want the same data range (rather than converting the image to uint8). imwrite() cannot itself write single precision tiff, so you need the Tiff library. More information is at https://www.mathworks.com/matlabcentral/answers/7184-how-can-i-write-32-bit-floating-point-tifs-with-nans . Or you could use the File Exchange contribution https://www.mathworks.com/matlabcentral/fileexchange/30519-export-image-to-tif-or-tiff-file-of-selected-data-type which you could install using the Add-On Explorer
4 Comments
Walter Roberson
on 2 Nov 2023
You might be interested in standardizeMissing
format long g
master_size = [87, 207];
unzip Test.zip
files = dir('*.tif');
filenames = fullfile({files.folder},{files.name});
N = numel(files);
for ii = 1:N
thisimg = imread(filenames{ii});
minval = min(thisimg(:));
if minval < -1e12
thisimg = standardizeMissing(thisimg, minval);
end
files(ii).data = imresize(thisimg, master_size);
end
datablock = cat(3, files.data);
image_median = median(datablock, 3, 'omitnan');
[min(image_median(:)), max(image_median(:))]
image(image_median)
More Answers (0)
See Also
Categories
Find more on Other Formats 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!