MATLAB getframe

F = getframe(h,rect) specifies

   a rectangular area from which to copy the pixmap. rect is relative to the lower left corner of the figure or axes h,in pixel units. rect is a four-element vector in the form [left bottom width height], where width and height define the dimensions of the rectangle.

   本函数是要从图形对象中截取一块出来。其中h是图像句柄,rect是一个四元素向量,代表[a b c d],其中a表示截取区域到图像左端的像素数,b表示截取区域到图像底端的像素数,c表示截取区域水平像素数,d表示截取区域垂直向像素数。

   得到的F是一个结构体(struct),包含两个元素:cdata和colormap,其中cdata是截取区域的数据,是一个三维数组,其中第三维的长度是3,即截取到的是区域中RGB的值。colormap是调色板,这个没啥意思。

例如:

x=0:pi/100:2*pi;

y=sin(x);

plot(x,y)

set(gcf,'color',[1 1 1]) %设置背景色为白色

title('测试图像保存')

F=getframe(gcf); % 获取整个窗口内容的图像

F1=getframe;     % 获取坐标轴为界的图像

imwrite(F.cdata,'test1.png')

imwrite(F1.cdata,'test2.png')

getframe获得的是一个架构struct类型的数据,

其中cdata子域的内容才可以用imwrite内容保存,用F.cdata表示

getframe(gcf) 即get current figure,获得窗口内图像,包含legend、title以及label。如果不添加gcf,默认为gca(get current axis)。

imwrite 可以保存jpg、png等格式图像,gif是7.0添加的,但是好像没办法保存为动画,只能保存第一帧。

与在图像界面直接利用复制,或用file>save as...保存不同的是,imwrite的背景色为窗口实际颜色,默认为灰色(RGB表示为[.7 .7 .7]),若想保留白色背景图,需添加例子中的set gcf color命令。

原文地址:https://www.cnblogs.com/hyb221512/p/12848990.html