How to save a matix as an image?
342 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Devadath Prabhu
el 4 de Mzo. de 2016
Comentada: Image Analyst
el 10 de En. de 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 comentarios
Adam
el 4 de Mzo. de 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.
Respuesta aceptada
Image Analyst
el 9 de En. de 2022
Editada: Image Analyst
el 9 de En. de 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 comentarios
Federico Fioretti
el 10 de En. de 2022
Editada: Federico Fioretti
el 10 de En. de 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
el 10 de En. de 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);
Más respuestas (5)
Image Analyst
el 4 de Mzo. de 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 comentarios
Walter Roberson
el 7 de Mzo. de 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
el 21 de Sept. de 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
Guillaume
el 4 de Mzo. de 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 comentarios
Guillaume
el 4 de Mzo. de 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
el 4 de Mzo. de 2016
Editada: Devadath Prabhu
el 4 de Mzo. de 2016
Image Analyst
el 8 de En. de 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 comentario
Federico Fioretti
el 8 de En. de 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?
Milind patil
el 10 de Feb. de 2019
Brother, Simply use .png extension instead of .jpg.
imwrite(finalMat,'myImage.png');
This will give u exact values for sure.
11 comentarios
Linh Nguyen
el 11 de Oct. de 2021
Editada: Walter Roberson
el 8 de En. de 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
el 8 de En. de 2022
img = imread('cameraman.tif');
A = rescale(img);
imwrite(A, 'file_name.png');
imshow(img, []); title('original')
imshow(A); title('rescaled')
Linh Nguyen
el 11 de Oct. de 2021
@Milind patil give me your email - address, I want to ask about this topic. Do you find any way to solve it?
8 comentarios
Walter Roberson
el 9 de En. de 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
el 9 de En. de 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))
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!