Convert matrix number into png image

Hello i'm new on package "Image Processing Toolbox". I have a matrix of number ( http://dl.dropbox.com/u/59868217/matlab.mat) obtained by this script ( http://dl.dropbox.com/u/59868217/show_im_GR.m) I would be able to convert an image to grayscale or black and white. Possibly in png format.
How can I do ?

 Akzeptierte Antwort

Image Analyst
Image Analyst am 15 Apr. 2012

0 Stimmen

I looked at your m-file. You get your data like this:
A = fread(fid,size,'*uint16');
img = reshape(A,width,height); %ok-->poi trasponi
Did you know you can get the 2D matrix directly like this:
img = fread(fid, [width, height], '*uint16');
% It will be a 2D array after this.
No reshaping will be necessary. Anyway, to get it into PNG format you have to save it out to disk as a PNG format image file because when it's in MATLAB it's simply an array of numbers and format doesn't really apply.
baseFileName = 'My matrix.png';
fullFileName = fullfile(folderName, baseFileName);
imwrite(img, fullFileName);

Weitere Antworten (1)

Geppo Batt
Geppo Batt am 15 Apr. 2012

0 Stimmen

but the png file is completely black, while if use figure,image(img);colormap(gray); title(filename); obtained this image : http://dl.dropbox.com/u/59868217/untitled.jpg

5 Kommentare

Image Analyst
Image Analyst am 15 Apr. 2012
If you look at img in the variable editor, are the values all zero (completely black)? If they are, then how do you get that image with spots?
Geppo Batt
Geppo Batt am 16 Apr. 2012
no,the values are reported in the "img" matrix are those in the file .mat http://dl.dropbox.com/u/59868217/matlab.mat than the values are all different zero
Image Analyst
Image Analyst am 16 Apr. 2012
I downloaded your mat file and tried it and it was fine. It read the .mat file fine, it displayed fine, and I could write it out and recall a PNG file fine. Here is my code. Does this work for you?
By the way, you should not save "ans" to the .mat file. This is matlab's default variable name. You should accept the output of your operation into some variable with a name and save that - don't just save "ans" itself.
s = load('matlab.mat')
image16 = s.ans;
minValue = min(image16(:))
maxValue = max(image16(:))
imshow(image16, []);
axis on;
impixelinfo;
% Save it out to a PNG file.
imwrite(image16, 'matlab.png');
% Recall the image from the PNG file.
i2=imread('matlab.png');
figure;
imshow(i2, []);
Geppo Batt
Geppo Batt am 17 Apr. 2012
ok perfect, thank you very much. you saved me. the last question minvalue and maxvalue what does they for ?
Image Analyst
Image Analyst am 17 Apr. 2012
I just called them to see if you had integer or floating point values and if they were in the range 0-255, 0-1, or something else. which might cause me to call mat2gray() or uint8().

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by