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. Write the result into a new image, not the original one.
Otherwise, you're producing weird feedback patterns.
-
Blur [weichzeichnen]: Form [Bilde] 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].
-
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: Let the 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
for the debug version, not for the release version).
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
The most important tool. Already shown in the lab.
Pointer Arithmetics
-
Much faster (but more dangerous) than usual access to an array, because
there is no test whether or not the array is accessed outside of its boundaries
-
Allowed in C# if enclosed in unsafe{...} and if the 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* p = (byte*)d.Scan0.ToPointer();
int n = d.Height*d.Stride;
for(int z = 0; z < n; z++)
{
*p
= (byte)(255-*p);
p++;
}
}
b.UnlockBits(d);
Note that this also inverts the dummy bytes that may be appended at
the end of each row. (The value of stride need not be three times the width,
but can be larger!) Be careful to exclude the dummy bytes when computing
blur or similar effects. Otherwise they will smear into the visible part
of the image.
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 [Abtasten]
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 to rotate
or stretch or geometrically distort a bitmap? You have to produce (that
is, guess) pixels that are not present in the original!