Convert axis to image file/matrix

20 visualizzazioni (ultimi 30 giorni)
Joseph Henry
Joseph Henry il 30 Lug 2019
Modificato: Rik il 1 Ago 2019
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 Commenti
KSSV
KSSV il 1 Ago 2019
How about
print(gcf,'test','-dpng','-r300');
Joseph Henry
Joseph Henry il 1 Ago 2019
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.

Accedi per commentare.

Risposta accettata

Rik
Rik il 1 Ago 2019
Modificato: Rik il 1 Ago 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 Commenti
Rik
Rik il 1 Ago 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 il 1 Ago 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!!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by