Exporting from Matlab To PowerPoint

Reviewing the results of an image based experiment, across many images, can be annoying in matlab. Too much clicking!

I’ve recently started using PowerPoint to view many of my results. This blog posts discuss how using the free export to PowerPoint toolbox it is possible to create a slide desk with all relevant information for easier viewing. It looks like this:


image1_annotated_trimmed

Not a lot to this, so we’ll just jump into the code:

First things first, add the toolbox to the search path

  1. addpath(genpath('C:\Research\code\utils\exportToPPTX'));

Create a new PowerPoint presentation in memory, we don’t name it now, only when we save it:

  1. exportToPPTX('new');

In this case, I have output in the form of “xxxxx_prob.png”, where xxxxx is the base filename.
we need a list of all files for which there is output:

  1. files=dir('*prob.png');

Then for every file, we want to first add a new slide to the PowerPoint presentation for this output image. Then we’re going to load our output, the original image, the ground truth:

  1. files=dir('*prob.png');
  2. for zz=1:length(files)
  3.     exportToPPTX('addslide');
  4.     io_output=imread(files(zz).name);
  5.     io_gt=imread(strrep(files(zz).name,'prob','mask'));
  6.     io_orig=imread(strrep(files(zz).name,'_prob.png','.tif'));

To make the overlaid image, we take take advantage of matlabs imfuse toolbox. In our case, the output is half the size of the original input, so we need to resize it accordingly:

  1. io_fuse=imfuse(io_orig,imresize(io_output,2));

The last thing before making the slide is to compute whatever statistics we are interested in. In this case I’ve chosen area, which is simply the number of non-zero pixels in the image

  1. gt_area=nnz(io_gt);
  2. output_area=nnz(im2double(io_output(:,:,2))>.5);

With that done, we need to place things on the power point slide, this consists of telling it we’d like to add a new image and also the position of the image in the format indicating the upper left corner [ x y height width], so that it cannot only place it but resize it accordingly:

  1. exportToPPTX('addpicture',io_orig,'Position',[0 0 5 5]);
  2. exportToPPTX('addpicture',io_gt,'Position',[5 0 5 5]);
  3. exportToPPTX('addpicture',io_output,'Position',[5 5 5 5]);
  4. exportToPPTX('addpicture',io_fuse,'Position',[0 5 5 5]);

We can see that each of the images is kept the same size (5, 5), but we move around the upper left corners from { (0,0), (5,0}, (5,5),(0,5) }. We can do this in any order. Reading the documentation also adds the ability for different width and color edges.
We’d like to add some text information, at bare minimum the filename so we know what we’re looking at, as well as some arbitrary statistics we’ve computed. This can be done using a sprintf statement where a ‘\n’ character does insert a new line in the PowerPoint text box.

  1. exportToPPTX('addtext',sprintf('filename: %s \n GT area: %d \n Output Area: %d',files(zz).name,gt_area,output_area),'Position',[-4 0 5 5]);

you’ll notice that the x coordinate is actually negative implying that we’re not limited to solely the white space of each slide. In this case, to not obstruct the view of the images, I’ve moved it over. Other options are to change the color, size, angel, rotation, etc. if placing over the images, make sure to respect the layering order, i.e. things in the “front” should be put later, while things in the “back” should be put inserted first.
Once the all the image are inserted, the final line is:

  1. newFile = exportToPPTX('saveandclose',’results_output');

To save and close the PowerPoint presentation to disk. This saves the output to results_output.pptx.

Compressing the Presentation

One small caveat. The final file size is usually very large, because the images are not resized to the document resolution, but are inserted in their entirety. Thus an 8MB image in matlab is going to take 8MB in PowerPoint, even if you resize it to 100 x 100 pixels.

This can become a bit unwieldy, this slide deck of 9 slides is about 34MB, far too large for emailing.

PowerPoint has the ability to compress the images to various sizes, this can be done by clicking on an image, clicking the Format menu option, and then compress pictures:

image2_compress

From there select the appropriate compression options, usually it makes to de-select “apply only to this picture” and to select the “E-mail” options to really shrink things down for sharing.


image2_compression_options

Afterward, our file goes from 34MB to 11MB (a 70% reduction) without noticeably affecting the results.

That’s it!

Full code available here and again the toolbox is available here

One thought on “Exporting from Matlab To PowerPoint”

Leave a Reply

Your email address will not be published. Required fields are marked *