# Anti-Aliasing Images

In EMT a special way to anti-alias images is used. To demonstrate this approach, we emulate it in EMT using the available tools for image manipulation. First we generate a plot.

>a=(0.1:0.2:5)'; plot2d("a*x^3-x",r=1, ...
>  title="Test",xl="x",yl="y",>vertical):

We have saved this image with the internal anti-aliasing as a 600×600 PNG for this blog.

How is the anti-aliasing done? For a first try, we use a very simple method. This method smears 9 surrounding pixels of a 400×400 version of the plot to get a more smooth look. The plot without anti-aliasing in 400×400 looks like this.

We save this plot to a PNG file, not anti-aliased, and reload it into EMT. Then we fold the matrices of each color with a 3×3 matrix F of the form

$$f_{i,j} = v_i v_j, \qquad 1\le i,j \le 3$$

which is scaled so that the total sum of its elements is 1. We took v=[1,6,1] here. Folding means that each 3×3 sub-matrix of the image is multiplied element-wise with F and the pixel in the center is replaced by the sum of these products.

$$\tilde m_{s,t} = \sum_{1 \le i,j \le 3} m_{s-2+i,t-2+j}f_{i,j}$$

>savepng(„test1.png“,400,400,false);
>{R,G,B}=getrgb(M);
>v=[1,6,1]; F=v*v‘; F=F/totalsum(F);
>R=fold(R,F); G=fold(G,F); B=fold(B,F);
>MF=rgb(R,G,B); insrgb(MF);

This is certainly smoothed. But it isn’t visually satisfying.

So we try another approach. We save our plot as a 1502×1502 plot. The lower left part of this plot looks like this.

Then we fold it with a 5×5 matrix of the same type as above, using v=[1,3,4,3,1]. Finally, we reduce this image to 500×500 taking every third pixel in each direction. Essentially we take the center of blocks of 3×3 sub-matrices, with a value determined by the surrounding 5×5 pixels. Clearly, the function fold() should be rewritten for this, since we discard most of its results later. This could be done in C. But for simplicity, we demonstrate the algorithm in EMT.

>M=loadrgb("test2.png"); insrgb(M[1:400,1:400],50);
>{R,G,B}=getrgb(M);
>v=[1,3,4,3,1]; F=v*v'; F=F/totalsum(F);
>R=fold(R,F); G=fold(G,F); B=fold(B,F);
>i=1:3:cols(R); j=1:3:rows(R);
>MF=rgb(R[i,j],G[i,j],B[i,j]); insrgb(MF);

The result of this procedure is very good.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.