在Matlab中以特定分辨率保存图像

问题描述 投票:2回答:2

我想用几个小时来简单地以特定分辨率(320x240)输出某个图。

  xmax = 320; ymax = 240;
  xmin = 0; ymin = 0;
  figure;
  set(gcf,'position',[1060 860 320 240]);
  axis([xmin,xmax,ymin,ymax]);
  plot(someLinesAndPointsInTheRange320X240);
  saveas(gca,outName,'jpg');
  export_fig(outName);

其中saveas以任意分辨率输出jpg图像。 export_fig仍然显示轴。

添加axis offaxis tight也无济于事。有人有想法吗?

更新: 问题已经解决了。为了完整起见,这是我目前的解决方案:

  xmax = 320; ymax = 240;
  xmin = 0; ymin = 0;
  figure;
  set(gcf,'position',[1060 860 320 240]);
  subaxis(1,1,1, 'Spacing', 0.01, 'Padding', 0, 'Margin', 0);  % Removes padding
  axis([xmin,xmax,ymin,ymax]);
  plot(someLinesAndPointsInTheRange320X240);
  axis([xmin,xmax,ymin,ymax]);

  set(gca,'xtick',[],'ytick',[]); % Removes axis notation
  I = frame2im(getframe(gcf)); %Convert plot to image (true color RGB matrix).
  J = imresize(I, [240, 320], 'bicubic'); %Resize image to resolution 320x240
  imwrite(J, 'outName.jpg'); %Save image to file
matlab
2个回答
3
投票

可能的解决方案是将图形转换为图像,并使用imresize

可以将图形位置固定为320x240分辨率,但使用imresize更简单(我认为)。

以下代码示例,将图转换为图像,并使用imrezie将分辨率设置为320x240:

figure;
% xmax = 320; ymax = 240;
% xmin = 0; ymin = 0;  
% set(gcf,'position',[1060 860 320 240]);
% axis([xmin,xmax,ymin,ymax]);

plot(sin(-pi:0.01:pi)); %Example figure
I = frame2im(getframe(gcf)); %Convert plot to image (true color RGB matrix).
J = imresize(I, [240, 320], 'bicubic'); %Resize image to resolution 320x240
imwrite(J, 'J.jpg'); %Save image to file

0
投票

有一个更简单的解决方案。

说你有你的图,gcf,你捕获框架,只需使用imresize编辑框架的对象属性cdata。

frame = getframe(gcf);
frame.cdata = imresize(frame.cdata,[240, 320]);

然后,您可以使用此帧编写视频,该帧现在具有指定的分辨率。

writeVideo(VideoObj,frame);

它运作得很好。

© www.soinside.com 2019 - 2024. All rights reserved.