Convert axis to image file/matrix

조회 수: 18 (최근 30일)
Joseph Henry
Joseph Henry 2019년 7월 30일
편집: Rik 2019년 8월 1일
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
KSSV
KSSV 2019년 8월 1일
How about
print(gcf,'test','-dpng','-r300');
Joseph Henry
Joseph Henry 2019년 8월 1일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2019년 8월 1일
편집: Rik 2019년 8월 1일
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
Rik
Rik 2019년 8월 1일
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 2019년 8월 1일
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!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by