how to convert a matrix with values in decimal point to an image

I want to convert a matrix having decimal(.) values with in range of 0-255 into an image. e.g the matrix contains the values like [ 122.0456 33.6785 47.0048; 233.8765 121.6743 45.6780]. floor/ceil/round will round off the decimal values . But i want to retrieve the same matrix with decimal points after retrieving the matrix from image. please help

6 Comments

An image just being an array of pixel intensities, your matrix is already an image and you could just save it as a mat file and be done with it. There's nothing special that makes a 2d matrix an image.
Now if by image you mean something that can be viewed / opened by most image viewers / editors, so anybody can see your image on screen, then I see no point in not rounding the values. Your display will not be able to show the difference between 122.04 and 122.49. Some displays are not even capable of showing the difference between 122 and 123.
my matrix is not an image but it is created and to be converted into an image. my requirement is to regenerate the same matrix with same values upto four decimal points when i read this image
You still have not indicated which viewer(s) the resulting image have to be able to display the file. TIFF floating point images are not common and might not be handled well in random viewers.
"my matrix is not an image". Yes, it is. Any matrix is an image.
What is your definition of an image? This
[0.2 12.5485
-14.875 1025.1234]
is a 2x2 image with four pixels of different intensities. It can simply be stored with matlab's save command. Now saving this in a format that can be visualised with most standard image viewers is a bit more complicated, but so far it's not what you've asked.
my interest is not in visualising the image but to preserve the data (which is in form of matrix) to image form which i can reconstruct later with same values. So visualisaton is not the issue, the issue is to recover floating values. thanks in advance
As Guillaume has already pointed out, your matrix is already an image. Every matrix is an image. Ergo if you save the matrix, you still have an image. And if you save the matrix with its decimal values, then you will have an image with decimal values.
If you are not planning on viewing the image using any standard image viewing tools (which require integer values in the file), then why not just save the matrix exactly as it is using any standard MATLAB matrix-saving tool?

Sign in to comment.

Answers (1)

You will need to use TIFF files with the TIFF Class and configure the SampleFormat as Tiff.SampleFormat.IEEEFP . That will allow you to store single precision floating point values.
t = Tiff(filename, 'w');
tagstruct.ImageLength = size(YourArray, 1);
tagstruct.ImageWidth = size(YourArray, 2);
tagstruct.Compression = Tiff.Compression.None;
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 32;
tagstruct.SamplesPerPixel = size(YourArray, 3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tagstruct.MinSampleValue = 0.0;
tagstruct.MaxSampleValue = 255.0;
t.setTag(tagstruct);
t.write(YourArray);
t.close();
Note: more work might be necessary to be able to view this with any particular viewer.

Asked:

on 26 Aug 2015

Edited:

on 4 Sep 2015

Community Treasure Hunt

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

Start Hunting!