MATLAB中显示图像都是在figure上显示,而figure一般显示的图像有很大的空白区域,如果不进行任何处理,在写论文时候,这个空白区域会很占据地方,让人头疼,那如何处理呢,程序如下:
直接用imwrite保存显示图片:
原图:
代码:
figure(3);imshow(Im3a);set(gca, 'position', [0 0 1 1 ]);axis normal;
rectangle('Position',[300,200,100,200],'EdgeColor','g','LineWidth',3);
AFrame=getframe(gcf);
imwrite(AFrame.cdata,strcat('C:\Users\Administrator\Desktop\234\',num2str(1),'.jpg')) %存储调整过大小的图片
close(gcf);
保存的结果:
以上是对显示图像如何没有空白区域的,对于其他的函数图像,如单纯只是plot之类的绘图,又有坐标轴之类的,如何使绘制的图像非常紧凑的显示,使得空白区域最少呢,对于只有一个图,不是subplot显示的,程序如下:
clear
clc
x=1:1:1000;
y=sin(x);
figure(1);
plot(x,y,'r.')
legend('相机1','相机2','相机3')
title('偏离度曲线','Fontsize', 10);
% set(gcf,'Fontsize',16);
set(gca,'FontSize',10);
set(gca, 'Position', get(gca, 'OuterPosition') - get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]);
saveas(gcf,['C:\Users\Administrator\Desktop\234\','test1.jpg'])
结果如下:
左边是显示的图,右边是保存的图,可见,已经尽可能多的使空白区域最少了。下面是针对于由多个子图的,一般用的subplot,程序及结果如下:
clear;clc;
x=1:0.001:2*pi;
y=sin(x);
Color=[1 0 0;0 1 0;0 0 1;1 1 0;1 0 1;0 1 1;1 1 1];
figure('color', [1, 1, 1], 'position', [0, 0, 800,400]); % 为区分边界,将底色改为灰色
set(gcf, 'InvertHardCopy', 'off'); % 让设置的背景色有效
sub_row = 3; % 子图行数
sub_col = 3; % 子图列数
for i_row = 1 : sub_row
for j_col = 1 : sub_col
order = (i_row-1)*sub_col+j_col; % 子图的顺序
subplot(sub_row, sub_col, order);
plot(x,y,'g.');
RemoveSubplotWhiteArea(gca, sub_row, sub_col, i_row, j_col); % 去除空白部分
end
end
% 对应子函数:
function [] = RemoveSubplotWhiteArea(gca, sub_row, sub_col, current_row, current_col)
% 设置OuterPosition
sub_axes_x = current_col*1/sub_col - 1/sub_col;
sub_axes_y = 1-current_row*1/sub_row; % y是从上往下的
sub_axes_w = 1/sub_col;
sub_axes_h = 1/sub_row;
set(gca, 'OuterPosition', [sub_axes_x, sub_axes_y, sub_axes_w, sub_axes_h]); % 重设OuterPosition
% TightInset的位置
inset_vectior = get(gca, 'TightInset');
inset_x = inset_vectior(1);
inset_y = inset_vectior(2);
inset_w = inset_vectior(3);
inset_h = inset_vectior(4);
% OuterPosition的位置
outer_vector = get(gca, 'OuterPosition');
pos_new_x = outer_vector(1) + inset_x; % 将Position的原点移到到TightInset的原点
pos_new_y = outer_vector(2) + inset_y;
pos_new_w = outer_vector(3) - inset_w - inset_x; % 重设Position的宽
pos_new_h = outer_vector(4) - inset_h - inset_y; % 重设Position的高
% 重设Position
set(gca, 'Position', [pos_new_x, pos_new_y, pos_new_w, pos_new_h]); %(此程序摘自博客 http://blog.csdn.net/shanchuan2012/article/details/53980288 )
左边为运行结果图,右边为保存结果图,对于显示多个子图的这个,当超过四五行的时候会出现下图所示的有些子图不显示的问题,该问题不知道怎么解决,但大家可以 http://www.ilovematlab.cn/thread-453586-1-1.html 这个网址去看看。
好,今天整理到这,希望对大家有帮助。