Convert data on a TIFF image and save it

Hi,
I made some calculations regarding data I use for my research in image analysis.
Since I wanted to have an image that showed the variations of my variable, I transformed my data into values between 0-65535. I can not work with 0-255, because it is a very small range for what my original data is.
My problem is: I tried to create a grayscale image, and save it into a .tiff file. However not only I cannot obtain the file, but the image that MatLab is showing me does not have any variations. Is either all black or all white.
I'm sure my data is correct. I looked into the original variable, and everything is correct. I also looked into the variable that transforms those original data into values between 0-65535. I found several values ranging, primarily, between 10000 and 50000, so everything looks fine.
Does anyone knows how to convert N² data into an image of NxN size and show those variations? Also how to save it, .tiff file to be more specific?
My code up until now, regarding the problem:
% shows my grayscale image
figure;
image(tcrho); %tcrho is my variable, already in 0-65535 scale
colormap(gray(65536); %grayscale colormap
cbh = colorbar('location', 'SouthOutside'); %just location and name of my colorbar
set(cbh, 'Units', 'normal');
text(3.5, -1, 'Densidade crescente', 'Parent', cbh);
%save my image. I need to save several images - that explains the %04d.
rtc = sprintf('rho%04d.tiff',d);
imwrite(tcrho,gray(65536),rtc,'tiff')

 Accepted Answer

What does
whos tcrho
show? I want to know if it's uint16 or double. By the way, you can't have a colormap with 65536 entries in it. Simply display your image like this:
imshow(tcrho, []);
and don't use a colormap at all.

6 Comments

Whos tcrho shows double.
The imshow function worked well. The image is showing properly.
Now I need to save it as a .tiff format, grayscale, with a colorbar, if possible. Do you know how?
My original code, considering I had created an image with a colorbar was:
rtc = sprintf('rho%04d.tiff',d);
imwrite(tcrho,gray(65536),rtc,'tiff')
What do I substitue gray(65536) for?
Thanks
You have to convert tcrho to uint8. If the range is outside 0-255 then you should call mat2gray:
imwrite(uint8(mat2gray(tcrho)), filename);
Haimon
Haimon on 10 Apr 2014
Edited: Haimon on 10 Apr 2014
It did not work.
The matrix range is 0-65535. When it saves the file, it shows only black, but the original matrix has values going from 10000 to 50000.
Uint16 worked though, just tried that. Without the mat2gray.
colormaps larger than 256 are not certain to work in MS Windows.
I would suggest that you use
imwrite( uint16(tcrho(:,:,[1 1 1])), rtc, 'tiff' );
The tcrho(:,:,[1 1 1]) is a short form of repmat(tcrho, 1, 1, 3) which is to say to convert the grayscale to rgb by duplicating the data into all three planes.
Sorry - it turns out mat2gray scales between 0 and 1, not 0 and 255. So if you want a regular 8 bit image, you have to multiply by 255. So if you want a 16 bit image, you have to multiply by 65535.
imwrite(uint8(255*mat2gray(tcrho)), filename);
Thank you for your help!

Sign in to comment.

More Answers (0)

Tags

Asked:

on 5 Apr 2014

Commented:

on 11 Apr 2014

Community Treasure Hunt

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

Start Hunting!