Modifying a TIFF file and saving it to a new one with the same size

Good day everyone,
I'm stucked with this code. I need to load a bounch of TIF files, merge them, and finally save everything to a new file.
However, the resulting file is smaller (in size) than the original one when I load it with another program, thus usless for my purpose.
The original files that I need to process are too large to attach them here. Anyway, when I "imfinfo" one of them what I get is:
Filename: 'C:\Users\usr1\Desktop\w1.mccd'
FileModDate: '17-feb-2021 18:46:59'
FileSize: 8392704
Format: 'tif'
FormatVersion: []
Width: 2048
Height: 2048
BitDepth: 16
ColorType: 'grayscale'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 16
Compression: 'Uncompressed'
PhotometricInterpretation: 'BlackIsZero'
StripOffsets: 4096
SamplesPerPixel: 1
RowsPerStrip: 2048
StripByteCounts: 8388608
XResolution: 126.3137
YResolution: 126.3137
ResolutionUnit: 'Centimeter'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 65535
MinSampleValue: 0
Thresholding: 1
Offset: 8
UnknownTags: [1×1 struct]
This is the code I'm using
clearvars; close all; clc;
SaveFileName='test.tif';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[name,path]=uigetfile('*.mccd','MultiSelect','on');
I=imfinfo(name{1});
[D,~] = imread([path name{1}]);
[Xsize,Ysize] = size(D);
Max=max(max(D));
disp([name{1} ' loaded...']);
A1 = double(D);
for i = 2:length(name)
A1 = A1 + double(imread(name{i}));
disp([name{i} ' loaded...']);
end
A1=A1/length(name);
A = uint16(A1);
Xlength = 10*I.Width/I.XResolution;
Ylength = 10*I.Height/I.YResolution;
figure('units','normalized','outerposition',[.12 .41 .31 .31]); hold on;
s{1}=subplot(1,2,1,'color','none'); hold on; pbaspect([1 1 1]); xlabel('X (pixels)'); ylabel('Y (pixels)');
s{1}.Title.String = 'Frame #1';
im = imagesc(D);
im.AlphaData = 0.75; view([0 -90]); colormap(copper)
xlim([1 length(D(1,:))]); ylim([1 length(D(:,1))]);
s{2}=subplot(1,2,2,'color','none'); hold on; pbaspect([1 1 1]); xlabel('X (pixels)'); ylabel('Y (pixels)');
s{2}.Title.String = 'All Frames Summed and Averaged';
im = imagesc(A);
im.AlphaData = 0.75; view([0 -90]); colormap(copper)
xlim([1 length(A(1,:))]); ylim([1 length(A(:,1))]);
% save Summed Frames into new TIFF
t = Tiff(SaveFileName,'w'); %%
setTag(t,'Photometric',Tiff.Photometric.MinIsBlack);%%
setTag(t,'Compression',Tiff.Compression.None);
if I.ResolutionUnit=='Centimeter'
res=3;
elseif I.ResolutionUnit=='Inch'
res=2;
elseif I.ResolutionUnit=='None'
res=1;
end
setTag(t,'ResolutionUnit',res);
t.setTag('XResolution',I.XResolution)
t.setTag('YResolution',I.YResolution)
setTag(t,'SampleFormat',Tiff.SampleFormat.UInt);
setTag(t,'ExtraSamples',Tiff.ExtraSamples.Unspecified);
setTag(t,'ImageLength',I.Width);%%
setTag(t,'ImageWidth',I.Height);%% 2048
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
tagstruct.BitsPerSample = I.BitsPerSample;
tagstruct.SamplesPerPixel = I.SamplesPerPixel;
tagstruct.RowsPerStrip = I.RowsPerStrip; %2
setTag(t,tagstruct)
write(t,A);
close(t);
F=imfinfo(SaveFileName);

10 Comments

How much smaller is the saved file? Remember that you are throwing away metadata such as comments and possibly subimages as well.
Thanks for your reply Walter,
but I think I explained myself badly, sorry! The problem is actually not about the file size but the pixel size.
Below a comparison between one of the loaded files and my generated TIF file on the right. Loaded files are TIF, even though the extension looks different.
I have tried to play with resolution and pixel size in my script but I failed to obtain the same picture size.
What is iminfo for the generated file?
Xlength = 10*I.Width/I.XResolution;
Ylength = 10*I.Height/I.YResolution;
I have to wonder if that is correct if the ResolutionUnit turns out not to be centimeter ?
XResolution: 126.3137
That is a curious number. I am having trouble figuring out why you would use exactly that value; I cannot see any physical significance. Except, that is, that it is just a small bit off from 127 pixels per centimeter, which would be a resolution of 50 pixels per inch.
Hmmm... looks like maybe you have a Rayonix SX series device, 79.59 micrometer resolution ?
Hi,
Yes, the detector in this example is an old MAR165 (165 mm diameters). But in principle I'd like to use the same script for other 2D detectors. Xlength and Ylength result in 162.1361, which are the edges of the original picture shown on the left. However, these pars are actually not used in my script.
The iminfo of the generated file is:
F =
struct with fields:
Filename: 'C:\Users\usr1\Desktop\test.tif'
FileModDate: '22-feb-2021 08:38:23'
FileSize: 8388818
Format: 'tif'
FormatVersion: []
Width: 2048
Height: 2048
BitDepth: 16
ColorType: 'grayscale'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: 16
Compression: 'Uncompressed'
PhotometricInterpretation: 'BlackIsZero'
StripOffsets: 8
SamplesPerPixel: 1
RowsPerStrip: 2048
StripByteCounts: 8388608
XResolution: 126.3137
YResolution: 126.3137
ResolutionUnit: 'Centimeter'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: 65535
MinSampleValue: 0
Thresholding: 1
Offset: 8388616
ExtraSamples: 0
SampleFormat: 'Unsigned integer'
I have no experience in generating image files (TIF) and I think I'm just missing some simple step that perhaps is obvious to someone with more experience... I don't understand whether I have to modify the converted data (A) or the right tag in the generated Tiff (t).
Thanks
Your output looks completely normal and consistent with your input. There are some minor differences, but they should not have any effect on what you see.
What viewer are you using in each case?
They looks the same when plotted in Matlab (but I'm plotting the 2048x2048 grid without conversion). I'm using GSAS-ii for data reduction of large datasets, which I think uses this library.
I'm actually using MATLAB since the in-built feature to sum 2D images in gsas does not work correctly with all formats and when it does it takes ages.
thanks
Dear Walter,
I think the problem is solved now. As you noticed, the problem was not in the MATLAB script. There is a conversion that gsas applies and it was not working on my current version. After installing a more stable version of gsas the conversion seems to be working fine.
Thank you very much!

Sign in to comment.

Answers (1)

Asked:

on 19 Feb 2021

Commented:

on 22 Feb 2021

Community Treasure Hunt

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

Start Hunting!