IMG2TIF Saves an image to a multichannel tiff. Input argument 'compression' can be 'none', 'lzw' or 'packbits'. Parameter index is a boolean flag that allows the user to save the image as an indexed tiff. Examples >> img2tif( a, 'a.tiff' ); >> img2tif( b, 'b.tiff', 'none' ); >> img2tif( c, 'c.tiff', 'lzw' );
0001 function answer=img2tif( img, filename, compression, index ) 0002 % IMG2TIF Saves an image to a multichannel tiff. Input argument 0003 % 'compression' can be 'none', 'lzw' or 'packbits'. Parameter index 0004 % is a boolean flag that allows the user to save the image as an 0005 % indexed tiff. 0006 % 0007 % Examples 0008 % >> img2tif( a, 'a.tiff' ); 0009 % >> img2tif( b, 'b.tiff', 'none' ); 0010 % >> img2tif( c, 'c.tiff', 'lzw' ); 0011 0012 % Author: Ivan E. Cao-Berg (icaoberg@cmu.edu) 0013 % Created: August 5, 2008 0014 % 0015 % March 7, 2012 R.F. Murphy Change stretching from 2% to min-max 0016 % June 5, 2012 M. Mackie Added parameter to deal with indexed images 0017 % August 4, 2012 D. Sullivan Added logical image "contrast stretching" 0018 % August 6, 2012 I. Cao-Berg Fixed statement where logical images were 0019 % stretched to the incorrect value 0020 % January 21, 2013 D. Sullivan no stretching seems to be present, added 0021 % min-max stretching 0022 % April 29, 2013 D. Sullivan stretchlim function broken, just scaling max 0023 % to 1 now. 0024 % 0025 % Copyright (C) 2008-2013 Murphy Lab 0026 % Carnegie Mellon University 0027 % 0028 % This program is free software; you can redistribute it and/or modify 0029 % it under the terms of the GNU General Public License as published 0030 % by the Free Software Foundation; either version 2 of the License, 0031 % or (at your option) any later version. 0032 % 0033 % This program is distributed in the hope that it will be useful, but 0034 % WITHOUT ANY WARRANTY; without even the implied warranty of 0035 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0036 % General Public License for more details. 0037 % 0038 % You should have received a copy of the GNU General Public License 0039 % along with this program; if not, write to the Free Software 0040 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0041 % 02110-1301, USA. 0042 % 0043 % For additional information visit http://murphylab.web.cmu.edu or 0044 % send email to murphy@cmu.edu 0045 0046 %mmackie 6/5/12 0047 answer=false; 0048 if( nargin < 3) 0049 compression = 'none'; 0050 index = false; 0051 end 0052 0053 if( nargin < 4) 0054 index = false; 0055 end 0056 0057 %Yajing Tang 7/3/13 Check the inputs' types 0058 if (~isnumeric(img) && ~islogical(img)) 0059 disp('img2tif:not a valid image, existing method'); 0060 return; 0061 end 0062 0063 if (~isstr(filename)) 0064 disp('img2tif:filename has to be a string, existing method'); 0065 return; 0066 end 0067 if isequal(strfind(filename,'.tif'),[]) || (strfind(filename,'.tif')~=(length(filename)-3 )) 0068 filename=[filename '.tif']; 0069 end 0070 0071 %D. Sullivan 4/29/13 Don't need to do this anymore. 0072 %devins 8/4/12 added logical contrast stretching 0073 if islogical(img) 0074 %icaoberg 8/6/2012 0075 %img = img.*256; 0076 img = img.*255; 0077 end 0078 0079 %D. Sullivan 1/21/13 0080 %no contrast stretching seems to be present, adding min-max stretching 0081 %D. Sullivan 4/29/13 Stretchlim does not seem to be working properly, 0082 %removing it 0083 % low_high = stretchlim(img(:)); 0084 % img = img./low_high(2); 0085 % maxval = double(max(img(:))); 0086 % img = double(img)./maxval; 0087 0088 %it will save an image as a mat file just to check contents 0089 %save( [ filename(1:end-4) '.mat' ], 'img' ); 0090 0091 %mmackie 6/5/2012 0092 if index 0093 %save first channel 0094 imwrite( img(:,:,1), gray, filename, 'Resolution', 300, 'Compression', compression); 0095 %append other channels to the first channel 0096 if size(img,3) > 1 0097 for i=2:1:size(img,3) 0098 imwrite( img(:,:,i), gray, filename, 'Resolution', 300, 'WriteMode', 'append', 'Compression', compression ); 0099 end 0100 end 0101 elseif ndims(img)==4 0102 %save first channel 0103 imwrite( squeeze(img(:,:,1,:)), filename, 'Resolution', 300, 'Compression', compression ); 0104 0105 %append other channels to the first channel 0106 if size(img,3) > 1 0107 for i=2:1:size(img,3) 0108 imwrite( squeeze(img(:,:,i,:)), filename, 'Resolution', 300, 'WriteMode', 'append', 'Compression', compression ); 0109 end 0110 end 0111 else 0112 %save first channel 0113 %D. Sullivan 5/7/13 check the file type for each channel and convert it 0114 %where appropriate so that outputs are displayed properly. 0115 if max(img(:))<=1 0116 imgsave = double(img(:,:,1)); 0117 elseif max(img(:))<=255 0118 imgsave=uint8(img(:,:,1)); 0119 elseif max(img(:))<=65536 0120 imgsave=uint16(img(:,:,1)); 0121 else 0122 warning('Unknown image type, renormalizing and saving as double') 0123 imgsave = img(:,:,1)./max(max(img(:,:,1))); 0124 end 0125 0126 imwrite( imgsave, filename, 'Resolution', 300, 'Compression', compression ); 0127 0128 %append other channels to the first channel 0129 if size(img,3) > 1 0130 for i=2:1:size(img,3) 0131 %D. Sullivan 5/7/13 check the file type for each channel and convert it 0132 %where appropriate so that outputs are displayed properly. 0133 if max(img(:))<=1 0134 imgsave = double(img(:,:,i)); 0135 elseif max(img(:))<=255 0136 imgsave=uint8(img(:,:,i)); 0137 elseif max(img(:))<=65536 0138 imgsave=uint16(img(:,:,i)); 0139 else 0140 warning('Unknown image type, renormalizing and saving as double') 0141 imgsave = img(:,:,i)./max(max(img(:,:,i))); 0142 end 0143 0144 imwrite( imgsave, filename, 'Resolution', 300, 'WriteMode', 'append', 'Compression', compression ); 0145 end 0146 end 0147 end 0148 answer=true; 0149 return; 0150 end%img2tif