How to calculate median matrix of many .tif files with MATLAB?

8 views (last 30 days)
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
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?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Nov 2023
format long g
master_size = [87, 207];
unzip Test.zip
Warning: Cannot open file "" for reading.
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(:))]
ans = 1×2
1.0e+00 * -1.06638e+13 7.329353e+11
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
Walter Roberson on 2 Nov 2023
You might be interested in standardizeMissing
format long g
master_size = [87, 207];
unzip Test.zip
Warning: Cannot open file "" for reading.
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(:))]
ans = 1×2
45.09927 214.264
image(image_median)
Rafat
Rafat on 3 Nov 2023
Hello Walter,
I went through the script, then I understand the wierd values other than the 1- "FillValues" and 2- the normal range of data from 0-1000
"master_size = [87, 207];"
this produce the wierd values in .tif files if rows> 87 and columns > 207; so to that end, I tested
"master_size = [88, 208];" for .tif files if rows= 88 and columns = 208; the wierd values disappierd, so I think we need to deliet the rows > 87 and the columns > 207 then apply from "master_size = [87, 207];" to the end of script.
I appriciat any help of that!!
Cheers,
rafat

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!