Modifying a TIFF file and saving it to a new one with the same size
22 views (last 30 days)
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
Answers (1)
See Also
Categories
Find more on Image Processing Toolbox 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!