Sie sind auf Seite 1von 9

Dithering

1st color
missing colors are simulated by mixing existing colors

Dithering and Raster Graphics


Special Aspects of Raster Images

2nd color

missing color
2 /54

Eduard Grller, Thomas Theul

Dithering Methods (Digital Halftoning)


s

Dithering in Printing Industry


newspapers black ink on light paper, rasterization of the images enables also grey levels, equal point density everywhere, variable size q color printing every primary color is rasterized separately, different printing angles ensure unbiased results
q

threshold dithering x ordered dither x stochastic dither x dot diffusion x .... s error diffusion dithering (Floyd-Steinberg)
3 /54

Eduard Grller, Thomas Theul

Eduard Grller, Thomas Theul

4 /54

Threshold Dithering
every pixel is compared to a threshold t:

Constant Threshold Dithering


sample image threshold values result

t can be: q equal everywhere (e.g. (ba)/2, arbitrary value, mean value, median, ...) q location dependent (defined locally or globally)
Eduard Grller, Thomas Theul 5 /54

p<t p>t

a b

1 1 1 1

7 6 5 4

6 5 4 2

5 4 3 1

4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5

0 0 0 0

9 9 9 0

9 9 0 0

9 0 0 0

(values between 0 and 9) corresponds to rounding


Eduard Grller, Thomas Theul 6 /54

Principle of Dithering
available values a, b missing value x between a and b shall be simulated by mixing a-pixels and b-pixels

Principle of Dithering (2)


a x-a x b

b-x b-a to produce color value x there have to be:

100 * x a % b-pixels ba 100 * b x % a-pixels ba


Eduard Grller, Thomas Theul 7 /54 Eduard Grller, Thomas Theul 8 /54

Dithering a Uniform Area


for a uniform area regular application of this pattern will produce 1/4 1/2 3/4 this grey tone 0

Dithering a Uniform Area (2)


This can be done by using a different threshold for every pixel (using the interval borders)

1/8

5/8 3/8 5/8 ...

1/8 7/8 1/8

5/8 3/8

1/8 7/8

5/8 ...

1
7/8

interval borders

1/8

3/8

5/8

7/8

1/8

= "Threshold Matrix"

Eduard Grller, Thomas Theul

all grey levels in this interval will be mapped to 1/4


9 /54

Eduard Grller, Thomas Theul

10 /54

Threshold Matrix
distances between interval borders are equal, therefore it suffices to define the sequence of the pixel values in the matrix:

Dither Matrix Example


dither matrix threshold matrix

7 0 5

2 4 6

3 8 1

1/8

5/8

2 1

instead of
7/8 3/8

only
3

15 18 1 18 11 18

5 7 18 18 9 17 18 18 13 3 18 18
2k+1 2n2
12 /54

i.e. for an nxn matrix: values [0,n21]


value k corresponds to threshold value:
Eduard Grller, Thomas Theul

2k+1 2n2
11 /54

value k corresponds to threshold value:


Eduard Grller, Thomas Theul

Threshold Matrix Dithering Example


sample image threshold values result

Generation of Threshold Matrices (1)


recursive method: 4 copies of smaller matrices

1 1 1 1

7 6 5 4

6 5 4 2

5 4 3 1

1.1 5.6 1.1 5.6 7.9 3.4 7.9 3.4 1.1 5.6 1.1 5.6 7.9 3.4 7.9 3.4

0 0 0 0

9 9
0

9
0

9
0 0

0 2 3 1

...

k= 0(1)n2-1

4k+0

4k+2

9
0

(values between 0 and 9)


Eduard Grller, Thomas Theul 13 /54

example: 0 12 3 15
Eduard Grller, Thomas Theul

8 4 11 7

2 14 1 13

10 6 9 5

4k+3

4k+1

14 /54

Generation of Threshold Matrices (2)


direct method: use of magic squares

Dithering between Grey Levels


a x b

example

magic squares produce fewer diagonal stripes


Eduard Grller, Thomas Theul 15 /54

calculation is done separately for every pixel (not once for a dithering matrix)
Eduard Grller, Thomas Theul 16 /54

Grey Level Dithering Example


4 grey values are available: 0, 3, 6, 9 0 dither matrix: 3

Dot Diffusion Dithering


2 1

ordering of the threshold values generates larger dot areas example:

sample image threshold values

result

1 1 1 1

7 6 5 4

6 5 4 2

5 4 3 1

0.4 7.9 3.4 4.9 2.6 3.4 5.6 4.1 0.4 4.9 3.4 1.9 2.6 4.1 2.6 1.1

6 6 6 3

6 3 6

6 3 3

0
3

11 16 12 8 4 5 15 17 13 3 0 1 10 14 9 7 2 6
simulates traditional printing techniques for high resolution devices
Eduard Grller, Thomas Theul 18 /54

0
17 /54

Eduard Grller, Thomas Theul

(values between 0 and 9)

0 11 12 7

14 3 5 8 2 15 9 4

13 6 1 10

threshold values have to lie between a and b:

a + 2k+1 (b-a) 2n2

Stochastic Dithering?
use of random numbers as threshold values
s expectation s no

Forced Random Matrix Dithering


very good results improved "random" matrices method: insert threshold values one by one into matrix, always use the position farthest away from all previous points r p repulsive force field: f(r) = e s precalculate large threshold matrices: 300x300 very good results!
Eduard Grller, Thomas Theul 20 /54

value of total error = 0 regular artificial patterns possible

unfortunately: very bad results! (due to bad distribution of random numbers)

Eduard Grller, Thomas Theul

19 /54

Error Diffusion Dithering


(Floyd-Steinberg)
the rounding error of every pixel is propagated to neighbor pixels and compensated there variations: which neighbor pixels are affected?

Simplest Error Diffusion Dithering


pixel line correct value rounded value ... k1 k2 k3 k4 k5 ... r1 r2 r3 r4 r5 ...

r1 := round (k1) error1 := r1 k1 r2 := round (k2 error1) error2 := r2 (k2 error1) ... ri := round (ki errori1) errori := ri (ki errori1)
21 /54 Eduard Grller, Thomas Theul 22 /54

Eduard Grller, Thomas Theul

Error Diffusion Dithering Example


example: the values 0, 3, 6, 9 are available

Diffusion Direction Variations


to gain better results, the error is distributed to several neighbors (with weights) sum of all weights = 1 1 x x 7 16 3 5 1 often used x 7 1 x weighting 48 3 5 7 5 distributions: 1 x 1 1 3 5 3 x 2 1 x 8 1 x 42 2 4 8 4 1 2 4 2
Eduard Grller, Thomas Theul

k: 1 1 1 2 3 4 7 1 5 ... r: 0 3 0 3 3 3 6 3 3 ... f: -1 1 0 1 1 0 -1 1 -1 ... ri := round (ki - fi-1) fi := ri - ki + fi-1

5 3 1 4 2 1

Eduard Grller, Thomas Theul

23 /54

24 /54

Error Diffusion Dithering Example


example: the values 0, 3, 6, 9 are available 1 error distribution: 2 sample image error
-1 1.5 .75 1.37
x

Serpentine Method
artificial stripes can be reduced drastically by processing the scanlines in serpentine order instead of "normal" now in "serpentines"

x 1 1

result

1 1 1 1

7 6 5 4

6 5 4 2

5 4 3 1

0
3

9 6 3 6

6 3 6

6 3 3 3
25 /54

1.5 1.5 -.87 -.75 -.25 -1.37 .87 .06 -1.12 .75 -1.12 1.47

0 0

...
no additional memory necessary
Eduard Grller, Thomas Theul

...

(values between 0 and 9) Eduard Grller, Thomas Theul

26 /54

Raster Conversion
converting primitives (lines etc.) into pixels very frequent operations, therefore necessary: s efficiency s possibility to implement in hardware

Raster Conversion of Lines


lines should appear straight lines should appear uniformly bright lightness should be independent of direction endpoints should be "exact"

Eduard Grller, Thomas Theul

27 /54

Eduard Grller, Thomas Theul

28 /54

Symm. DDA for Line (x1,y1) (x2,y2)


(x,y) := ( (x2,y2) (x1,y1) ) / 2i draw (rd(x1) / rd(y1)) (x1,y1) := (x1+x,y1+y) x2 < x1+x YES NO rd(x1x) rd(x1) ORrd(y1y) rd(y1) YES
Eduard Grller, Thomas Theul

Simple DDA
symmetric DDA produces lines of variable breadth, but if x and y are chosen such that max(|x|, |y|) = 1 only 1 pixel per unit in the longer direction

2i < length i-1 <2

draw (rd(x2) / rd(y2))

problem: requires a real division solution: Bresenham Algorithm image equal to simple DDA, but no division

NO

STOP
29 /54 Eduard Grller, Thomas Theul 30 /54

Comparision Symmetric - Simple DDA

Raster Conversion of Lines


(Bresenham's Line Algorithm)
T D H

} dy yD-yS < yT-yD yD-yS > yT-yD

S T

symmetric DDA
Eduard Grller, Thomas Theul

simple DDA
31 /54

only for lines with angle 45 other lines by mirroring/rotating with 90/180...
Eduard Grller, Thomas Theul 32 /54

Some Bresenham Mathematics


yD yS < yT yD yD yH < 0 yD yS > yT yD yD yH > 0

Bresenham's Line Algorithm (1)


ys:=y1; d:=-0.5; {d=yDyH} dy:=(y2-y1)/(x2-x1); FOR xs:=x1 TO x2 DO BEGIN SetPixel(xs,ys); d:=d+dy; IF d>0 THEN BEGIN ys:=ys+1; d:=d-1 {because yH:=yH+1} END END

in every case yD := yD + dy and (xS/yS) are drawn let d = yD yH


Eduard Grller, Thomas Theul

Bresenham's Line Algorithm (2)


ys:=y1; e:=-(x2-x1)div 2; {d*(x2-x1)} de:=(y2-y1); {dy*(x2-x1)} FOR xs:=x1 TO x2 DO BEGIN SetPixel(xs,ys); e:=e+de; IF e>0 THEN BEGIN ys:=ys+1; e:=e-(x2-x1) END only integers! END only addition, subtraction, shift!
Eduard Grller, Thomas Theul 35 /54

if yD yH < 0 if yD yH > 0

yS, yT do not change, yS := yS+1, yT := yT+1

decision variable
33 /54 Eduard Grller, Thomas Theul 34 /54

Raster Conversion of Circles


(Bresenham's Circle Algorithm)

utilize threefold symmetry!

only one eighth has to be calculated

Eduard Grller, Thomas Theul

36 /54

Bresenham's Circle Algorithm


P T H S
yD-yS > yT-yD yD-yS < yT-yD

Bresenham's Circle Algorithm


yDyS > yTyD H inside the circle that is if xH2+yH2 < r2 or if f(xH,yH) = xH2+yH2 r2 < 0

(only for 2nd octant)


Eduard Grller, Thomas Theul 37 /54

Bresenham's Circle Algorithm


if dold < 0 then Hnew = Hold + (1,0) , ie. dnew = f(xp+2, yp1/2) = (xp+2)2 + (yp1/2)2 r2 dnew = dold + (2xp+3) if dold > 0 then Hnew = Hold + (1,1) , ie. dnew = f(xp+2, yp3/2) = (xp+2)2 + (yp3/2)2 r2 dnew = dold + (2xp 2yp + 5)
Eduard Grller, Thomas Theul 39 /54

Bresenham's Circle Algorithm


initialization: restriction to integer radii
x=0

and

y=r

H = (1,r-1/2) d = f(H) = 1 + (r2-r+1/4) - r2 = 5 /4 - r

Eduard Grller, Thomas Theul

T S

d = "decision variable" i.e. dold = d = f(xp+1, yp1/2) , then dnew can be calculated as follows:
Eduard Grller, Thomas Theul 38 /54

Bresenham's Circle Algorithm


PROCEDURE Circle (r:INTEGER); VAR x,y: INTEGER; d: REAL; BEGIN ... {initialize x,y,d} REPEAT Draw_8(x,y) IF d < 0 THEN d:=d+2*x+3 ELSE BEGIN d:=d+2*(x-y)+5; y:=y-1 END; x:=x+1; UNTIL y < x Eduard Grller, Thomas Theul END;

Bresenham's Circle Algorithm


PROCEDURE Circle (r:INTEGER); VAR x,y: INTEGER; d: REAL; BEGIN x:=0; y:=r; d:=5/4 - r; REPEAT Draw_8(x,y) IF d < 0 THEN d:=d+2*x+3 ELSE BEGIN d:=d+2*(x-y)+5; y:=y-1 END; x:=x+1; UNTIL y < x Eduard Grller, Thomas Theul END;

41 /54

criterion:

f(xH,yH) < 0 f(xH,yH) > 0

T S

40 /54

42 /54

Raster Transformations
how to apply geometrical transformations to raster images? translation: trivial scaling: resampling necessary s shearing: by line conversion s rotation: partition in three shearings
s s

Raster Scaling: Scaling Up


old resolution new resolution

center point of pixel in new resolution defines its color = "resampling"


43 /54 Eduard Grller, Thomas Theul 44 /54

Eduard Grller, Thomas Theul

Raster Scaling: Scaling Down


old resolution new resolution

Shearing
x-shearing
(x y) 1 0 = (x+y y) 1

y-shearing
(x y) 1 = (x y+x) 0 1

center point of pixel in new resolution defines its color = "resampling"


Eduard Grller, Thomas Theul 45 /54

Eduard Grller, Thomas Theul

46 /54

Raster Shearing

Raster Rotation
90-rotation, 180-rotation, etc. trivial
principle of other rotations: subdivide rotation in a series of three shears

can be seen as the multiple application of a line raster conversion algorithm (e.g. Bresenham) no information lost

cos sin -sin cos

1 0 . 1 . 1 0 1 0 1 1

where = - tan (/2) and = sin


Eduard Grller, Thomas Theul 48 /54

Eduard Grller, Thomas Theul

47 /54

Rotation = 3 Shears
tan (/2) = (1- cos ) / sin 1 cos 1 sin 0. 1 1 sin 0 1 . 1 0 cos 1 1 = sin

Example: 45 Rotation

1 cos 1 sin

0 1 sin . 1 1 = cos cos sin

tan (/2) = 0.4142 sin = 0.7071

sin 1+ cos 1 cos sin cos 1+ cos2 cos cos = -sin cos sin
Eduard Grller, Thomas Theul 49 /54 Eduard Grller, Thomas Theul 50 /54

Example: 45 Rotation

Example: 45 Rotation
tan (/2) = 0.4142 sin = 0.7071

tan (/2) = 0.4142 sin = 0.7071

Eduard Grller, Thomas Theul

51 /54

Eduard Grller, Thomas Theul

52 /54

Distortion of Raster Images


v y

Distortion of Raster Images


y u=0 interpola tion interpola tion u v=0 v=1 interx pola tion (u,v) v u=1 u=1 u=0

we are looking for the (u,v)-coordinates of the center of pixel (x,y)


Eduard Grller, Thomas Theul 53 /54

(u,v) are calculated from (x,y) by interpolation from the corner coordinates
Eduard Grller, Thomas Theul 54 /54

Das könnte Ihnen auch gefallen