WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Extract all *.png images from one *.mat file

 

userf

Gast


Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 25.03.2015, 16:25     Titel: Extract all *.png images from one *.mat file
  Antworten mit Zitat      
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: ---
     Beitrag Verfasst am: 25.03.2015, 17:01     Titel: It should be something like this
  Antworten mit Zitat      
it should be something like this (maybe you can help me with correcting the code in matlab language)

Code:

function my_function(matrix, path_to_folder)

    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

end
 
 
Jan S
Moderator

Moderator


Beiträge: 11.057
Anmeldedatum: 08.07.10
Wohnort: Heidelberg
Version: 2009a, 2016b
     Beitrag Verfasst am: 26.03.2015, 09:19     Titel: Re: Extract all *.png images from one *.mat file
  Antworten mit Zitat      
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');

Kind regards, Jan
Private Nachricht senden Benutzer-Profile anzeigen
 
userf

Gast


Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 26.03.2015, 10:06     Titel:
  Antworten mit Zitat      
Thanks a lot for your reply.

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 Smile
 
userf

Gast


Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 26.03.2015, 11:37     Titel:
  Antworten mit Zitat      
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

for i = 1:size(Data,1)

    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');

end


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
 
Jan S
Moderator

Moderator


Beiträge: 11.057
Anmeldedatum: 08.07.10
Wohnort: Heidelberg
Version: 2009a, 2016b
     Beitrag Verfasst am: 26.03.2015, 13:20     Titel:
  Antworten mit Zitat      
Hi userf,

And a small simplification:

Code:
FileData = load('path_to_folder/my_file.mat');
Data = FileData.X;  % to be renamed

for i = 1:size(Data,1)
    im = reshape(Data(i,1:27648), [96,96, 3]);
    filename = fullfile('output_folder', num2str(i));
    imwrite(im,filename,'png');
end

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
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

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
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2025 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

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.