How to save a matix as an image?

346 Ansichten (letzte 30 Tage)
Devadath Prabhu
Devadath Prabhu am 4 Mär. 2016
Kommentiert: Image Analyst am 10 Jan. 2022
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 Kommentare
Adam
Adam am 4 Mär. 2016
I'm not an expert on different image formats, I literally just took a quick look at the Matlab help for imwrite and saw that. You can easily experiment with different file formats though.
Devadath Prabhu
Devadath Prabhu am 4 Mär. 2016
Sure! Thank you :)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 9 Jan. 2022
Bearbeitet: Image Analyst am 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 Kommentare
Federico Fioretti
Federico Fioretti am 10 Jan. 2022
Bearbeitet: Federico Fioretti am 10 Jan. 2022
@Image Analyst I'm sorry but the code doesn't work. I obtain a white image except for a small part of it. I think it's beacuse the code considers only values between 0 and 1, while I have values also higher than 1 and i could also have negative values. So i need a bigger range of values to be considered. How can i do it?
Image Analyst
Image Analyst am 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);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (5)

Image Analyst
Image Analyst am 4 Mär. 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 Kommentare
Walter Roberson
Walter Roberson am 7 Mär. 2016
If you really want to write floating point numbers in an image format, then I suggest using the Tiff class, or DICOM format.
If your purpose is steganography then you are going about it the wrong way.
Image Analyst
Image Analyst am 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

Melden Sie sich an, um zu kommentieren.


Guillaume
Guillaume am 4 Mär. 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 Kommentare
Guillaume
Guillaume am 4 Mär. 2016
It is different in what way?
I assume that the values in finalMat are integers. If not, then there's no way you can save it as a jpg file without losing the decimals.
I also assume that your matrix is an 3D matrix (of size m x n x 3). If it's only a 2D matrix, you'll have to convert it to rgb before writing to jpg:
imwrite(repmat(uint8(finalMat), [1 1 3]), 'myImage.jpeg')
And to get your original image, just keep one of the page.
readIm = imread('myImage.jpeg');
readIm = readIm(:, :, 1);
Devadath Prabhu
Devadath Prabhu am 4 Mär. 2016
Bearbeitet: Devadath Prabhu am 4 Mär. 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?

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 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 Kommentar
Federico Fioretti
Federico Fioretti am 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?

Melden Sie sich an, um zu kommentieren.


Milind patil
Milind patil am 10 Feb. 2019
Brother, Simply use .png extension instead of .jpg.
imwrite(finalMat,'myImage.png');
This will give u exact values for sure.
  11 Kommentare
Linh Nguyen
Linh Nguyen am 11 Okt. 2021
Bearbeitet: Walter Roberson am 8 Jan. 2022
I want a .png file.
I have a double matrix.
img = imread('cameraman.tif'); img = double(img);
if I use:
imwrite(A, 'file_name.png');
it is different from imshow(A, []);
Walter Roberson
Walter Roberson am 8 Jan. 2022
img = imread('cameraman.tif');
A = rescale(img);
imwrite(A, 'file_name.png');
imshow(img, []); title('original')
imshow(A); title('rescaled')

Melden Sie sich an, um zu kommentieren.


Linh Nguyen
Linh Nguyen am 11 Okt. 2021
@Milind patil give me your email - address, I want to ask about this topic. Do you find any way to solve it?
  8 Kommentare
Walter Roberson
Walter Roberson am 9 Jan. 2022
There is a File Exchange contribution that can write floating-point TIFF files.
Caution: it is not common for programs to support floating-point TIFF files. If I recall correctly, ImageJ does support them, but Windows Media does not; I am not sure about Adobe programs.
Image Analyst
Image Analyst am 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))

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by