16

I'm exporting an image in Matlab using the eps format, but it smooths the image. Matlab does not blur the image using other formats such as png. I would like to know how to export a non-blurry image with eps format. Here is the resulting image using png: enter image description here

And here is the resulting image using eps: enter image description here

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
Gorayni
  • 606
  • 2
  • 8
  • 26

10 Answers10

11

UPDATE:

The problem is reproducible on a Mac, and the issue is with the eps renderer rather than MATLAB. For e.g., saving imagesc(rand(20)) and viewing with Preview and GSview results in the following:

Preview screenshot

enter image description here

GSview screenshot

enter image description here

Clearly, the information is not lost. It is just not interpreted/read correctly by some EPS viewers. The solution is simple: use GSview to view your eps images. You can download it from here

On Macs especially, if your end application is latex/pdflatex, you will have to explicitly set it to use GS/GSview, because otherwise, it will default to the Quartz engine, which is baked into the OS.


PREVIOUS ANSWER:

I am unable to reproduce the behavior your described. Here is the code I used, tested using R2010b on WinXP 32-bit:

M = fspecial('gaussian',[20 20],5);
imagesc(M)
print('-dpng','a.png')
print('-depsc2','b.eps')

a.png

a.png

b.eps

b.eps

Perhaps this is an issue with your EPS viewer...

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • Great answer, but can you please elucidate how to `explicitly set it to use GS/GSview` for `LaTeX`? I tried googling, but didn't find a way of doing so. Thanks! – Sibbs Gambling Mar 09 '15 at 14:57
  • 1
    @SibbsGambling: I'm mostly a Windows user, the Mac part of the answer was edited by yoda at the time (who has since changed account to [r.m.](http://stackoverflow.com/users/3730911/r-m) if I'm not mistaken)... I guess what he meant was to associate PDF and EPS files to open with GSView by default instead of the default viewer in OSX (which apparently does some sort of anti-aliasing which explains the blurry looking images). Unlike other OSX apps (Preview, Keynote, ..), GSView seems to have its own rendering engine and apparently does not use the one provided by the system (Quartz). – Amro Mar 10 '15 at 00:14
  • I see. Thanks a lot. I only found the `Ghostscript` option in `Distiller`. Is it the right place? If it is the right place, then it is weird, since I have already ticked `Ghostscript ` and unticked `Apple Distiller`, but the .eps is still blurry. – Sibbs Gambling Mar 10 '15 at 03:37
  • @Amro In which system did you use to open the files? I think I need to have Ubuntu Linux with GSview in VirtualBox, just for previewing .eps files in OS X El Capitan. – Léo Léopold Hertz 준영 Mar 21 '16 at 07:16
  • 1
    @Masi actually I'm on Windows where I use any of [GSView](http://pages.cs.wisc.edu/~ghost/gsview/), [SumatraPDF](http://www.sumatrapdfreader.org/), or [Evince](https://wiki.gnome.org/Apps/Evince) to open PostScript files (they also handle PDF). None of them show any blurriness.. Perhaps you can install Evince on OSX (I think it's available in both homebrew and fink). btw the mac part of the answer is not me :) – Amro Mar 21 '16 at 08:16
  • @Amro I profiled Evince with my test signals. It loses data in opening .eps files. Do you have more recommendations for .eps viewers? – Léo Léopold Hertz 준영 Mar 21 '16 at 14:31
  • 1
    @Masi did you try the mac version of GSView as originally suggested (it uses ghostscript as backend)? – Amro Mar 21 '16 at 18:47
  • @Amro No. They have servers down so I could not download their images. I instead set up Ubuntu Linux in VirtualBox where I use `gv` directly. I need to have several benchmarks such that I can evaluate each viewer's quality. I try to get some standard built-image of GSView working when they are hosted for OS X. – Léo Léopold Hertz 준영 Mar 21 '16 at 19:35
5

not sure why it works but you can try doing the following:

eps2eps oldfile newfile

does the trick for me (on a mac os)

Jean-Paul
  • 19,910
  • 9
  • 62
  • 88
avigoz
  • 51
  • 1
  • 1
4

At first I thought you were doing something incorrectly, but then I remembered that this was an issue that was bothering the hell out of me a year or so ago. I couldn't come up with a way to "fix" this behaviour and from what I've researched, this is most likely a bug and several others have had this problem too and there is no known solution. Of course, I could be wrong about the last part and there might be solutions out there that have come out since I looked for them.

Any way, my workaround this problem was to use pcolor with shading flat instead of imagesc. When you export this to an eps format it preserves the image correctly. Example:

pcolor(rand(20));
shading flat
print('-depsc','figure.eps')

enter image description here

NOTE: You might see the appearance of thin, faint white lines along the anti-diagonals of each little square (depends on the OS & viewer). These are the edges of the graphics primitives that are used to render the image. However, this is not a flaw in MATLAB's export, but rather a fault in rendering in your EPS/PDF viewer. For e.g., with the default settings in Preview on my mac, these lines show up, whereas with the default in Adobe Reader 9.4, they don't appear.

Community
  • 1
  • 1
abcd
  • 41,765
  • 7
  • 81
  • 98
  • This works perfectly! No faint white lines appear when using LaTeX's `epstopdf` conversion. Thanks a lot! – Jean-Paul Jun 03 '15 at 09:53
2

If anyone is still interested in a workaround: Open the .eps-file with text editor and search for "interpolate". You'll probably find "/Interpolate true def" two or three times. Replace "true" with "false" and be happy :)

Tim
  • 29
  • 1
1

I've been long struggling with this problem as well. So far, GSView is the only viewer I've found that displays the eps figures produced by Matlab (R2015b) correctly. eps2eps did not work for me (psutils 1.23). The following eventually worked for me:

  1. Export the figure to pdf, following the instructions here
  2. pdf2ps file.pdf file.eps
Johannes
  • 11
  • 1
  • Please, do not use `saveas`. It is very buggy in Matlab 2016a. The command `export_fig` is much more robust https://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig – Léo Léopold Hertz 준영 Jul 09 '16 at 10:26
1

A note regarding Yoda's answer: in Preview in Mac OS X, you can make the thin white diagonal lines across each of the squares disappear by unchecking "Anti-alias text and screen art". Of course, the downside is that then any text (e.g. figure axes, etc) is not anti-aliased. Unfortunately, unchecking that has no effect on blurriness if you're using imagesc.

Another note is that if you use preview to make a pdf from your eps, the resulting pdf still displays correctly (non-blurry) when you open it in Acrobat.

carmel
  • 11
  • 1
  • Very interesting notice! How can you uncheck *Anti-alias text and screen art* in Preview's Settings? I cannot find it in El Capitan. Do you have any picture what the white diagonal lines look like. I am very interested in seeing their appearance. I opened a new thread about your claim here http://apple.stackexchange.com/q/231745/15504 – Léo Léopold Hertz 준영 Mar 21 '16 at 07:05
0

Apply opengl renderer to the figure

figure(gcf);
set(gcf,'renderer','opengl');
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
user1165814
  • 405
  • 4
  • 5
0

I just wrote this simple drop-in replacement for imagesc. It doesn't support all but the most basic features, but I still hope it helps.

function h = imagesc4pdf(C)

[ny nx] = size(C);

px = bsxfun(@plus, [-0.5; 0.5; 0.5; -0.5], reshape(1:nx, [1 1 nx]));
py = bsxfun(@plus, [-0.5; -0.5; 0.5; 0.5], 1:ny);

n = numel(C);
px = reshape(repmat(px, [1 ny 1]), 4, n);
py = reshape(repmat(py, [1 1 nx]), 4, n);

h = patch(px, py, reshape(C,1,n), 'linestyle', 'none');

xlim([.5 nx+.5]);
ylim([.5 ny+.5]);
set(gca, 'ydir', 'reverse');
marton78
  • 3,899
  • 2
  • 27
  • 38
0

This page helped me a lot: http://tech.mof-mof.co.jp/blog/machine-learning-octave.html (written in Japanese, please use google translate for it)

And this is also helpful: Octave-Gnuplot-AquaTerm error: set terminal aqua enhanced title "Figure 1"...unknown terminal type"

I also answered at https://www.coursera.org/learn/machine-learning/discussions/weeks/2/threads/Dh-aRfqSEeaHSQ6l4xnh6g.

I reinstalled gnuplot like this:

$ brew cask install xquartz
$ brew cask install aquaterm
$ brew uninstall gnuplot
$ brew install gnuplot --with-aquaterm --with-x11 --with-qt  # you can show other options by `$ brew options gnuplot`

You may edit ~/.octaverc like this:

setenv("GNUTERM", "qt")

and in octave window, after typing "system gnuplot", then

set pm3d interpolate 2, 2

After saving the file, open octave-cli.app, and type

imagesc(magic(3)), colorbar

I got this.

enter image description here

peketamin
  • 151
  • 1
  • 2
  • 10
0

The blurring actually depends on the rendering software your viewer application or printer uses. To get good results all the time, make each pixel in your image an 8x8 block of pixels of the same color, i.e. resize the image like this:

im2 = imresize(im1, 8, 'nearest');

The blurring then only affects the pixels at the edge of each block. 8x8 blocks are best as they compress without nasty artifacts using DCT compression (sometimes used in eps files).

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
user664303
  • 2,053
  • 3
  • 13
  • 30