3

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{:});
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Ahmed
  • 321
  • 1
  • 3
  • 14

2 Answers2

7

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))
Ashish Uthama
  • 1,331
  • 1
  • 9
  • 14
  • +1 Works! You might use something like `data = uint32(rand(100)*2^32-1);` as better example data. – horchler Feb 07 '14 at 20:05
  • Nice! I've never used `Tiff` to directly interface with libtiff. – chappjc Feb 07 '14 at 20:10
  • Mr.Ashish based on the code you've mentioned my code should be like I wrote above 'I updated my code see it ' but it still not working I got this error under matlab:Error using tifflib – Ahmed Feb 10 '14 at 07:19
  • 1
    Ahmed, your current edits in the question do not reflect the tagstruct setup and the call to t.setTag. Please try running the code in my answer and then modify it to suite your need. – Ashish Uthama Feb 10 '14 at 14:15
  • Ashish I'm trying to execute step by step your code but I'm stuck at the second line Error using tifflib Unable to open TIFF file "myfile.tif". !!!!!! should I integrated by myself 'Tifflib'?I understand it like this, knowing that I'm using Matlab R2012a if yes I've found this http://download.osgeo.org/libtiff/ but I'm confused which one I choose?:/ – Ahmed Feb 10 '14 at 20:49
  • 1
    Ahmed, please post full error messages. Based on your explanation, I would guess that MATLAB is in a directory where you dont have write permission. And no, you need not integrate 'Tifflib', everything you need for this is part of your MATLAB installation. – Ashish Uthama Feb 10 '14 at 20:51
  • Ashish,In my case I've 37 images So if I want to write each image after my treatment I should creat 37 t files right? or t file can contains all of them, knowing that I need each out image for doing further operations? – Ahmed Feb 10 '14 at 21:58
  • for the sake of clarity,I've 37 noisy images and after I denoised them I want to reread them under ImageJ my problem is even may file can contain my 37 data I want to write them seperatly to do my further treatments please can you help me? – Ahmed Feb 11 '14 at 09:57
  • 1
    Edited the code to hopefully help you get started with reusing this snippet. ('t' is a variable, not a file). If you need to work frequently with Tiff files, I would strongly encourage you to spend time reading the links in the code. It is a lot of information, but it would save you a lot of time in the future. – Ashish Uthama Feb 11 '14 at 13:33
  • sorry yes I meant myfile not t :p t = Tiff('myfile.tif','w'); I read the links but what I want if there is a possibly to create more than file for me I need to create 37 to write my denoised images for example t = Tiff(myfile{k} num2str(k)'w'); like this I know it's not working 'too many arguments' just an example – Ahmed Feb 11 '14 at 20:40
1

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.

carandraug
  • 12,938
  • 1
  • 26
  • 38
  • Thank you I've never write a mex code using octave or but at least I know that tiff 32 it's impossible under Matlab – Ahmed Feb 07 '14 at 19:36
  • 1
    @Ahmed you misunderstood me. I said either write a mex function using libtiff (that's Matlab), or use Octave since it's a lot like Matlab but supports writing and reading of uint32. I did not say to write mex in Matlab. – carandraug Feb 08 '14 at 00:02
  • 1
    ups! The end of the previous comment should had been "mex in Octave". – carandraug Feb 09 '14 at 22:36