How to save a Tiff using signed values with LibTiff library.

조회 수: 6 (최근 30일)
Katherine
Katherine 2014년 11월 17일
댓글: Katherine 2014년 11월 17일
I am using the LibTiff library, as I already know that imwrite can't deal with signed values. If I try to convert FinalImage to int16 before saving it, I get the following error message:
Error using tifflib If SampleFormat is Uint, the image datatype is restricted to one of the following: logical, uint8, uint16, uint32.
Error in Tiff/writeAllStrips (line 1933) tifflib('writeEncodedStrip',obj.FileID, stripNum-1,imageData(row_inds,:));
Error in Tiff/write (line 1459) obj.writeAllStrips(varargin{:});
Error in tiffsaveexample (line 33) t.write(intFinalImage(:,:,1));
So it seems that I must change this variable SampleFormat, but I don't know how to modify this. Any recommendations?
This results from the follwing code:
% Save tiff unsigned attempt
FileTif='11_7_blank1_avgentire_stack_bck.tif';
InfoImage=imfinfo(FileTif);
mImage=InfoImage(1).Width;
nImage=InfoImage(1).Height;
NumberImages=length(InfoImage);
FinalImage=zeros(nImage,mImage,NumberImages,'uint16');
TifLink = Tiff(FileTif, 'r');
for i=1:NumberImages
TifLink.setDirectory(i);
FinalImage(:,:,i)=TifLink.read();
end
TifLink.close();
intFinalImage = int16(FinalImage);
tiffFile = strcat('bksub2_', FileTif);
t = Tiff(tiffFile,'w')
tagstruct.ImageLength = size(intFinalImage,1);
tagstruct.ImageWidth = size(intFinalImage,2);
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 16;
tagstruct.SamplesPerPixel = 1;
tagstruct.RowsPerStrip = 16;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky
tagstruct.Software = 'MATLAB'
t.setTag(tagstruct)
t.write(intFinalImage(:,:,1));
t.close();

채택된 답변

Ashish Uthama
Ashish Uthama 2014년 11월 17일
편집: Ashish Uthama 2014년 11월 17일
You need one extra tag:
intFinalImage = int16(magic(10));
tiffFile = 't.tif';
t = Tiff(tiffFile,'w');
%-----------------------------------------------
tagstruct.SampleFormat = Tiff.SampleFormat.Int;
%-----------------------------------------------
tagstruct.ImageLength = size(intFinalImage,1);
tagstruct.ImageWidth = size(intFinalImage,2);
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 16;
tagstruct.SamplesPerPixel = 1;
tagstruct.RowsPerStrip = 16;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tagstruct.Software = 'MATLAB';
t.setTag(tagstruct)
t.write(intFinalImage(:,:,1));
t.close();
d = imread(tiffFile);
class(d)
ans =
int16

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by