How to save image pixel size larger than large than 343x343

2 views (last 30 days)
Tina Hsiao
Tina Hsiao on 7 Apr 2021
Commented: Tina Hsiao on 8 Apr 2021
Hello, I would like to seek help from you. The image pixel size is already could save pixel by pixel, but when setting save pixel by pixel large than 343. (Such as save pixel size 400x400). The image still saves in 343x343. But if save the image pixel size below 343x343 (Such as 299x299). The image save pixel size 299x299.
The code as below,
clear all; close all; clc
X = [-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150;-150,-125,-100,-75,-50,-25,0,25,50,75,100,125,150];
Y = [-150,-150,-150,-150,-150,-150,-150,-150,-150,-150,-150,-150,-150;-125,-125,-125,-125,-125,-125,-125,-125,-125,-125,-125,-125,-125;-100,-100,-100,-100,-100,-100,-100,-100,-100,-100,-100,-100,-100;-75,-75,-75,-75,-75,-75,-75,-75,-75,-75,-75,-75,-75;-50,-50,-50,-50,-50,-50,-50,-50,-50,-50,-50,-50,-50;-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25,-25;0,0,0,0,0,0,0,0,0,0,0,0,0;25,25,25,25,25,25,25,25,25,25,25,25,25;50,50,50,50,50,50,50,50,50,50,50,50,50;75,75,75,75,75,75,75,75,75,75,75,75,75;100,100,100,100,100,100,100,100,100,100,100,100,100;125,125,125,125,125,125,125,125,125,125,125,125,125;150,150,150,150,150,150,150,150,150,150,150,150,150];
r_M = [27,19,12,8,3,3,1,0,0,3,9,15,23;30,20,14,9,5,3,1,0,0,3,7,12,21;33,23,15,11,6,3,2,1,2,4,7,13,20;33,23,15,12,7,4,2,2,2,4,7,12,19;30,20,16,13,8,5,3,3,4,5,7,12,20;27,20,16,14,9,6,4,4,5,5,7,12,21;27,20,16,14,8,5,3,3,4,4,6,11,20;27,19,15,13,7,5,3,2,3,4,6,12,21;25,18,14,12,6,4,2,2,3,4,6,11,21;28,19,15,13,7,4,2,2,3,4,7,13,23;29,19,15,13,8,5,3,2,2,4,7,15,25;33,22,17,14,8,5,3,2,2,3,5,12,23;37,25,19,15,8,6,4,2,1,2,2,10,19];
figure(3)
contourf(X,Y,r_M,2560,'LineColor','none')
shading interp
axis square
% colorbar
% caxis([0 4])
colormap(flipud(gray(256)));
set(gca,'xtick',[])
set(gca,'ytick',[])
set(gca,'units','pixels')
origpos=get(gca,'position');
origpos(3)=399; % 400-1 (setting pixel size)
set(gca,'position',origpos)
outpict=export_fig('-a2','-m1');
imwrite(outpict,'finallyworks.png')

Accepted Answer

DGM
DGM on 8 Apr 2021
Edited: DGM on 8 Apr 2021
As I mentioned, doing it this way is a bit fiddly. It relies on the figure window being big enough. I've already noticed that there tends to be a 1px offset depending on versions, so I'm not even sure that my results would match yours. I'm sure there's a convoluted workaround with setting figure properties, but maybe we should avoid that if it's being troublesome.
If you have Image Processing Toolbox, you can just export the image sufficiently oversized, and then rescale it directly so that it's exactly the size you want. It's slower, but it's not as prone to breaking.
contourf(X,Y,r_M,2560,'LineColor','none')
shading interp
axis square
% colorbar
% caxis([0 4])
colormap(flipud(gray(256)));
set(gca,'xtick',[])
set(gca,'ytick',[])
set(gca,'units','pixels')
outpict=export_fig('-a4','-m2'); % export a bigger image
outpict=imresize(outpict,[400 400]);
imwrite(outpict,'finallyworks.png')
If you don't have IPT installed, the MIMT (link below) has imresizeFB(), which should act as a direct replacement for imresize() in this case:
outpict=imresizeFB(outpict,[400 400]);
If someone else has some elegant, quick, and robust way to do this, I'd like to hear it.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by