text:
Code covered by the BSD License

### Highlights from Myaa - My Anti-Alias for Matlab

4.7027
4.7 | 37 ratings Rate this file 161 Downloads (last 30 days) File Size: 268 KB File ID: #20979 Version: 1.0

# Myaa - My Anti-Alias for Matlab

### Anders Brun (view profile)

05 Aug 2008 (Updated )

Better looking Matlab figures with anti-aliasing!

### Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

File Information
Description

Add myaa.m to your path and enjoy anti-aliased professionally looking graphics in Matlab at any time. Myaa works with any kind of graphic (3-D, plots, scatterplots, ...) and even adds anti-aliasing to text, ui controls and grids. Myaa is ideal for complex, cluttered and saturated plots.

See attached screenshot for a demonstration. More examples included in the code, just run 'help myaa'.

Curiosa:
For those of you who 'publish' your code often, an undocumented anti-aliasing option is included in the snapnow.m function in Matlab. To publish a file called test.m you can do:

opts.figureSnapMethod = 'antialiased';
publish('test.m',opts)

However, you will have more control over the process using myaa, which is also the best choice when using Matlab interactively.

In any case: Anti-aliasing is good for you! :-)

Acknowledgements

This file inspired Aacam Realistic Depth Of Field Rendering In Matlab 3 D Figures and Export Fig.

MATLAB release MATLAB 7.6 (R2008a)
26 Sep 2014 Wolfgang

18 Sep 2014 Kwin

### Kwin (view profile)

06 Aug 2014 Christopher Jones

### Christopher Jones (view profile)

There is a problem that non-visible components of a gui are made visible during the process and appear in the saved aa-image. I cannot quite work it out.
It happens in this row:
print(self.source_fig,['-r',num2str(1*screen_DPI*self.K(1))], '-dpng', tempfile);

Appart from that, for non GUI export I love this function to bits!

15 Oct 2013 Arnaud

### Arnaud (view profile)

goooood

21 Jun 2013 Masoud Ghanbari

### Masoud Ghanbari (view profile)

Interesting Trick ;)

06 Mar 2013 Garrie

### Garrie (view profile)

Hi there,

Like the images that this function produces, but is there any way to get it so that the plot tools do not disappear and options like save etc. Tried to save many of these as eps files, but they don't save as well as normal matlab plots.

Any ideas?

Comment only
28 Dec 2012 Rafael

### Rafael (view profile)

I'm having trouble with vertical plot labels using the LaTex interpreter. A command like this:

ylabel('$v$ (\textit{pixels})','Interpreter','LaTex','FontSize',12);

Doesn't render correctly using myaa. The v displays correctly, but the "(pixels)" just disappears. The xlabel works correctly though.

Any ideas how to get this right?

Comment only
13 Dec 2012 Gaszton

15 Oct 2012 mark

### mark (view profile)

12 Sep 2012 Anders Brun

### Anders Brun (view profile)

if you like myaa, please have a look att aacam to! :-)

Comment only
17 Jul 2012 John Blommers

### John Blommers (view profile)

There appears to be a bug. Under certain conditions of the arguments raw_lowres is undefined and myaa errors out. See below:

myaa(4,8);
Undefined function or variable "raw_lowres".

Error in myaa (line 226)
sz = size(raw_lowres);

Comment only
06 Jun 2012 Jessica Kalynn

### Jessica Kalynn (view profile)

Figure looks fabulous but when I use the saveas function on it, it only saves the bottom right corner (assuming because the image is quite large). Help?!

I used this snipit of code:

[f1, cdata] = myaa([4, 2]);
imwrite(cdata, 'runtest.png', 'png');
winopen('runtest.png');
saveas(gcf,'Crisium','jpg');

Comment only
20 Mar 2012 sam

### sam (view profile)

nice one!

10 Mar 2012 Sashankh Rao

### Sashankh Rao (view profile)

Great tool! Thanks! Quick question: I have dashed lines (--)in my plot. When I use this tool it returns a straight line. Is there a fix for this?

28 Sep 2011 Daniel

### Daniel (view profile)

22 Aug 2011 Natasha

### Natasha (view profile)

Only reason doesn't get 5 stars is cause saving isn't built in

02 Jun 2011 ubuntu21 T

### ubuntu21 T (view profile)

Thank you for sharing this nice code.

To easily have the image saved on my hdd, I modified 2 lines in original code:

1. 192# %delete(tempfile);
meaning that I have commented this line, such that the file with the image should not be removed

2. 182# tempfile = ['myaa_temp_screendump ',num2str(rand(1)*1e5,'%5.0f'), '.png'] ;
meaning that each time I run myaa.m, the image name should change, ending with 5 random numbers. Therefore, I do not overwrite the same image file each time I run myaa.m

13 Jan 2011 A

### A (view profile)

Can someone remind me how to execute myaa.m? when I viewed the source code. it says I can run it simply as "myaa;" and it will operate on the "current figure". As a tests. I cropped the screenshot that came in the zip and saved the unfinished left side as it's own jpg. I then used P = imread(<filename>) to import the jpg I just saved, imshow(P) to put the images as the "current figure" then myaa. figure 1 was the orignal image as I expected but figure two (the output of myaa) was the same eyesore image I loaded. It's as if myaa did not get applied to it.

the sample code I've been running is:

imshow(I);
myaa;

Comment only
11 Jan 2011 Thomas

### Thomas (view profile)

Since this discussion seems to be still up to date...

Why not simply override the rendering settings for matlab in the graphics driver?

Using my nVidia card i simply start the nVidia control center, add matlab.exe as a
program using custom settings and choose "Override all application settings" (i hope this translation from my german graphics driver is correct) and set

Antialiasing-transparency = Supersampling
and
Antialiasing-setting = 4 or 8 or ....

I cannot tell if it works well with everything you can plot in matlab - but so far it does.

Comment only
22 Dec 2010 Timothée

### Timothée (view profile)

Hi, Thanks for this tool, the rendering is great, however I have a problem with the text font size. In contrast with what David said (Jan 2010), mine is absolutely tiny (actually totally unreadable), which is really too bad.

Is there a way to fix this ?

To add a few code , it will improve much.

1. Double resolution of temp PNG

185# print(self.source_fig,['-r',num2str(2*screen_DPI*self.K(1))], '-dpng', tempfile);

2. Then, get handle of image in figure

231# hi = image(raw_lowres);
... hi = image(...
...

3. Save image matrix

290# elseif nargout == 2
varargout(1) = {fig};
varargout(2) = {get(hi, 'CData')};

%%%%%%%%%%%%%

Test program

surf(peaks(60) ,'FaceColor','interp', 'FaceAlpha',0.8,'EdgeAlpha',0.8, 'LineWidth', 0.2);

set(gcf, 'Color', [1,1,1]);
[f1, cdata] = myaa([4, 2]);

imwrite(cdata, 'runtest.png', 'png');

winopen('runtest.png');

It's high resolution picture !

Comment only
01 Sep 2010 stephen Strenn

### stephen Strenn (view profile)

Great tool. However I seem to get the following error for figures larger than, say, 640x480. Any suggestions?

??? Error using ==> conv2
Out of memory. Type HELP MEMORY for your options.

Error in ==> myaa at 198
mm = myconv(ones(size(self.raw_hires(:,:,1))),kk,'same');

28 Jul 2010 Sara

### Sara (view profile)

Awesome, it's great, thank you;-)

12 May 2010 Jonas Lundgren

### Jonas Lundgren (view profile)

Thank you! This is useful.

17 Feb 2010 Dave Holden

### Dave Holden (view profile)

17 Jan 2010 Rob Campbell

### Rob Campbell (view profile)

Nice. This is the sort of thing that should come built in to Malab. In general, the display of 3-D plots in Matlab leaves much to be desired.

16 Jan 2010 David

### David (view profile)

Hello Anders,

Very nice thank you!

I found it increases the text size of the original graphic - is this expected or have I found a 'feature'?

Thanks again,
David.

30 Nov 2009 Daniel Armyr

### Daniel Armyr (view profile)

I give it a 4 out of five becaus it is slow as a doornail.
I was wondering about one step that might not be strictly necessary, and that is line 198
mm = myconv(ones(size(self.raw_hires(:,:,1))),kk,'same');
Here you do a convolution on what is normally a huge matrix filled with just ones. I assume you do this to get the edge behaviour. If that is the case, you could just do it on a smaller matrix and then expand the middle row and middle column to get a larger image. My huch says that will save you an entire second of the 8 it takes on my comp.

Also, I think there is potential in using the grabframe() instead of print, but I see problems in making a high-res copy without side-effects, so that one will have to simmer a bit.

27 Aug 2009 Linda F

### Linda F (view profile)

14 Jul 2009 cartonn

### cartonn (view profile)

great submission! How do you set a high super-sampling factor, a low down-sampling factor, and still get the full image? When I do those, myaa automatically zooms in and I only get some portion of the image..

Comment only
10 Jul 2009 cartonn

### cartonn (view profile)

10 Jul 2009 7ate9

### 7ate9 (view profile)

24 Jun 2009 Morag

### Morag (view profile)

04 Jun 2009 Nitika

### Nitika (view profile)

Anders, Could you use this while making a movie (say with getframe)? I havent tried it yet, but it looks like - if you can, it will probably take infinite time to do that...
Nevertheless, this is a beautiful application! Thanks!

Comment only
12 Dec 2008 Anders Brun

### Anders Brun (view profile)

Hi Sebastian,
I already know of LineSmoothing and I created Myaa because I needed something more general. LineSmoothing does exactly what is says, it can smooth a line. If two lines crosses, things start go wrong. And there are more relevant objects in Matlab, such as text labels and (unintentionally) hires images, say a 3000x3000 image. Myaa will show you what your figure will look like in print, LineSmoothing will not. But ofcourse LineSmoothing is useful to some people and in some situations, speed is one of its advantages but generality is not.

Believe me, nobody would be more happy than me the day Matlab can do true antialiasing in hardware. :-)

Comment only
12 Dec 2008 Sebastian Hölz

### Sebastian Hölz (view profile)

I hate to tell you, but your file is of very limited use. Little known fact: Matlab (at least from 7.04 on) includes an AA, it is just hidden. Try the following:
>> spharm2
>> set(gco,'LineSmoothing','on')
It will give you AA on the real 3D object, i.e. paning, rotating, zooming possible in real-time, no need for time- and memory-consuming calculations. The "LineSmoothing"-property is available for all relevant HGObjects, i.e. lines, patches, surfaces.
I think the smoothing is also applied to exported and saved figures, as long as you specify ZBuffer or OpenGL as renderer.

Sebastian

Comment only
14 Nov 2008 Oliver Woodford

### Oliver Woodford (view profile)

A nice idea that has greatly improved some figures I'm publishing.

I would have liked the function to simply save the anti-aliased output to file (png). I've now written something similar which does just that.

08 Nov 2008 matt dash

### matt dash (view profile)

very nice, but i think it should optionally take a figure handle as an input instead of just using gcf.

Comment only
08 Nov 2008 matt dash

### matt dash (view profile)

21 Oct 2008 Luc Masset

Fantastic. Great work. I have been looking for an anti-aliasing feature in Matlab for years.

I've just changed two small things to have myaa to work in Matlab R13 :

line 111: remove the (), I guess it always takes @conv2

line 213: I changed this way :

raw_lowres = double(imresize(self.raw_hires,1/self.K(2),'bilinear'))/256;
raw_lowres = single(raw_lowres);

Thanks again Anders.

12 Sep 2008 Stefan Novak

Excellent job! Looking forward to future releases and features!

11 Sep 2008 Liu Michael

Very good, wonderful...

04 Sep 2008 Husam Aldahiyat

Excellent job. Thanks very much!

02 Sep 2008 Nass K

It works fine, thanks.

26 Aug 2008 Matt S
19 Aug 2008 Andrey Krishkevich

Beautiful!

12 Aug 2008 Per Magnusson

myaa works great and is very easy to use. Thanks Anders!

07 Aug 2008 Peter A.

Works great, thanks!

06 Aug 2008 Anders Brun

For your information, every resubmission takes some time to propagate through Mathworks review process... so if you have problems related to the ippl() function, please implement the bugfix below. The new version should be available for download in about a day or so.

Thank you for your patience. /Anders

Comment only
06 Aug 2008 Anders Brun

Thanks for the suggestion Petr. I have submitted a new version now, including a small change for this:

try
if ippl()
myconv = @imfilter;
else
myconv = @conv2;
end
catch
myconv = @conv2;
end

Comment only
06 Aug 2008 i scardanzan

rating based on the posted visualization

05 Aug 2008 Petr Krysl

Well done! Just a small suggestion for improvement: ippl() was not found by Matlab (probably because I do not have the image processing toolbox). This should be perhaps treated with a try, catch?

I have not looked at the code in detail, hence no rating yet.

Comment only
06 Aug 2008

Fixed a bug for people not having the image processing toolbox...

21 Aug 2008

Added interactivity to the user interface and cleaned up some of the code.

21 Aug 2008

Found bugs in last night update. The interactive zoom is now somewhat more stable... Enjoy.

21 Aug 2008

Again, some minor bugs... sorry.