How do I convert a PNG to a matrix?

15 views (last 30 days)
Larry
Larry on 24 Aug 2017
Commented: Image Analyst on 1 Sep 2017
I need assistance in converting a PNG file to a matrix. At first, I save downloaded file from a matrix to a PNG file, using the code below:
if true
% code
fig=figure('visible','off');
imagesc(outo);
colormap(jet(512))
% colormap(jet(256))
x=gca;
x.YDir='normal';
set(x,'position',[0 0 1 1],'units','normalized')
truesize;
caxis([0 55])
axis off
set(fig,'PaperPositionMode','auto')
print('outo','-dpng','-r0')
end
Now, once I have this file called outo.png, I want to return it back to a matrix I originally started with. Down below is the code I use to read that file, my problem is that I do not get the same numbers.
if true
% code
imgpath2= tt1(i).name;
map=colormap(jet(1024));
RGB=imread(imgpath2);
S=rgb2ind(RGB,map);
S=double(S);
L=size(map,1);
outraw=interp1(1:L,linspace(min_org,max_org,L),S(:,:));
% pixels around campus
outraw=flipud(outraw);
end
Does anyone know how to do a better job? I use jet(1024) instead of jet(256), because it give me better numbers.
I have attached the file I generated and a MAT file with the original matrix, so you can see what the values should be in this 348X348 matrix. I would really appreciate someone coming up with this, I am sure there are many who could use the alsorithm.
Thank you,
Larry
  2 Comments
Suhan
Suhan on 31 Aug 2017
Hi Larry, I am having troubles in reproducing the issue. You have attached only an array refl_new. Is this the variable 'outo' in the above code snippet?
Also, the code snippet is not very clear. Could you modify the code snippet and also elaborate your query so that all of us can understand it better? For example, what is 'imgpath2' in the code snippet?
Rik
Rik on 1 Sep 2017
It sounds to me as if you want to index an image, so why not using ind2rgb? Writing a file to read it immediately after that is usually a sign you should rethink your approach.
Also, next time, please follow the question guidelines here and here. It will greatly improve your chances of getting an answer.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 1 Sep 2017
Onto.png never was a matrix so you can't return it to one. You're saving the whole figure not the image matrix itself. That includes axes, tick mark, etc. If you want to save the image itself and recall it then use imwrite() instead of print().
  3 Comments
Image Analyst
Image Analyst on 1 Sep 2017
Larry, again, you're saving the figure, not the image. The image is saved into the CData property of an "axes" which is in a "figure". Using print saves the figure, not the image inside the axes. Just look at the help: "print(filename,formattype) saves the current figure to a file".
If you want to save and recall a double variable - only in MATLAB and not with any other program - then save the outo array to a proprietary format .mat file with save().
save('outo.mat', 'outo');
If you want to save outo - the actual image, NOT the figure - as an image that any program can read, you must save it in a file format like PNG. So then cast it to integer and save it with imwrite():
uint8Image = uint8(255 * mat2gray(outo));
imwrite(uint8Image, 'outo.png');
Image Analyst
Image Analyst on 1 Sep 2017
Again, you're saving the figure, not the image. The image is saved into the CData property of an "axes" which is in a "figure". Using print saves the figure, not the image inside the axes. Just look at the help: "print(filename,formattype) saves the current figure to a file".
If you want to save and recall a double variable - only in MATLAB and not with any other program - then save the outo array to a proprietary format .mat file with save().
save('outo.mat', 'outo');
To recall:
s = load('outo.mat');
outo = s.outo;
If you want to save outo - the actual image, NOT the figure - as an image that any program can read, you must save it in a file format like PNG. So then cast it to integer and save it with imwrite():
uint8Image = uint8(255 * mat2gray(outo));
imwrite(uint8Image, 'outo.png');
To recall you use imread()
grayImage = imread('outo.png');
but to get the values in the correct range, you're going to have to save the min and max values of it in a .mat file so you can recall them later.
minValue = min(outo(:));
maxValue = max(outo(:));
save('outo.mat', 'minValue', 'maxValue');
To recall:
s = load('outo.mat');
minValue = s.minValue;
maxValue = s.maxValue;
outo = (maxValue - minValue) * double(grayImage / 255) + minValue;
But if you're going to do that, you might as well save outo in the mat file also.

Sign in to comment.

Categories

Find more on Images 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!