How to save a matix as an image?

862 views (last 30 days)
I have a matrix finalMat of data type double with values in the range of 0 to 255. It actually corresponds to some image. I am displaying it as below :
imshow(finalMat, []);
But when i try to save it using the below code, the image saved is completly white.
imwrite(finalMat,'myImage.jpeg','JPEG');
I want to save the image on disk without changing the values in the finalMat matrix. When I read the saved image i.e myImage.jpeg, I must get the same values as in finalMat. Can somebody please help in saving the image? Thank you in advance..
  4 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 9 Jan 2022
Edited: Image Analyst on 9 Jan 2022
To save a floating point image as a TIFF file, this should do it.
% Create floating point image.
rgbImage = rand (10, 20, 3);
% Image must be single precision.
rgbImage = single(rgbImage);
% Display it.
imshow(rgbImage, 'InitialMagnification', 1000)
axis('on', 'image');
% Create tiff object.
fileName = '_floatingPointImage.tif';
tiffObject = Tiff(fileName, 'w')
% Set tags.
tagstruct.ImageLength = size(rgbImage,1);
tagstruct.ImageWidth = size(rgbImage,2);
tagstruct.Compression = Tiff.Compression.None;
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 32;
tagstruct.SamplesPerPixel = size(rgbImage,3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tiffObject.setTag(tagstruct);
% Write the array to disk.
tiffObject.write(rgbImage);
tiffObject.close;
% Recall image.
m2 = imread(fileName)
% Check that it's the same as what we wrote out.
maxDiff = max(max(m2-rgbImage))
  7 Comments
Image Analyst
Image Analyst on 10 Jan 2022
@Federico Fioretti it does work. Look here is proof:
% Create floating point image.
rgbImage = 3000 * rand (10, 20, 3);
% Image must be single precision.
rgbImage = single(rgbImage);
% Display it.
imshow(rgbImage, 'InitialMagnification', 1000)
axis('on', 'image');
% Create tiff object.
fileName = '_floatingPointImage.tif';
tiffObject = Tiff(fileName, 'w')
% Set tags.
tagstruct.ImageLength = size(rgbImage,1);
tagstruct.ImageWidth = size(rgbImage,2);
tagstruct.Compression = Tiff.Compression.None;
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 32;
tagstruct.SamplesPerPixel = size(rgbImage,3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tiffObject.setTag(tagstruct);
% Write the array to disk.
tiffObject.write(rgbImage);
tiffObject.close;
% Recall image.
m2 = imread(fileName)
% Check that it's the same as what we wrote out.
maxDiff = max(max(m2-rgbImage)) % Should be zero.
You can see I have values as high as 3000 and I get back those same values because maxDiff is zero. Don't try to display this in MATLAB because MATLAB expects all RGB images to be in the range of 0-1. I thought this was going to be used in your photogrammetry program so don't worry about how it's displayed in MATLAB. If you really want to see it in MATLAB you'll have to divide by the max value to get a scaled image before you call imshow().
scaledRGB = rescale(rgbImage, 0, 1);
imshow(scaledRGB);

Sign in to comment.

More Answers (5)

Image Analyst
Image Analyst on 4 Mar 2016
If your image is uint8, I recommend PNG. It's lossless compression. About a third the size of BMP and TIFF images. More than JPEG but with perfect quality, unlike the crummy images you can get from jpeg if you compress too much. Don't use jpeg for image analysis. Create your filename with a .png extension and you don't need to use the third argument for imwrite.
imwrite(rgbImage, 'fubar.png');
You can find out what other formats are allowed with the imformats command:
>> imformats
EXT ISA INFO READ WRITE ALPHA DESCRIPTION
-----------------------------------------------------------------------------------------
bmp isbmp imbmpinfo readbmp writebmp 0 Windows Bitmap
cur iscur imcurinfo readcur 1 Windows Cursor resources
fts fits isfits imfitsinfo readfits 0 Flexible Image Transport System
gif isgif imgifinfo readgif writegif 0 Graphics Interchange Format
hdf ishdf imhdfinfo readhdf writehdf 0 Hierarchical Data Format
ico isico imicoinfo readico 1 Windows Icon resources
j2c j2k isjp2 imjp2info readjp2 writej2c 0 JPEG 2000 (raw codestream)
jp2 isjp2 imjp2info readjp2 writejp2 0 JPEG 2000 (Part 1)
jpf jpx isjp2 imjp2info readjp2 0 JPEG 2000 (Part 2)
jpg jpeg isjpg imjpginfo readjpg writejpg 0 Joint Photographic Experts Group
pbm ispbm impnminfo readpnm writepnm 0 Portable Bitmap
pcx ispcx impcxinfo readpcx writepcx 0 Windows Paintbrush
pgm ispgm impnminfo readpnm writepnm 0 Portable Graymap
png ispng impnginfo readpng writepng 1 Portable Network Graphics
pnm ispnm impnminfo readpnm writepnm 0 Portable Any Map
ppm isppm impnminfo readpnm writepnm 0 Portable Pixmap
ras isras imrasinfo readras writeras 1 Sun Raster
tif tiff istif imtifinfo readtif writetif 0 Tagged Image File Format
xwd isxwd imxwdinfo readxwd writexwd 0 X Window Dump
  7 Comments
Image Analyst
Image Analyst on 21 Sep 2021
How? For tiff, this doesn't work:
grayImage = imread('cameraman.tif');
grayImage = double(grayImage) + rand(size(grayImage));
imwrite(grayImage, '_deleteme.tif');
recalledImage = imread('_deleteme.tif'); % It's uint8, not double
and for dicom, the documentation says this for the input image.
Data Types: int8 | int16 | uint8 | uint16

Sign in to comment.


Guillaume
Guillaume on 4 Mar 2016
Matlab consider the type of the matrix to establish what intensity range corresponds to fully dark-fully white.
If the matrix is of type double, then matlab uses the range 0-1 to represent intensities. Any value above 1 is considered the same as 1 and is maximum brightness. Hence your intensities 1 to 255 are all considered the same = white.
However, if the matrix is of type uint8, then matlab uses the range 0-255, just as you have. So convert your matrix to uint8:
imwrite(uint8(finalMat) 'myImage.jpeg');
  3 Comments
Devadath Prabhu
Devadath Prabhu on 4 Mar 2016
Matrices are different in the sense that their values are different though the image looks similar. But I want the values also to be same. In my case, both finalMat and readIm are 512*1024. The first 8*8 block of finalMat has these values -
131.087903712260 126.588085931488 129.019131897930 127.986820756743 128.036797886933 128.129163153756 128.901568706468 127.913467179027
126.865651056741 128.644726154519 126.696839938151 129.320745973049 129.064319024373 127.759203600434 128.006382827703 128.032312069063
128.547542279462 128.013326895391 128.389098782354 128.799496025384 128.055922974060 127.763243851185 127.922268272279 128.470671480691
128.028037000488 127.433327438777 127.479136613125 128.980352019314 128.193475727530 128.480245823339 128.413770202099 127.880514709290
129.227871666373 128.063934629160 127.465928877913 128.144312598059 127.757980122585 129.250265336802 128.413391078785 128.787456156361
127.965837250422 126.915520053310 127.628369773152 127.390251727192 127.971102024689 127.836604160943 128.531568211385 127.799176238100
128.197089880864 128.309110329146 128.403008073977 127.913385806887 128.332535509389 127.758264955073 127.422464120998 127.273960679635
128.041637714197 128.369029343605 127.807345577358 127.854815737404 128.197686086408 128.134149584508 128.051525804888 127.880870927027
After trying the code you suggested, first 8*8 block of readIm has these values -
128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128
128 128 128 128 128 128 128 128
I want readIm to have same values as in finalMat. How can I do that?

Sign in to comment.


Image Analyst
Image Analyst on 8 Jan 2022
Save it as a .mat file instead of an image format file
save('my floating point image.mat', 'myImage'); % Save myImage into a .mat file.
To recall it later:
s = load('my floating point image.mat'); % Load data in .mat file into a structure.
myImage = s.myImage; % Extract the floating point image from the structure.
  1 Comment
Federico Fioretti
Federico Fioretti on 8 Jan 2022
I try to explain better what my problem is. I have a matrix of noninteger values and i have to save this matrix as an image because I have to use it later in an other software.
I have written a code that loads a file.mat in matlab, modifies it, and then i have to export this file (that is the matrix of non integers values) as an image format file. Can you help me?

Sign in to comment.


Milind patil
Milind patil on 10 Feb 2019
Brother, Simply use .png extension instead of .jpg.
imwrite(finalMat,'myImage.png');
This will give u exact values for sure.
  11 Comments
Walter Roberson
Walter Roberson on 8 Jan 2022
img = imread('cameraman.tif');
A = rescale(img);
imwrite(A, 'file_name.png');
imshow(img, []); title('original')
imshow(A); title('rescaled')

Sign in to comment.


Linh Nguyen
Linh Nguyen on 11 Oct 2021
@Milind patil give me your email - address, I want to ask about this topic. Do you find any way to solve it?
  8 Comments
Image Analyst
Image Analyst on 9 Jan 2022
To save a floating point image as a TIFF file, this should do it.
% Create floating point image.
rgbImage = rand (10, 20, 3);
% Image must be single precision.
rgbImage = single(rgbImage);
% Display it.
imshow(rgbImage, 'InitialMagnification', 1000)
axis('on', 'image');
% Create tiff object.
fileName = '_floatingPointImage.tif';
tiffObject = Tiff(fileName, 'w')
% Set tags.
tagstruct.ImageLength = size(rgbImage,1);
tagstruct.ImageWidth = size(rgbImage,2);
tagstruct.Compression = Tiff.Compression.None;
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 32;
tagstruct.SamplesPerPixel = size(rgbImage,3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tiffObject.setTag(tagstruct);
% Write the array to disk.
tiffObject.write(rgbImage);
tiffObject.close;
% Recall image.
m2 = imread(fileName)
% Check that it's the same as what we wrote out.
maxDiff = max(max(m2-rgbImage))

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!

Translated by