(Anti)Aliasing and Image Manipulation for (y = 0; y < Size; y++) { for (x = 0; x < Size; x++) { Image[x][y] = 7 + 8 * sin((sqr(x Size) + SQR(y Size)) / 3.0); } } // Size = Size / ;
Aliasing Can t draw smooth lines on discrete raster device get staircased lines ( jaggies ): Images with too fine details create artifacts (Moire patterns) Why? How to solve? Aliasing - Center for Graphics and Geometric Computing, Technion
Antialiasing Aliasing - Center for Graphics and Geometric Computing, Technion 3
Antialiasing Aliasing - Center for Graphics and Geometric Computing, Technion 4
Sampling D raster image = discrete sampling of continuous image (signal) Consider D signal sampling Aliasing - Center for Graphics and Geometric Computing, Technion 5
Sampling D raster image = discrete sampling of continuous image (signal) Consider D signal sampling Aliasing - Center for Graphics and Geometric Computing, Technion 6
Sampling D raster image = discrete sampling of continuous image (signal) Consider D signal sampling Aliasing - Center for Graphics and Geometric Computing, Technion 7
Sampling D raster image = discrete sampling of continuous image (signal) Consider D signal sampling Aliasing - Center for Graphics and Geometric Computing, Technion 8
Sampling Theory in a Nutshell Terminology Cycle a repeated sequence over infinite signal Frequency number of cycles in a unit length Example sine wave Cycle = π Frequency = / π Aliasing - Center for Graphics and Geometric Computing, Technion 9
Sampling Theory Theorem: Any periodic signal can be represented as (infinite) sum of sine waves, at different frequencies Can represent signal using coefficients of the sine waves - alternative basis used by Fourier Analysis Fourier Transform- Tool to transform to/from sine basis, to/from the frequency domain Aliasing - Center for Graphics and Geometric Computing, Technion 0
Example A square wave is represented by infinite sum sin(3t) sin(5t) SqrWave( t) = sin( t) + + +... 3 5 First few components: Aliasing - Center for Graphics and Geometric Computing, Technion
Shannon Sampling Theorem Shannon Theorem: Any signal that is band-limited to frequency f max and sampled at equally spaced intervals smaller than t s = /(f max ) can be fully reconstructed The minimal sampling frequency of t s = /(f max ) is called the Nyquist frequency of the signal That this condition is necessary can be trivially shown: Question: What is the result of this sampling? Aliasing! Aliasing - Center for Graphics and Geometric Computing, Technion
More Aliasing Examples Sampling high frequency wave below Nyquist frequency yields results that look like a low frequency wave The appearance of high frequencies as low frequencies, due to sampling, is called Aliasing: Aliasing - Center for Graphics and Geometric Computing, Technion 3
Even More Aliasing Examples Consider two sine waves: -sin(3t) and sin(5t): -sin(3t) = sin(5t) is satisfied for t = π n / 4 Sampling sin(5t) at t i = π i / 4 looks like sampling sin(3t) Aliasing - Center for Graphics and Geometric Computing, Technion 4
Aliasing Aliasing the appearance of high frequencies as low frequencies To avoid Sample at a higher frequency - only if band-limited to begin with A-priori remove high frequencies Low pass filtering before sampling For images the discrete image is a sampling set of a continuous image (band limits?) Aliasing - Center for Graphics and Geometric Computing, Technion 5
The Fourier Transform Takes us from the spatial domain to the frequency domain: F( t) = f ( x) e itx dx The inverse Fourier transform equals f ( t) = F( x) π e itx dx We will see some examples shortly Aliasing - Center for Graphics and Geometric Computing, Technion 6
Convolution The convolution, denoted by *, is defined as: ( f * g)( x) = f ( t) g( x t) dt Question Question: What is the intuition behind the convolution operation? Theorem: Convolution in the spatial domain is the same as a multiplication in the frequency domain and vice-versa Aliasing - Center for Graphics and Geometric Computing, Technion 7
The Fourier Transform (cont d) Question: What is the Fourier transform of a hat? Aliasing - Center for Graphics and Geometric Computing, Technion 8
Convolution Consider an f max band-limited signal. Sampling is the same as multiplying the signal with equally spaced impulse functions δ(x): By the theorem, this operation is equivalent to a convolution in the frequency domain: Aliasing - Center for Graphics and Geometric Computing, Technion 9
Convolution (cont d) During the reconstruction process, we apply a low pass filter that removes all frequencies above f max This ideal filter is a box filter in the frequency domain: Samples that are less dense in the spatial domain will become more dense in the frequency domain Question: What will happen if we sample below the Nyquist limit Aliasing - Center for Graphics and Geometric Computing, Technion 0
We will get aliasing: Convolution (cont d) The different convolved elements, in the frequency domain, overlap. We have high frequencies intermixed with low frequencies and they are inseparable. The only way to prevent aliasing is pre-filtering Aliasing - Center for Graphics and Geometric Computing, Technion
Back to Computer Graphics Question: how all this sampling theory relates to computer graphics? When a discrete image I d (x,y) is created, we are effectively generating a sample set of a (possibly imaginary) continuous image I c (x,y) Extending sampling theory to two dimensions: I c (x,y) should be band-limited for it to be reconstructable Sampling should be above the Nyquist limit for I c (x,y) to be reconstructable The box filter in the frequency domain is the ideal low-pass filter (what is it in the spatial domain?) Aliasing - Center for Graphics and Geometric Computing, Technion
Back to Computer Graphics (cont d) Question: is I c (x,y) band-limited? Question: can we pre-filter I c (x,y)? One can reduce aliasing by super-sampling Question: can we post-filter the data using convolution with a sinc filter in the spatial domain? sin ( ) x + y x + y Aliasing - Center for Graphics and Geometric Computing, Technion 3
Sinc/Truncated sinc Box ) sin( y x y x + + and, y x 3 3 4 3 3 4 9 0 4 6 4 0 6 9 6 0 4 6 4 0 33 Common Filters Aliasing Aliasing - Center for Graphics and Geometric Computing, Technion Center for Graphics and Geometric Computing, Technion 4 Triangle (Hat) Guassian otherwise 0, + + otherwise 0,, y x y x 4 6 9 5 7 3 4 6 4 3 6 9 6 3 4 6 4 3 8 4 4 0 4 4 50 y x e + π
Common Filters (cont d) All filters are normalized so they sum to one. Why? Due to obvious reasons we cannot apply the infinite filters and must compromise on the truncated versions Note the sinc filter can assume negative values which is a potential for minor problems. Why? The difference between different filters are quite minor and most filters of size 4 4 offers similar improvements Aliasing - Center for Graphics and Geometric Computing, Technion 5
Filter results Unfiltered Box Gaussian Aliasing - Center for Graphics and Geometric Computing, Technion 6
Supersampling Example None 3x3 5x5 Aliasing - Center for Graphics and Geometric Computing, Technion 7
A Word on Temporal Aliasing So far we dealt with Spatial Aliasing In animation, aliasing could also occur between one frame and the next This aliasing is known as Temporal Aliasing Recall that in a movie each frame represents a finite time range of /5 or /30 of a second. Hence, it would serve better to accumulate the influence of moving objects over this time Aliasing - Center for Graphics and Geometric Computing, Technion 8
The technique of Motion Blur accumulates the affect of each object over the time step of the frame The result will look like the image on the right Playing animation with motion blur is more natural to the eye Motion Blur Aliasing - Center for Graphics and Geometric Computing, Technion 9
Image Manipulation Image operations Scaling Rotation Composition Scientific American, Feb 994 Aliasing - Center for Graphics and Geometric Computing, Technion 30
Image Scaling & Rotation Consider Linear Transformation L of image I: ImageLinTrans( I, L) clear I to background color For x from 0 to XMax(I) For y from 0 to YMax(I) I( L( x, y)) = I( x, y); What is wrong with this algorithm? Aliasing - Center for Graphics and Geometric Computing, Technion 3
Correct Image Scaling & Rotation Guarantee coverage iterate over target ImageLinTrans( I, L) clear For For I to background color x from 0 to I( x, y) = XMax(I) y from 0 to YMax(I) I( L ( x, y)); What can be wrong with this algorithm? Any reason to limit L to a linear transformations? Aliasing - Center for Graphics and Geometric Computing, Technion 3
Image Transformations Each pixel in the target image is computed from a single source pixel prone to Aliasing Solution in filtering A pixel in target image is some affine combination of several source pixels Coefficients based on area coverage Aliasing - Center for Graphics and Geometric Computing, Technion 33
Rotation By Shear Convenient for images as it involved only intra-row or intra-column operations Rotation can be performed by two shears can be lossy for large angles x cosθ = y sinθ Image size might be reduced in intermediate stage Rotation by three shears is lossless up to integer precision. cosθ sinθ 0 sinθ 0 = θ θ sinθ cosθ tan 0 tan Aliasing - Center for Graphics and Geometric Computing, Technion 34 sinθ x cosθ y
Image Composition + = Aliasing - Center for Graphics and Geometric Computing, Technion 35
Basic Method Composition ImageOverC ompose(, I clear I to background color For x from 0 to XMax(I) For y from 0 to YMax(I) if I ( x, y) = background I ( x, y) = I else I ( x, y) = I I ) ( x, y); ( x, y); color Will work until portion of foreground has background color Unfortunately we are facing more problems Aliasing - Center for Graphics and Geometric Computing, Technion 36
Composition (cont d) Question: This is the result of applying the basic algorithm to teapot-over-whitebackground. What is going on? Aliasing - Center for Graphics and Geometric Computing, Technion 37
Composition (cont d) The reason for this behavior could be found in the filtering (anti-aliasing) conducted during the rendering process On the boundary there exist pixels that are only partially covered by the teapot Rendered with black background, these boundary pixels are darker Yet, as dark as they are, they are not black and hence they are being copied verbatim Question: Any remedy? Aliasing - Center for Graphics and Geometric Computing, Technion 38
Composition (cont d) A background pixel has zero coverage while a pixel totally contained in the teapot s projection will have a coverage of one. This coverage information is also known as the Alpha channel that accompanies the RGB Question: How can we compute the Alpha channel? Question: How can we use the Alpha channel to resolve this boundary problem? Aliasing - Center for Graphics and Geometric Computing, Technion 39
Composition (cont d) T Teapot, S - Sand Over: Tα T + S( -α T ) Out: S( -α T ) In: Sα T Aliasing - Center for Graphics and Geometric Computing, Technion 40
Composition (cont d) Scientific American, Feb 994 Aliasing - Center for Graphics and Geometric Computing, Technion 4
Special Effects Chroma Key Aliasing - Center for Graphics and Geometric Computing, Technion 4
Special Effects Chroma Key II Aliasing - Center for Graphics and Geometric Computing, Technion 43
Special Effects No Blue Background Aliasing - Center for Graphics and Geometric Computing, Technion 44