[iarec] [Up] [iafloyd] Halftoning Approximation

iadither
Ordered Dither.

Synopsis

g = iadither( f, n )

Implemented in Python.

Input

f Image. Gray-scale (uint8 or uint16) or binary image (logical).

input image

n Image. Gray-scale (uint8 or uint16) or binary image (logical).

dimension of the base matrix

Output

g Image. Accepts any image data type.

Description

Ordered dither (Bayer 1973).

Examples

>>> f1 = iaramp([20,150], 256, [0,255])

              
>>> f2 = iaresize(iaread('woodlog.pgm'), [150,150])

              
>>> f = Numeric.concatenate((f1, f2))

              
>>> g_4 = iadither(f, 4)

              
>>> g_32 = iadither(f, 32)

              
>>> iashow(f)
(170, 150) Min= 0 Max= 255 Mean=95.406 Std=57.48
>>> iashow(g_4)
(170, 150) Min= 0 Max= 1 Mean=0.385 Std=0.49
>>> iashow(g_32)
(170, 150) Min= 0 Max= 1 Mean=0.379 Std=0.49
f g_4 g_32

Source Code

def iadither(f, n):
    from Numeric import array, log, ones, concatenate, ravel
    D = 1.*array([[0,2],[3,1]])
    d = 1*D
    k = log(n/2.)/log(2.)
    for i in range(k):
        u = ones(D.shape)
        d1 = 4*D + d[0,0]*u
        d2 = 4*D + d[0,1]*u
        d3 = 4*D + d[1,0]*u
        d4 = 4*D + d[1,1]*u
        D = concatenate((concatenate((d1,d2),1), concatenate((d3,d4),1)))
    D = 255*abs(D/max(ravel(D)))
    g = iatile(D.astype('b'), f.shape)
    g = ianormalize(f,[0,255]) >= g
    return g
    
[iarec] [Up] [iafloyd] http://www.python.org