MATLAB Answers

Convert axis to image file/matrix

26 views (last 30 days)
Joseph Henry
Joseph Henry on 30 Jul 2019
Edited: Rik on 1 Aug 2019
Suppose I create an axis and draw several shapes on it from a .mat file:
function Image = draw_rois(filename)
myAx = axes('YDir','reverse', 'Units', 'Pixels', 'XLim', [0 800], ...
'Position',[39,40,800,450], 'YLim', [0 450]);
set(face1{2,1}, 'Parent', myAx)
set(face2{2,1}, 'Parent', myAx)
I would like to convert myAx to the same kind of matrix produced when you call imread on an image file. The following code sort of works, but it skews the aspect ratio and makes everything look terrible if it is reloaded:
F = getframe(myAx);
Image = frame2im(F);
imwrite(Image, 'axis.png')
I need to use this to determine which regions of an identically sized image fall into the shapes drawn here. Hence it would be best if I could just immediately go from axis to image matrix without having to save/print as a .png .jpg etc. Is this even possible?
Since the comments section got rather long, I've decided to move the working solution up here. I was able to get what I needed using export_fig, which I was helpfully directed to by Rik.


KSSV on 1 Aug 2019
How about
Joseph Henry
Joseph Henry on 1 Aug 2019
Is there any way to change the dimensions of the printed image though? This always seems to turn an 800 x 450 axis into a 2500 x 1406 png file, and I need the png to have the same dimensions as the axis.

Sign in to comment.

Accepted Answer

Rik on 1 Aug 2019
Edited: Rik on 1 Aug 2019
As KSSV mentions, print(gcf,'test','-dpng','-r300'); should do the trick if you want to print your entire figure content to a png file.
The print function relies on a dpi setting to determine how many pixels your figure is. You might be able to force the same pixel size by setting the paper properties of your figure to a different setting.
As an alternative you could consider the ScreenCapture FEX submission. It might be able to keep your figure resolution in an easy way. It also allows you to capture only the axis itself if you want that.
The export_fig function has a different mode of operation (it doesn't use Java for a screen capture), so it can be used as well. The downside is that it only works on entire figures, so the result has to be cropped afterwards.


Show 7 older comments
Joseph Henry
Joseph Henry on 1 Aug 2019
Thanks. The line you gave me did not work. I've uploaded the result below so you can have a better idea of what I'm talking about. Also I am not using multiple monitors, just saying that if my coworkers would like to use this code on their own computers, the changing dimensions issue would possibly reappear. I'm using the default toolbar location.
What is a MWE and where is the file exchange entry? (I'm assuming that's what FEX means) The MWE link you posted 404'd.
Rik on 1 Aug 2019
I made a typo which destroyed the Wikipedia link (should be fixed now). A 'minimal working example' (or actually in this case 'minimal failing example') helps diagnosing the issue by minimizing the lines of code that might be the root cause.
With FEX entry I simply mean the page on which you could download the function, so the link I provided in my answer.
Joseph Henry
Joseph Henry on 1 Aug 2019
Okay, export_fig seemed to do the trick. Going to try and finish this script, if all works well I'll mark this as accepted. Thank you!!

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by