Verfasst am: 25.03.2015, 16:25
Titel: Extract all *.png images from one *.mat file
I have a mat file my_file.mat where 3 channel 96x96 images are stored in a matrix. Every line of a matrix contains one image. So in one line: the first 96x96 cells (= 9216) contain the RED values, the next 96x96 the Green values and the last 96x96 cells the BLUE values.
I want to extract the images and save them in a folder as png images. Is there a function that can do this for me? Or did one of you write a small code to this and could post it here?
Thanks a lot
userf
Gast
Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
Verfasst am: 25.03.2015, 17:01
Titel: It should be something like this
it should be something like this (maybe you can help me with correcting the code in matlab language)
for l in matrix.lines
mat curr_image;
for c in 1:9216
currline = c/96;
currcolumn = c - c/96;
curr_image(currline,currcolumn)[0] = matrix(l,c);
curr_image(currline,currcolumn)[1] = matrix(l,c+9216);
curr_image(currline,currcolumn)[2] = matrix(l,c+2*9216);
end imwrite(curr_image, path_to_folder + l + '.png') end
Verfasst am: 26.03.2015, 09:19
Titel: Re: Extract all *.png images from one *.mat file
Hi userf,
The posted code is far from valid Matlab code. I recommend to read the GEtting Started chapters of the documentation, because it is impossible to use such a powerful tool like Matlab without reading the manuals. A froum is not an efficient way to learn the basics.
Code:
FileData = load('my_file.mat');
Data = FileData.matrix; % adjust to the name of your array
Red = reshape(Data(1, :), 96, 96);
Green = reshape(Data(2, :), 96, 96);
Blue = reshape(Data(3, :), 96, 96);
imwrite(Red, 'Red.png');
imwrite(Green, 'Green.png');
imwrite(Blue, 'Blue.png');
I know the code is far from matlab code. I just wanted to write the way I imagine the function, so that the readers can see better what I mean.
Thanks for your code. But I think what it does, is to extract (all in all) 3 images from the matrix: Red.png, Green.png, Blue.png.
What i want to do is: when the input data_matrix (in my_file.mat) has a dimension: 3000x27648, I should extract 3000 images (one image corresponds to one line in the matrix). The red, green and blue values should not be saved as 3 independent images. They are the RGB values of the 3 channel 96x96 image.
I hope my question is now clearer and I am looking forward for your answer
userf
Gast
Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
Verfasst am: 26.03.2015, 11:37
Titel:
Here is now the right code, maybe it can help somebody
Code:
FileData = load('path_to_folder/my_file.mat');
Data = FileData.X; % to be renamed
red = reshape(Data(i,1:9216), [96,96]); %Data(i, 1:9216) saves the first 9216 values to a vector, reshape(..., [96x96]) reshapes the vector to be a 96x96 matrix
green = reshape(Data(i,9217:18432), [96,96]);
blue = reshape(Data(i, 18433:27648), [96,96]);
im = cat(3,red, green, blue);
filename = strcat('output_folder/',num2str(i));
imwrite(im,filename,'png');
The values are hard coded because I only need to use this function on 96x96 images. Of course they should be made to variables, if you want to use it with different imagesets of different sizes
FULLFILE considers the correct line breaks under Windows and Linux. This might not be necessary currently. But it is a good idea to start writing code, which does not depend on the platform as early as possible.
Kind regards, Jan
Einstellungen und Berechtigungen
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.