Combine multiple figure views in a single image for export

Dear all,

I have a series of source activations that I would like to export as images.

I was wondering if there is a straightforward procedure (or at least one :wink:) in Brainstorm to combine the different views of the cortex in a single figure (rather than multiple figure objects) and then export the result as a single image.

To make it clear, I would like to accomplish a result similar to what you can achieve using the subplot function in MATLAB.

In my case I would like to combine together the six views (L/R,Int/Ext, Top/Bottom).

Thank you for your help,
Antonio

This is something you can do with videos, but not with static images. If you have a lot of screen captures to make, you could write a script to call automatically the function out_figure_image for all the available figures. Something like:

hFigs = bst_figures('GetAllFigures');
for i = 1:length(hFigs)
    out_figure_image(hFigs(i), sprintf('image_%02d.png', i));
end

Look at the code of the function for additional options:
https://github.com/brainstorm-tools/brainstorm3/blob/master/toolbox/io/out_figure_image.m

If you want to print them on a black background in a similar way the videos, you can find some inspiration in out_figure_movie:
https://github.com/brainstorm-tools/brainstorm3/blob/master/toolbox/io/out_figure_movie.m#L196

Hi Francois thanks for your suggestion!

I don't know why but the out_figure_image function returns error when I set as filename extension .fig, so I was unable to export the data as a standard figure object even if it seemed to me the right thing to do based on the function help and the function source code (line 72).

Anyway, I came out with this script which kind of does what I want:

% Get all the figures displayed in the GUI
hFigs = bst_figures('GetAllFigures');

% Create a new figure and set it to invisible
newfig = figure;
newfig.Visible = 'off';

for i = 1:length(hFigs)
    
    % Store the Brainstorm image matrix (3D uint8 values) in a temporary
    % object
    tmp_bst_image = out_figure_image(hFigs(i), 'Figure', []);
    
    % Set the Brainstorm figure as not visibile
    hFigs(i).Visible = 'off';
    
    % Set the destination figure as the active one
    figure(newfig);
    
    % Write the image in a subplot
    subplot(2,3,i)
    imshow(tmp_bst_image);
    
    % Clear the temporary matrix and go the next image
    tmp_bst_image = [];
end

The problem that I am struggling to solve now is how to close the Brainstorm figure after I got all the necessary data. If I do not close it, the figure handles are still in memory, even if anything is displayed in Brainstorm and so when I re-run the script on a new set of images, I end up getting in the hFigs object bot the previous and the new images.

Do you have any other hint on how to deal with this problem?

Thanks!!

I don't know why but the out_figure_image function returns error when I set as filename extension .fig

The function out_figure_image.m function is part of Brainstorm and is used to make screen captures of images, not figures.
If you add the parameter 'Figure', it makes a copy of the figure without all the callbacks, and returns a handle to this new figure. Then you can save this figure to the hard drive directly with Matlab's function save(), but I'm not sure this exactly what you want to do here... If you want to get an image to show in a subplot, just do:

 img = out_figure_image(hFigs(i));

The problem that I am struggling to solve now is how to close the Brainstorm figure after I got all the necessary data. If I do not close it, the figure handles are still in memory, even if anything is displayed in Brainstorm and so when I re-run the script on a new set of images, I end up getting in the hFigs object bot the previous and the new images.

Setting the Visibilty doesn't close the figure, it just hides it.
To close a Matlab figure, call:

  close(hFigs(i));

To release all the temporary objects allocated in memory by Brainstorm (equivalent of clicking on the button "Close all figures and clear memory"):

  bst_memory('UnloadAll', 'Forced');
1 Like

Hi Francois,

thanks again for your help.

I followed your suggestion

and I managed to solve my problem.

Anyway, as you can see also from my previous post

that adding the parameter 'Figure' when calling out_figure_image does not return a figure handle without the callbacks as it should, but rather a 3D matrix.

I don't know why.

Cheers,
Antonio

Ah indeed, I read my own documentation in a wrong way...
Whenever you request an output, it returns the RGB image, no matter what the inputs are.
I don't think that generating copies of figure handles from a script is going to be a much used feature, so I will leave it this way.

If you need to get a copy of the figure and save a .fig file, you can do it by calling:

out_figure_image(hFigs(i), 'Figure');
savefig(gcf, 'filename.fig')

It happens to the best of us :smile:

As I said previously, I solved following your suggestion of calling directly the hFigs within my script to copy the figures in separate subplots.

Thanks for your help!!

Dear all,
I just have the same question here. I plot some source activation from three views (L/R/Top) and want to combine them in one single figure. Thank you for the discussion and I used the scripts in this discussion to put all the images in one figure. But I have a question that the resolution of the outputed figures is very low. Is there a way to improve?
Thank you very much!

Anqi

the resolution of the outputed figures is very low. Is there a way to improve?

The resolution of your figures depends only of the size in pixels of the windows.
Make the windows bigger for higher resolution of the graphics.

Note that the text is using fixed-size fonts, so increasing the size of the windows makes the text relatively smaller. If you want larger text in the screen captures, make the windows smaller.

If there is anything you'd like to improve in the figures, you can edit all the elements (text and graphics, programmatically or interactively (Right-click > Figure > Plot edit toolbar / Matlab controls).