我用它来获取和保存一个图形:
function sketch(cmd)
if nargin == 0
cmd = 'init';
end
switch cmd
case 'init'
fig = figure('DoubleBuffer','on','back','off');
info.ax = axes('XLim',[0 1],'YLim',[0 1]);
info.drawing = [];
info.x = [];
info.y = [];
set(fig,'UserData',info,...
'WindowButtonDownFcn',[mfilename,' down'])
case 'down'
myname = mfilename;
fig = gcbf;
info = get(fig,'UserData');
curpos = get(info.ax,'CurrentPoint');
info.x = curpos(1,1);
info.y = curpos(1,2);
info.drawing = line(info.x,info.y,'Color','k');
set(fig,'UserData',info,...
'WindowButtonMotionFcn',[myname,' move'],...
'WindowButtonUpFcn',[myname,' up'])
case 'move'
fig = gcbf;
info = get(fig,'UserData');
curpos = get(info.ax,'CurrentPoint');
info.x = [info.x;curpos(1,1)];
info.y = [info.y;curpos(1,2)];
set(info.drawing,'XData',info.x,'YData',info.y)
set(fig,'UserData',info)
case 'up'
fig = gcbf;
set(fig,'WindowButtonMotionFcn','',...
'WindowButtonUpFcn','')
saveas(gcf, 'test.png');
export_fig test2.png
end
我想将区域大小固定为50x50(50像素,50像素(,并且不带x-y轴。
我试过了:
x0=500;
y0=500;
width=50;
height=50;
set(gcf,'position',[x0,y0,width,height])
然而,它看起来不太好,并将图像保存为120x50像素。如何将图形保存为50x50像素?
看看我写的这个函数,它完全按照你的要求——绘制一个图形并将图像导出到指定大小的
https://github.com/fangq/iso2mesh/blob/v1.9.2/img2mesh.m#L516-L535
关键是使用这条线设置轴
ax=axes('parent',gcf,'Units','pixels','position',[1, 1, imsize(1), imsize(2)]);
其中单位是像素,并且通过CCD_ 1将轴宽度调整为期望的像素长度。完成此操作后,getframe
将获得图像。