Changeset 2013


Ignore:
Timestamp:
Nov 17, 2007, 4:02:58 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Random halftoning perturbation.
  • Random dither matrix selection.
Location:
www/study
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • www/study/part2.html

    r2012 r2013  
    200200</p>
    201201
    202 <!--
    203 <p> Blah blah: </p>
     202<p> Random dithering can help reduce the major problem caused by halftoning,
     203which is the apparition of pattern artifacts. The method is as simple as
     204slightly perturbating matrix coefficients (or pixel values) during the
     205halftoning step. The difficult part is the proper pertubation choice. </p>
     206
     207<p> For instance, this is the result of 8×8 Bayer dithering perturbated by a
     208gaussian distribution (mean 0.0, standard deviation 0.08): </p>
    204209
    205210<p style="text-align: center;">
    206211  <img src="out2-4-3.png" width="256" height="256"
     212       class="inline" alt="4×4 Bayer dithering, gaussian perturbation" />
     213  <img src="grad2-4-3.png" width="32" height="256"
     214       class="inline" alt="4×4 Bayer dithering, gaussian perturbation gradient" />
     215</p>
     216
     217<p> Another way to use random number generators to avoid pattern artifacts
     218is random dither matrix selection. The image space is no longer tiled with
     219the same matrix over and over again, but with a random selection from a list
     220of similar dither matrices. </p>
     221
     222<p> This example shows random matrix selection from a list of six 3×3 dither
     223matrices: </p>
     224
     225<p style="text-align: center;">
     226  <img src="fig2-4-1.png" width="290" height="190"
     227       class="matrix" alt="four 3×3 dispersed dot matrices" />
     228  <img src="out2-4-4.png" width="256" height="256"
    207229       class="inline" alt="random Bayer matrix dithering" />
    208   <img src="grad2-4-3.png" width="32" height="256"
     230  <img src="grad2-4-4.png" width="32" height="256"
    209231       class="inline" alt="random Bayer matrix dithering gradient" />
    210232</p>
    211 -->
    212233
    213234<div style="float: left;">
  • www/study/study.py

    r2012 r2013  
    270270test242(gradient256bw).save("grad2-4-2.png")
    271271
    272 # Output 2.4.3: random dither matrices
     272# Output 2.4.3: 4×4 Bayer dithering with gaussian perturbation
    273273def test243(src, mat):
    274274    random.seed(0)
     
    277277    dx = len(mat[0])
    278278    dy = len(mat)
    279     randmat = [[(int)(random.random() * 16) for x in range(w / dx)] for y in range(h / dy)]
    280279    for y in range(h):
    281280        for x in range(w):
    282281            c = src.getGray(x, y)
    283             val = (mat[y % dy][x % dx] + randmat[y / dy][x / dx]) % (dx * dy)
    284             threshold = (1. + val) / (dx * dy + 1)
     282            threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1)
     283            threshold += random.gauss(0, 0.08)
    285284            c = c > threshold
    286285            dest.setGray(x, y, c)
    287286    return dest
    288287
    289 test243(lenna256bw, DITHER_BAYER44).save("out2-4-3.png")
    290 test243(gradient256bw, DITHER_BAYER44).save("grad2-4-3.png")
     288test243(lenna256bw, DITHER_BAYER88).save("out2-4-3.png")
     289test243(gradient256bw, DITHER_BAYER88).save("grad2-4-3.png")
     290
     291# Output 2.4.4: random dither matrice selection
     292def test244(src, mlist):
     293    random.seed(0)
     294    (w, h) = src.size()
     295    dest = Image((w, h))
     296    dx = len(mlist[0][0])
     297    dy = len(mlist[0])
     298    for y in range(h / dy):
     299        for x in range(w / dx):
     300            mat = mlist[(int)(random.random() * len(mlist))]
     301            for j in range(dy):
     302                for i in range(dx):
     303                    c = src.getGray(x * dx + i, y * dy + j)
     304                    threshold = (1. + mat[j][i]) / (dx * dy + 1)
     305                    d = c > threshold
     306                    dest.setGray(x * dx + i, y * dy + j, d)
     307    return dest
     308
     309m1 = [[1, 4, 7],
     310      [6, 0, 2],
     311      [3, 8, 5]]
     312m2 = [[4, 6, 3],
     313      [8, 1, 5],
     314      [0, 3, 7]]
     315m3 = [[5, 0, 3],
     316      [2, 8, 6],
     317      [7, 4, 1]]
     318m4 = [[8, 2, 5],
     319      [6, 4, 0],
     320      [1, 7, 3]]
     321m5 = [[2, 5, 8],
     322      [0, 7, 3],
     323      [4, 1, 6]]
     324m6 = [[7, 4, 1],
     325      [3, 6, 8],
     326      [2, 0, 5]]
     327mlist = [m1, m2, m3, m4, m5, m6]
     328test244(lenna256bw, mlist).save("out2-4-4.png")
     329test244(gradient256bw, mlist).save("grad2-4-4.png")
    291330
    292331##############################################################################
Note: See TracChangeset for help on using the changeset viewer.