How to Take Reference of Matlab's imagesc with

2019-04-17 17:10发布

Test code for the thread Why this imagesc-imshow with Colormap not Working in Matlab? which returns a structure of two fields cdata and colormap where cdata is an RGB representation of the figure

hFig=figure;
imagesc(time, potential, matrix); % choose any data

%% Test Code Starts
fr = getframe(hFig);
rgbImage = fr.cdata;
figure  
imshow(rgbImage) 
colormap parula 
%% Test Code ends

saveas(hFig, '/home/masi/image.eps', 'eps');

Output

  • without test code, saveas successful
  • with test code, saveas fails with blank output and you get successfully figure output on your screen

Expected output: saveas on the disk and show reference of the object on the screen. I do not understand the interruption caused by taking the reference of the imagesc object.

To export_fig

Suever's proposal. I do successfully following here

filenamePng=fullfile('/home/masi/image.png'); 
export_fig(filenamePng, '-png', hFig); % works with right resolution
export_fig(filenamePng, '-png', '-q101', hFig ); % works
export_fig(filenamePng, '-png', '-native', hFig);  % works but reduced resolution i.e. your screen resolution of 72 dpi here
export_fig(filenamePng, '-png', '-q101', '-native', hFig);  % works but reduced resolution

filename=fullfile('/home/masi/image'); 
% '-depsc' uses eps-3 so avoiding Matlab's buggy eps-2
% '-nocrop' for no cropping of the data
% '-a1' no anti-aliasing because we do not need it
export_fig(filename, '-png', '-eps', '-depsc', '-nocrop', '-q101', '-a1', hFig); % works 
% results in 0.6 MB image

% '-opengl' results in 12 MB image and aliasing so incomplete vectorisation in Matlab  
export_fig(filename, '-png', '-eps', '-depsc', '-nocrop', '-opengl', ...
    '-q101', hFig); % works 
% have -a1, much alias, 12 MB 
% have -a4, much alias, 34 MB and warning `Warning: print2array generating a 33.2M pixel image. This could be slow and might also cause memory problems.`

So do not use -opengl in Matlab. Also, think if you can use at all .eps reliably in Matlab. [Suever, Masi]

  • Without -opengl, is the rendering vectorised properly with -depsc? The size of the file is significantly smaller without noticebly aliasing. TODO test yourself. Ticket here.

System: Linux Ubuntu 16.04 64 bit
Hardware: Macbook Air 2013-mid
Linux kernel: 4.6
Linux kernel options: wl
Matlab: 2016a

1条回答
狗以群分
2楼-- · 2019-04-17 17:43

I suspect that it is an issue with saving the image as an EPS on your machine. MATLAB is pretty notorious for creating bad EPS files and if you actually want to use EPS files, I would suggest using export_fig from the MATLAB File Exchange.

Also, if you're going to be exporting to an EPS, it is recommended to not use the opengl renderer as that will typically result in very large EPS files that are not "vectorized" properly.

That being said, you don't really gain much from using an EPS in this instance. Since you have essentially take a screenshot of an image and then displayed this screenshot using imshow, you have already lost a lot of quality from your initial image. Instead, for raster data you can use a lossless image format such as a PNG to save your data with the added benefit that MATLAB is a bit more reliable at producing PNG files.

Also it may be worth using imwrite rather than imshow + saveas to save the image.

imwrite(fr.cdata, 'output.png')
查看更多
登录 后发表回答