Thursday, April 05, 2007

Cleaning up the Mesh

Allright, the following is intended only for a small public probably, but whoever occasionally puts pictures of adaptive meshes in electronic presentations should read on. I work with (moving) adaptive meshes. There is one major problem when visualizing these things: resolution. In printed matter, my EPS or PDF pictures exported from MATLAB are of course tack sharp. Unfortunately in small pictures, the hundreds of lines tend to blacken the entire image. Setting the line width to approximately 0.2 solves this.
» mh = mesh(x, y, 0*x, 'LineWidth', 0.2, 'EdgeColor', 'k');
That whas the easy part. Now what if the output device does not have as much resolution as a laser printer?

Mesh Moiré

When using the same PDF pictures in an electronic presentation, e.g., by latex-beamer, the viewer is left to do a proper rendering of the vector graphics in the PDF picture at a low resolution (think of a 1024 px screen width). I have not seen a viewer do this without showing moiré effects. Below is a screen capture of acroread rendering a 200 by 200 adaptive mesh: It looks awful! This will not impress the audience! What makes this even more complicated is that each program performs differently (I've had the best results with acroread 4 so far...), and that at each location the beamer is probably different in resolution from prior locations. So, I wanted to get rid of the on site rendering of these vector graphics. I'll just create a bitmap version and include that. Here's the MATLAB PNG output: (Click on image for actual MATLAB output: MATLAB does not do antialiasing!) So, which program does do good rasterizing? I had heard Martin Bravenboer being really enthusiastic about the SVG rasterizer in the Batik SVG Toolkit. I got an SVG export for my MATLAB figure by the plot2svg add-on. In MATLAB, this was just:
» plot2svg('hd22conf11.svg')
Next, from a terminal, I let Batik render at the desired resolution:
> java -Xmx512m -jar $PKGROOT/batik/current/batik-rasterizer.jar -w 500 -h 500 -bg hd22conf11.svg
(Notice how I had to increase Java's maximum heap size; the SVG itself is 8MB, and the rasterizing apparently takes a lot of memory.) The resulting MATLAB -> SVG -> PNG output: Still far from optimal... I'm willing to lose some sharpness, if that rids me of the moiré. So, I rasterize a huge image, say 4000 by 4000 px, and scale that down to the desired size, e.g., by ImageMagick's convert:
> java -Xmx512m -jar $PKGROOT/batik/current/batik-rasterizer.jar -w 4000 -h 4000 -bg hd22conf11.svg
> convert -resize 500x500 hd22conf11.png hd22conf11_batik4000convert500.png
The resulting MATLAB -> SVG -> PNG large -> PNG resized output: Now, that's what makes me happy. Of course, some sharpness is lost, but what do you expect, for a 200 by 200 mesh on just slightly more than 400 by 400 px? A final note: when including the above PNG into your latex-beamer presentation will again lead to a resizing of this PNG, with minor moiré as a result. I found an image of 560 px wide, included at half size the most acceptable, and far better than anything I've seen so far.


Post a Comment

<< Home