Saving indexed tif images from for loop: Problem with file size.

Hi, I have the following loop that takes a 32-bit 4D data set data(m,n,i,j) and outputs i*j indexed m by n images.
fid=fopen('myfile','rb','ieee-le');
data4D=fread(fid,'int32');
image_dir=pwd;
for i=1:size(data4D,3)
for k=1:size(data4D,4)
data3D=squeeze(abs(data4D(:,:,1:1:end,k)));
data2D=(mat2gray(squeeze(data3D(:,:,i))));
baseFileName = strcat( 'Image_', num2str(k) ...
, 'SubImage_', num2str(i),'.tif');
fullFileName = fullfile(image_dir,baseFileName);
imwrite(data2D, fullFileName);
end
end
This saves the images, but they are not the correct file size, or bit number. I have tried to use the Tiff function to save the images, but I must not be doing it incorrectly, because the images are saved, but they all look 100% black. Is there a Matlab master out there who can tune up this script?
Thanks?

Réponses (1)

I don't think TIFF can take pixels in the range 0-1. Well maybe it can with special options. But you're calling mat2gray so why not simply multiply it by 255 and cast to uint8?
data2D= uint8(255*mat2gray(squeeze(data3D(:,:,i))));

9 commentaires

Hey! Well, the ImageJ plugin I'm using calls for 32 bit images. It seems like a big problem to save 32 bit in Matlab. I found two ways, one works. That is use the MIJ function to generate images from a .mat file in the workspace. The other is to use the Tiff function, but I have not figured that out yet.
mat2gray() converts it into a double. It's no longer a 32 bit integer after you pass the image in through mat2gray().
Do you know of another function that can replace mat2gray?
I have no idea what you're using it for. It scales the image values to between 0 and 1. Is that what you want to do? If so, why?
I thought that it was necessary to make images from the mat files. Basically I need a function that can save 32 bit images from the 32-bit double mat files.
What's the original range of the data? And do you want to change it (to improve contrast or something) when you create the 32 bit integer array, or do you just want to round it off to the nearest integer?
I'm reading the data in as int32. I don't want to change it. Just want to save the data without precision loss as image. I'm going by this chart:
Mapping the Matlab numeric type to ImageJ type
Matlab ImageJ image type
uint8 Grayscale 8-bit
int8 Grayscale 8-bit
uint16 Grayscale 16-bit
int16 Grayscale 16-bit
uint32 Grayscale 16-bit (Possible loss of precision)
int32 Grayscale 16-bit (Possible loss of precision)
uint64 Not allowed
int64 Not allowed
single Grayscale 32-bit
*double Grayscale 32-bit (Possible loss of precision)*
Please answer my question about the original range of the data. It may well be that the upper bytes are not being used.
You say your code "takes a 32-bit 4D data set data(m,n,i,j)". What is the data type of "data"? Is it int32, uint32, or single? It's not double like you said above if it's only 32 bits. Double is 8 bytes or 64 bits.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Question posée :

le 20 Nov 2014

Commenté :

le 25 Nov 2014

Community Treasure Hunt

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

Start Hunting!

Translated by