Modifying a TIFF file and saving it to a new one with the same size
Show older comments
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
Walter Roberson
on 19 Feb 2021
How much smaller is the saved file? Remember that you are throwing away metadata such as comments and possibly subimages as well.
Matt086
on 20 Feb 2021
Walter Roberson
on 20 Feb 2021
What is iminfo for the generated file?
Walter Roberson
on 20 Feb 2021
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 ?
Walter Roberson
on 20 Feb 2021
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 ?
Matt086
on 22 Feb 2021
Walter Roberson
on 22 Feb 2021
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?
Matt086
on 22 Feb 2021
Matt086
on 22 Feb 2021
Walter Roberson
on 22 Feb 2021
Whew!
Answers (1)
yanqi liu
on 20 Feb 2021
0 votes
sir, may be need upload some files to debug.
Categories
Find more on Audio and Video Data 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!