-2

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

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • What if you use something other than an EPS? (You don't have line art here so an EPS is not useful). Try a PNG and see if you get the same result. – Suever Jul 07 '16 at 20:35
  • 1
    Also why are you creating an image, then taking a screenshot of that image, and then plotting the screenshot? This seems backwards – Suever Jul 07 '16 at 20:35
  • 1
    I suggested saving as something other than EPS as a test to see if it's an issue with printing to EPS. Try saving to a PNG and see if it's *still* black. Also if you actually open the EPS outside of MATLAB you'll see that for images saving as an EPS within MATLAB actually leads to very low quality results. A PNG is a lossless image format and your data isn't vector graphics. – Suever Jul 08 '16 at 11:30
  • @Masi I downvoted. Reason: You copy pasted some chuncks of code without understanding what they do. Basically, you didwhat Suever says in his 2nd comment, and didnt realize because you didnt read 10 lines of code you have.... Please, try to know what you are doing before asking. – Ander Biguri Jul 08 '16 at 16:03
  • 1
    the 12 MB image using `-opengl` makes sense and is what I was referring to before. Open it in an external EPS editing program (Adobe illustrator, etc.) and you'll see that MATLAB exports those EPS's as big strips and *not* as a "real" vector graphic. This is because Images are *not* vector graphics therefore PNG is better. – Suever Jul 09 '16 at 13:48
  • Just don't use the opengl renderer. That's the issue here. Also just use a PNG. – Suever Jul 09 '16 at 13:50
  • It's really irrelevant to the answer, it's just a comment on your commentary in your edit. – Suever Jul 09 '16 at 13:52
  • 1
    It depends you'll just have to test it yourself – Suever Jul 09 '16 at 13:54

1 Answers1

1

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')
Suever
  • 64,497
  • 14
  • 82
  • 101
  • 1
    @Masi Can you backup and start from the beginning? Tell me what your input data is and what you want as the output. Do you simply have matrix data that you want as an image? – Suever Jul 08 '16 at 13:35
  • 1
    @Masi Just read the help for `export_fig` it's quite clear. You can substitute it into your initial code in place of the `saveas`. – Suever Jul 08 '16 at 13:46
  • @Masi It should not change any of that but you haven't shown enough of your code to determine why. – Suever Jul 08 '16 at 13:47
  • @Masi If you're going to save as a PNG there is no need to use `export_fig`. If you are going to save as an EPS, use `export_fig` – Suever Jul 09 '16 at 01:07
  • 1
    @Masi The native resolution setting for `export_fig` uses your screen resolution (72 dpi). It looks like you have found a resolution that works for you. I believe this question is complete. If you have more questions about the inner workings of `export_fig` that really a different question. – Suever Jul 09 '16 at 13:11
  • Your initial question was "why is the image all black when printing to EPS". That has been answered. It's up to you to figure out how to get the quality you need for whatever tests you aren't showing us. If you can't that's another question – Suever Jul 09 '16 at 13:28
  • Yes, of course, I want to add more value to the thread by solving related things. Leaving bugs here does not impress me. – Léo Léopold Hertz 준영 Jul 09 '16 at 13:29