Convert axis to image file/matrix

Suppose I create an axis and draw several shapes on it from a .mat file:
function Image = draw_rois(filename)
figure
myAx = axes('YDir','reverse', 'Units', 'Pixels', 'XLim', [0 800], ...
'Position',[39,40,800,450], 'YLim', [0 450]);
load(filename)
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?
EDIT/SOLUTION:
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.

2 Comments

How about
print(gcf,'test','-dpng','-r300');
Thanks!
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
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.
Edit:
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.

10 Comments

Joseph Henry
Joseph Henry on 1 Aug 2019
Edited: Joseph Henry on 1 Aug 2019
Thanks!
Even if I change the figure position, the resulting image has different dimensions.
This works, but I guess what would be more ideal is if I could skip the last part of saving to a file. I really only need the matrix-format of the image, as I need to determine how many fixations from eye tracking data fall into the shapes I've drawn.
The GUI you mentioned looks helpful, but again I only need the image matrix. Furthermore, I'd like to be able to do this all automatically with code, as we have almost 16000 points of data across 90 participants. It seems like GazeAlyze does something similar to what I need, but the program does not work on R2019a and there are enough lines of code involved with that project that I'd rather not sort through them all to find the handful I need.
You should be able to get the image matrix with the function I linked. Have you looked through the documentation?
Ah, sorry. I stopped reading as soon as I saw GUI. You are indeed correct, thanks for the info!
You're welcome. If my suggestion solved your issue, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer.
The screencapture function works, but not very well. It takes a lot of finicking to capture the correct region (as I believe this is a screenshot program and not directly intended to capture the axis). Any of the following did not capture the correct region for me:
I = screencapture(gcf);
I = screencapture(gca);
I = screencapture(myAx, myAx.Position); % name of the axis I created using axes()
I = screencapture(gca, myAx.Position);
I = screencapture(gca, [50,50,800,450]); % this is the position of myAx and gca
I = screencapture(gcf, [50,50,800,450]);
What does work is a seemingly random set of values for position:
I = screencapture(myAx, [19,-198,999,561]);
It took me a lot of trial and error to figure out exactly what position should be input. I'm also positive these values will change if I use a different computer/monitor.
Any other suggestions? I really appreciate the advice so far!
Unless the two set calls change the size, I would have expected the line below to work.
I = screencapture(gcf, [39,40,800,450]);
If that doesn't work, I would suggest you make a MWE, post that here in a comment, and post a link to this thread in the FEX entry.
Multiple monitors and non-standard locations of the task bar can cause issues, so it is probably a good idea to also include that information. I also realized display scaling might cause this function to misbehave (since the sizes you mention are relatively close to 125%), so that might explain it as well. The first cause shouldn't be an issue, and the second one is just thinking out loud, so your mileage may vary.
Another route may be to use the export_fig function and crop that down. I glanced through its code, and despite being maintained by the same author, it seems to be using a totally different strategy (except for the imclipboard function included in both).
Joseph Henry
Joseph Henry on 1 Aug 2019
Edited: 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.
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.
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)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Asked:

on 30 Jul 2019

Edited:

Rik
on 1 Aug 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!