Search code examples
imagematlabmatlab-figureresolution

Save a figure to file with specific resolution


In an old version of my code, I used to do a hardcopy() with a given resolution, ie:

frame = hardcopy(figHandle, ['-d' renderer], ['-r' num2str(round(pixelsperinch))]);

For reference, hardcopy saves a figure window to file.

Then I would typically perform:

ZZ = rgb2gray(frame) < 255/2;
se = strel('disk',diskSize);
ZZ2 = imdilate(ZZ,se); %perform dilation.
Surface = bwarea(ZZ2); %get estimated surface (in pixels)

This worked until I switched to Matlab 2017, in which the hardcopy() function is deprecated and we are left with the print() function instead.

I am unable to extract the data from figure handler at a specific resolution using print. I've tried many things, including:

frame = print(figHandle, '-opengl', strcat('-r',num2str(round(pixelsperinch))));

But it doesn't work. How can I overcome this?

EDIT

I don't want to 'save' nor create a figure file, my aim is to extract the data from the figure in order to mesure a surface after a dilation process. I just want to keep this information and since 'im processing a LOT of different trajectories (total is approx. 1e7 trajectories), i don't want to save each file to disk (this is costly, time execution speaking). I'm running this code on a remote server (without a graphic card).

The issue I'm struggling with is: "One or more output arguments not assigned during call to "varargout"."

getframe() does not allow for setting a specific resolution (it uses current resolution instead as far as I know)

EDIT2 Ok, figured out how to do, you need to pass the '-RGBImage' argument like this:

frame = print(figHandle, ['-' renderer], ['-r' num2str(round(pixelsperinch))], '-RGBImage');

it also accept custom resolution and renderer as specified in the documentation.


Solution

  • I think you must specify formattype too (-dtiff in my case). I've tried this in Matlab 2016b with no problem:

    print(figHandle,'-dtiff', '-opengl', '-r600', 'nameofmyfig'); 
    

    EDIT: If you need the CData just find the handle of the corresponding axes and get its CData

    f = findobj('Tag','mytag')
    

    Then depending on your matlab version use:

    mycdata = get(f,'CData');
    

    or directly

    mycdta = f.CData;
    

    EDIT 2:

    You can set the tag of your image programatically and then do what I said previously:

    a = imshow('peppers.png');
    set(a,'Tag','mytag');