how to convert a matrix with values in decimal point to an image
Show older comments
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
Guillaume
on 26 Aug 2015
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.
studentambitious
on 28 Aug 2015
Walter Roberson
on 28 Aug 2015
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.
studentambitious
on 3 Sep 2015
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?
Answers (1)
Walter Roberson
on 26 Aug 2015
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.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!