How to save image as pixel by pixel size and without white background

10 views (last 30 days)
Tina Hsiao
Tina Hsiao on 7 Apr 2021
Commented: DGM on 7 Apr 2021
Hello, I would like to seek help for the image save as bmp.file with pixel by pixel size and without white background.
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',[])
% %-------save compensation hologram-------
name = 'hologram';
fpath = 'C:\Users\Admin\Desktop';
saveas(gca, fullfile(fpath, name), 'bmp');

Accepted Answer

DGM
DGM on 7 Apr 2021
Edited: DGM on 7 Apr 2021
Getting figures saved in a particular way is always a challenge, and the methods vary with versions. Personally, I just use export_fig()
set(gca,'units','pixels')
origpos=get(gca,'position');
origpos(3)=399; % 400-1
set(gca,'position',origpos)
outpict=export_fig('-a2','-m1');
This gives me a 400x400 close-cropped image, and should work so long as the figure window is big enough. You can just save the image as needed.
imwrite(outpict,'finallyworks.png')
  4 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!