简体   繁体   中英

save tif 32 bit images by using imwrite

I'm trying to save my images as tif 32 bits but I got this error:

Cannot write uint32 data to a TIFF file

This is my code:

for k=1:10

    Id{k} = waverec2(t_C,L,'sym8');
    filename= ['C:\Path \Id_number_' num2str(k) '.tif'];
    Id{k}=uint32(Id{k});
    imwrite(Id{k},filename);

end 

I need to save my images as tif 32 bits. Any idea?

Edit using the method from Ashish (see below)

for k=1:10

        Id{k} = waverec2(t_C,L,'sym8');
        filename= ['C:\Path \Id_number_' num2str(k) '.tif'];
        t = Tiff('filename','a')
    Id{k}=uint32(Id{k});
    t.write(Id{k});
end

but under Matlab I got this error :

Error using tifflib
Unable to retrieve ImageLength.

    Error in Tiff/getTag (line 784)
                        tagValue = tifflib('getField',obj.FileID,Tiff.TagID.(tagId));

    Error in Tiff/writeAllStrips (line 1660)
                h = obj.getTag('ImageLength');

    Error in Tiff/write (line 1228)
                    obj.writeAllStrips(varargin{:});

It is possible with MATLAB:

%
% Start with:
% http://www.mathworks.com/help/matlab/import_export/exporting-to-images.html#br_c_iz-1

data = uint32(magic(10));


%% -------------------------------------
%  Modify these variables to reuse this section: (enclosed by ----s)
%     - outputFileName  (filename in your question)
%     - data            (Id{k} in your question)
%


outputFileName = 'myfile.tif';
% This is a direct interface to libtiff
t = Tiff(outputFileName,'w');


% Setup tags
% Lots of info here:
% http://www.mathworks.com/help/matlab/ref/tiffclass.html
tagstruct.ImageLength     = size(data,1);
tagstruct.ImageWidth      = size(data,2);
tagstruct.Photometric     = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample   = 32;
tagstruct.SamplesPerPixel = 1;
tagstruct.RowsPerStrip    = 16;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tagstruct.Software        = 'MATLAB';
t.setTag(tagstruct)


t.write(data);
t.close();

%% -------------------------------------

%%
d = imread('myfile.tif');
disp(class(d));
assert(isequal(d,data))

Although the TIFF format does support uint32 , Matlab does not. Accepted intput classes are double, single, uint8, uint16, or logical.

If you losing precision is acceptable to you, you can convert it to uint16 (use im2uint16 ). If it is not, you can convert it to double with im2double . Note that im2double is not the same as double (your_uint16_image) so be careful with it.

If none of those options are acceptable, you might need to write your own mex code using libtiff or use Octave (which will require GraphicsMagick built with quantum depth 32).

EDIT: apparently there is also a new TIFF class (see answer below) which gives a direct interface to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM