Home | Lehre | Videos | Texte | Vorträge | Software | Person | Impressum, Datenschutzerklärung | Blog
Blur, Sharpen & the Like
-
Compute a new image pixel by pixel, but take the neighbors of the old pixel
into account.
-
Blur [weichzeichnen]: Form [Bilden] an average color, possibly giving the
neighbor pixels less weight. Typical weighting: Gaussian curve (bell shape),
hence Gaussian blur.
-
Sharpen [scharfzeichnen]: Move the color away from the average, for instance
compute 7*old color - 6*average.
-
Noise Reduction [Rauschunterdrückung] for low-level grain-like
noise: Average only if the pixel's color is close to that of its neighbors,
so that contours are conserved [bleiben erhalten]. For instance:
-
Apply a blur effect where the color difference is below some adjustable
[einstellbar] threshold [Schwellenwert].
-
Bilateral Filter (Tomasi and Manduchi, 1998): Form the average with a weight
f(d)*g(c), where d is the spatial [räumliche] difference, c the color
difference, and f, g are decaying [abfallende] functions such a bell shape.
-
Noise Reduction for high-level grain-like noise: Median. Among the
colors of the old pixel and of its neighbors find the color that is most
similar to the other colors.
Some Programming Practice
Trace
Tracing: "Program tells us what it does." In .NET use the following:
Define TRACE as a symbol (compiler settings)
using System.Diagnostics;
// on initialization:
Trace.AutoFlush = true;
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
// or:
Trace.Listeners.Add(new TextWriterTraceListener("logfile.txt"));
// throughout the program:
Trace.WriteLine("blah");
Assert
Assertions [Zusicherungen]: For debugging, check pre- and post-conditions
and similar with code that will be disabled in the final version, which
thus is faster and smaller.
Define DEBUG as a symbol (automatically done in compiler settings)
using System.Diagnostics;
//check a simple condition:
Debug.Assert(earnings <= 10000, "Earnings may not exceed 10000
Euros.");
//check a complex condition:
#if DEBUG
//do the computation necessary for the test
if( /*...*/ ) Debug.Fail("This and that has happened.");
#endif
Debugger
demo of source-level debugging
Pointer Arithmetic
-
Faster (and more dangerous) than usual access to an array in particular
because there is no test whether the array is accessed outside of its boundaries
-
Is allowed in C# if enclosed in unsafe{...} and if compiler is
switched to unsafe mode
Example: Invert the colors of a bitmap
Bitmap b = new Bitmap(@"some_bitmap.bmp");
Rectangle r = new Rectangle(0, 0, b.Width, b.Height);
BitmapData d = b.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* data = (byte*)d.Scan0.ToPointer();
int n = d.Height*d.Stride;
for(int z = 0; z < n; z++)
{
*data = (byte)(255-*data);
data++;
}
}
b.UnlockBits(d);
Note that this also inverts the dummy bytes that may be appended at
the end of each row. Be careful to exclude the dummy bytes when computing
blur effects. Otherwise they will be smeared into the visible part of the
image.
Commenting Style
Do no longer use comments for each line. Write self-explaining code that
needs virtually no comments.
Antialiasing and Interpolation
-
Aliasing [nicht übersetzbar; Alias = anderer Name]
-
Examples:
-
Jaggies [Zacken, deutsch: Pixeltreppen] on a display screen
-
Moiré in a scan of a print
-
Wheels running backward in a motion picture
-
Some kinds of distortion in digital audio
-
Reason: Sampling frequency [Abtastrate] is too low to capture ["einfangen"]
the fine detail present in the original signal
-
Anti-Aliasing: Do something to prevent aliasing, which means: suppress
[unterdrücken] detail, that is: introduce blur. Demo with type in
photoshop. Typically, antialiasing is done via super-sampling: Sample [Taste
ab] at a higher frequency, cut off high frequencies, and then sample at
the intended frequency. You can switch on anti-aliasing in .NET by setting
the SmoothingMode of the graphics context to SmoothingMode.AntiAlias,
a member of the System.Drawing.Drawing2D name space.
-
Interpolation: A problem somewhat related to anti-aliasing: How do rotate
or stretch or ... a bitmap? You have to produce pixels not present in the
original!