【问题描述】:matlab使用getframe函数保存指定像素大小的图片。
使用 set(gcf, 'position', [124,124,800,800]);设置图片位置和大小,可将图片保存为256x256大小。
(代码第3行)
仅把'position'参数从124改成125时,不能保存为期望的256x256大小,反而是320x320大小,这是为什么??(代码第4行)
试试其他几组position参数,也会出现保存尺寸不对的情况,这是为什么? 对position有什么要求吗?不解
【代码】
- figure
- ppp=rand(256,256);
- FigurePos=[124,124];
- % FigurePos=[125,125];
- set(gcf, 'position', [FigurePos(1),FigurePos(2),800,800]); % 设置当前图窗的位置,屏幕左下方为原点,
- set(gcf, 'color', 'w'); % 背景颜色为白色
- set(gca, 'units', 'pixels'); % 设定单位为像素
- set(gcf, 'units', 'pixels');
- imagesc(ppp);
- set(gca, 'position',[5, 5,256,256]); % 确定画图区域的位置,即指定的分辨率
- set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')
- ax = gca;
- ax.Units = 'pixels';
- pos = ax.Position;
- img = getframe(gcf, pos);
- imgSize=size(img.cdata);
- imgSize=imgSize(1:2)
- % imwrite(img.cdata,'thisFigure.png');
- % close
- % FigurePos->imgSize
- % (121,121) ->(320x320)
- % (122,122) ->(320x320)
- % (123,123) ->(256x256)
- % (124,124) ->(256x256)
- % (125,125) ->(320x320)
- % (126,126) ->(320x320)
- % (127,127) ->(256x256)
- % (128,128) ->(256x256)
- % (129,129) ->(320x320)
- % (130,130) ->(320x320)
下图为随机生成的图片:
【解决思路】:
解决方法1:使用imresize函数
axis([xmin,xmax,ymin,ymax]); Img_re = imresize(Img, [256, 256], 'bicubic');
- close all
- xmax = 256;
- ymax = 256;
- xmin = 0;
- ymin = 0;
- figure;
- set(gcf,'position',[60 60 256 256]);
- % set(gcf,'color','none');
- % subaxis(1,1,1, 'Spacing', 0.01, 'Padding', 0, 'Margin', 0); % Removes padding
- imagesc(signalstrength_all);
- axis([xmin,xmax,ymin,ymax]); % 调整内部坐标轴显示范围,超出补零,设置为整数,否则出现0.5
- set(gca,'xtick',[],'ytick',[]); % Removes axis notation
- set(gca,'position',[0 0 1 1]);
- axis off
- h=gcf;
- h2=gca;
- Img = frame2im(getframe(gcf)); %Convert plot to image (true color RGB matrix).
- % 即 Img = getframe(gcf); 中的Img.cdata
- Img_re = imresize(Img, [256, 256], 'bicubic');
- imwrite(Img_re, 'outName.png');


雷达卡






京公网安备 11010802022788号







