Code covered by the BSD License  

Highlights from
Myaa - My Anti-Alias for Matlab

4.67857

4.7 | 28 ratings Rate this file 173 Downloads (last 30 days) File Size: 268.24 KB File ID: #20979
image thumbnail

Myaa - My Anti-Alias for Matlab

by Anders Brun

 

05 Aug 2008 (Updated 21 Aug 2008)

Better looking Matlab figures with anti-aliasing!

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

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 submission has inspired the following:
export_fig
MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (39)
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.

06 Aug 2008 i scardanzan

rating based on the posted visualization

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;
        mypad = 'same';
    else
        myconv = @conv2;
        mypad = 'valid';
    end
catch
    myconv = @conv2;
    mypad = 'valid';
end

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

07 Aug 2008 Peter A.

Works great, thanks!

12 Aug 2008 Per Magnusson

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

19 Aug 2008 Andrey Krishkevich

Beautiful!

26 Aug 2008 Matt S  
02 Sep 2008 Nass K

It works fine, thanks.

03 Sep 2008 Adam Chapman

Excellent job. Thanks very much!

04 Sep 2008 Husam Aldahiyat  
11 Sep 2008 Liu Michael

Very good, wonderful...

12 Sep 2008 Stefan Novak

Excellent job! Looking forward to future releases and features!

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.

08 Nov 2008 matt dash  
08 Nov 2008 matt dash

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

14 Nov 2008 Oliver Woodford

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.

12 Dec 2008 Sebastian Hölz

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

12 Dec 2008 Anders Brun

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. :-)

04 Jun 2009 Nitika

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!

24 Jun 2009 Morag  
10 Jul 2009 7ate9  
10 Jul 2009 cartonn  
14 Jul 2009 cartonn

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..

27 Aug 2009 Linda F  
30 Nov 2009 Daniel Armyr

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.

16 Jan 2010 David

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.

17 Jan 2010 Rob Campbell

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.

17 Feb 2010 Dave Holden  
12 May 2010 Jonas Lundgren

Thank you! This is useful.

28 Jul 2010 Sara

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

01 Sep 2010 stephen Strenn

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');

23 Oct 2010 Admin

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 !

22 Dec 2010 Timothée

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 ?

Thanks in advance

11 Jan 2011 Thomas

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.

13 Jan 2011 A

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:

I = imread('C:\Documents and Settings\amsowchu\Desktop\myaa\myaa\test.jpg');
imshow(I);
myaa;

02 Jun 2011 ubuntu21 T

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

22 Aug 2011 Natasha

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

28 Sep 2011 Daniel  
Please login to add a comment or rating.
Updates
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.

21 Aug 2008

Changed some nomenclature in the code. Thanks to Khalid.

Tag Activity for this File
Tag Applied By Date/Time
annotation Anders Brun 22 Oct 2008 10:13:22
customization Anders Brun 22 Oct 2008 10:13:22
antialiasing Anders Brun 22 Oct 2008 10:13:22
smooth Anders Brun 22 Oct 2008 10:13:22
html Anders Brun 22 Oct 2008 10:13:22
presentation Anders Brun 22 Oct 2008 10:13:22
png Anders Brun 22 Oct 2008 10:13:22
jpg Anders Brun 22 Oct 2008 10:13:22
raster Anders Brun 27 Oct 2008 16:58:23
myaa Anders Brun 31 Oct 2008 13:33:04
potw Cristina McIntire 07 Nov 2008 13:05:43
antialiasing Menl 26 Feb 2009 05:18:28
antialiasing Mike 28 Jul 2009 15:12:27
antialiasing Jose Ercolino 29 May 2010 08:21:00
html Jose Ercolino 29 May 2010 08:21:03
presentation Jose Ercolino 29 May 2010 08:21:12
publish Jose Ercolino 29 May 2010 08:21:21
export Jose Ercolino 29 May 2010 08:21:32
pick of the week Jiro Doke 11 Feb 2011 20:13:26
smooth Tamas 14 Feb 2011 04:50:56
antialiasing Don Orofino 31 May 2011 11:53:24
antialiasing Greg 16 Jan 2012 14:41:36

Contact us at files@mathworks.com