how can i convert a matrix with decimal values into a image and then retrieve the same matrix back from the image with minimal error?? plz help

26 views (last 30 days)
i have a matrix with decimal values both positive n negative. want to convert it into a image n then get the previous matrix from this image with minimal errors.
  5 Comments
Image Analyst
Image Analyst on 14 Sep 2015
This is not a color image (you don't have three independent color channels, only one), so your only option is to apply a colormap - a pseudocolor look up table - to turn the numbers into colors.
colormap(hsv(256));
colorbar;
Walter Roberson
Walter Roberson on 14 Sep 2015
To display a matrix that might have both negative and positive values as a colored image, use
imagesc(B)
The results of this will depend upon the range of data in the array. To get consistent results we would need to know the range of values that might occur in the array. Will the entries always be in the range -255 to +255 ? Could they be -256 exactly to +256 exactly? Could they be -256 + eps(256) to +256 - eps(256) which is to say (-256,256) exclusive?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Sep 2015
sz = size(YourDoubleMatrix);
R = typecast(uint64(YourDoubleMatrix(:) * 2^56),'uint8');
R1 = reshape(R, [8 sz]);
R2 = permute(flipud(R1), [2 3 1 4]);
R3 = reshape(R2, [sz(1), sz(2)*8, sz(3)]);
Now R3 is an image the same height but 8 times as wide as the original image. The first horizontal 1/8th of R3 in the image is equivalent to uint8(floor(YourDoubleImage)) -- so for example for 234.67 and 3.6789 it would have 234 and 3. The second horizontal 1/8th of R3 is equivalent to uint8(floor((YourDoubleImage - floor(YourDoubleImage)) * 256)) -- that is, it has the first 8 bits of the fractions, such as the first 8 bits of 0.67 and 0.6789. The next horizontal 1/8th of R3 in the image corresponds to the next 8 bits, and so on, until the last 1/8th of R3 corresponds to the last 8 bits. The last section will be pure black except for the parts of YourDoubleImage that are less than 16.
This arrangement stores all of the information in the original for values that are at least 1, if I have calculated correctly. It would be possible to store all of the information completely for all values, but if that were done, no portion of the visual result would look like the result of displaying uint8(YourDoubleMatrix) which it seems likely you would like to see.
  9 Comments
Walter Roberson
Walter Roberson on 15 Sep 2015
"i want to convert decimal values(234.67,3.6789 etc) to values in between the range 0-255 to view it as a coloured rgb image n then retrive the same decimal values." together with "i want that this information that is being removed from the original matrix to be stored somewhere and then use it when needed."
So the poster needs all of the bits stored somewhere but the image to still be in RGB uint8 format.
I could have achieved this by typecast()'ing the double array to uint8 and doing a trivial reshape back to 3D, but none of the result would have been understandable. I choose to do the permutes and reshapes so that at least one portion of the image (the left-most 1/8th) would show up the same as if you had used uint8(floor(TheImage)) and thus would look the same as if you had not stored all the extra information, but at the same time for the extra information to be recoverable from the image.
I might have pointed one of my previous answers that showed how to represent single precision float as TIFF images, a process that could be generalized to double precision if the right tag name is found. But that would have violated the constraint that the image had to be 0-255.
So the poster wants an image as if they had uint8()'d the data but with the extra bits retrievable. Why that is important is unclear to me, but the permute and flip and so on is one way of achieving it.
Image Analyst
Image Analyst on 15 Sep 2015
Well I could be wrong because I don't have the Mind Reading Toolbox like you have :-) But I think this might be a case of the poster asking for "x" but not telling us that they really want to get at "z", whereas if they told us in advance that they want "z" we'd say that approach "x" is not the right approach like they thought, and they should do approach "y". That kind of thing happens all the time.
The comment the poster added later, below the original post, said that what she really needed to do was to have a pseudcolored version of a gray scale matrix (the badly-named "B") for display (which we told her how to do), as well as saving the original floating point matrix for later use (which can be done just by using the original B floating point variable if it's used in the same instance of the program, or by saving to a mat file if it's needed in a later instance of the program or by a different program).
tania should clarify. Or maybe she already has because she accepted this answer.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 13 Sep 2015
Your matrix already is an image. Why do you think it's not? Images can be floating point - they don't have to be uint16 or uint8 you know.
  2 Comments
Walter Roberson
Walter Roberson on 13 Sep 2015
TIFF might be the only image file format that allows you 64 bits of floating point per pixel. I was able to find the TIFF tag for 32 bits of floating point per pixel but not for 64, other than to confirm that it exists.
Image Analyst
Image Analyst on 14 Sep 2015
She didn't say she needed to write it out to disk - make a round trip out to disk in an industry standard format. If that is what is needed, then I suggest she just use a .mat file - unless for some reason the disk file also needs to be read in and used by some program other than MATLAB.

Sign in to comment.


amreena najeeb
amreena najeeb on 27 Jan 2019
good afternoon sir
i am doing image steganography btt i have problem in inverse cat map i am using the following code for encryption but feel problem in decryption
function X = three_d_catmap(Y)
p = size(Y,1); % get the number of pixels on each side
X = zeros(size(Y)); % make space for X (all zeros to start)
for i = 1:p % loop through all the pixels
for j = 1:p
newi = mod(((i-1) + (j-1)),p) + 1; % get new i coord (a+b) modulas p
newj = mod(((i-1) + 2*(j-1)),p) + 1; % get new j coord (a+2b) modulas p
X(newi,newj,:) = mod((4*(i-1)*1*(j-1)+Y(i,j,:)),p) + 1;
end
end%first applied this

Categories

Find more on Convert Image Type 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!