-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimwrite2tif.m
145 lines (145 loc) · 4.88 KB
/
imwrite2tif.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function imwrite2tif(varargin)
% IMWRITE2TIF Write image to tif file with specified datatype.
% IMWRITE2TIF(IMGDATA,HEADER,IMFILE,DATATYPE) exports IMGDATA with HEADER
% to TIF file named IMFILE. HEADER is usally obtained by IMFINFO from
% original image file, and it can also be left empty. String DATATYPE
% specifies data type for the export. Supported data types include
% logical, uint8, int8, uint16, int16, uint32, int32, uint64, int64,
% single and double.
%
% IMWRITE2TIF(IMGDATA,HEADER,IMFILE,DATATYPE,TAG NAME1,TAG VALUE1,TAG NAME2,
% TAG VALUE2,...) writes with specified Matlab supported TIF tag values.
% These new tag values overide those already defined in HEADER.
%
% Note 1:
% to avoid errors such as '??? Error using ==> tifflib The value for
% MaxSampleValue must be ...', overide tag MaxSampleValue by Matlab
% supported values. Or simply remove the tag from HEADER.
%
% Note 2:
% Overwriting of the existing image files is not checked. Be cautious
% with the export image file name.
%
% Example 1:
% imgdata = imread('ngc6543a.jpg');
% header = imfinfo('ngc6543a.jpg');
% imwrite2tif(imgdata,header,'new_ngc6543a.tif','uint8');
%
% Example 2:
% imgdata = imread('mri.tif');
% imwrite2tif(imgdata,[],'new_mri.tif','int32','Copyright','MRI',
% 'Compression',1);
%
% More information can be found by searching for 'Exporting Image Data
% and Metadata to TIFF Files' in Matlab Help.
% Zhang Jiang
% $Revision: 1.0 $ $Date: 2011/02/23 $
if nargin<4 || mod(nargin,2)==1
error('Invalid number of input arguments.');
end
% assign input argument
imgdata = varargin{1};
header = varargin{2};
imfile = varargin{3};
datatype = varargin{4};
if nargin>4
tag_name = cell((nargin-4)/2,1);
tag_value = cell((nargin-4)/2,1);
for ii=1:(nargin-4)/2
tag_name{ii} = varargin{2*ii+3};
tag_value{ii} = varargin{2*ii+4};
end
end
% check errors
if ~isnumeric(imgdata)
error('The first input argument (image data) must be a numeric array.');
end
if ~isempty(header) && ~isstruct(header)
error('The second input argument (header info) must be empty or a structure.');
end
if ~ischar(imfile)
error('The third input argument (output tif file name) must be string.');
end
switch lower(datatype)
case 'logical'
BitsPerSample = 1;
SampleFormat = 1;
imgdata = logical(imgdata);
case 'uint8'
BitsPerSample = 8;
SampleFormat = 1;
imgdata = uint8(imgdata);
case 'int8'
BitsPerSample = 8;
SampleFormat = 2;
imgdata = int8(imgdata);
case 'uint16'
BitsPerSample = 16;
SampleFormat = 1;
imgdata = uint16(imgdata);
case 'int16'
BitsPerSample = 16;
SampleFormat = 2;
imgdata = int16(imgdata);
case 'uint32'
BitsPerSample = 32;
SampleFormat = 1;
imgdata = uint32(imgdata);
case 'int32'
BitsPerSample = 32;
SampleFormat = 2;
imgdata = int32(imgdata);
case 'single'
BitsPerSample = 32;
SampleFormat = 3;
imgdata = single(imgdata);
case {'uint64','int64','double'}
BitsPerSample = 64;
SampleFormat = 3;
imgdata = double(imgdata);
otherwise
error('Invalid output data type.');
end
% creat a Tiff object
t = Tiff(imfile,'w');
% duplicate tags from header
tagnamelist = Tiff.getTagNames();
tagnamelist_delete = {...
'StripByteCounts';...
'StripOffsets';
'TileByteCounts';...
'TileOffsets';...
'MaxSampleValue';...
'MinSampleValue';...
'ResolutionUnit'};
for ii=1:length(tagnamelist_delete) % remove read only tag names
tagnamelist(strcmpi(tagnamelist_delete{ii},tagnamelist)) = [];
end
if ~isempty(header)
for ii=1:length(tagnamelist)
if isfield(header,tagnamelist{ii}) && ~isempty(header.(tagnamelist{ii}))
tagstruct.(tagnamelist{ii}) = header.(tagnamelist{ii});
end
end
end
% update tags determined from imgdata and datatype
tagstruct.ImageLength = size(imgdata,1);
tagstruct.ImageWidth = size(imgdata,2);
tagstruct.SamplesPerPixel = size(imgdata,3);
tagstruct.SampleFormat = SampleFormat;
tagstruct.BitsPerSample = BitsPerSample;
% update some default tags (these tags can be overriden by user input below)
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.Compression = 1;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tagstruct.Software = 'MATLAB';
% update user input tag values
if nargin>4
for ii=1:length(tag_name)
tagstruct.(tag_name{ii}) = tag_value{ii};
end
end
% set tags and write to tif file
t.setTag(tagstruct)
t.write(imgdata);
t.close();