Changeset 2112


Ignore:
Timestamp:
Dec 3, 2007, 6:48:29 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Started dynamic thresholding.
Location:
www/study
Files:
4 added
2 edited

Legend:

Unmodified
Added
Removed
  • www/study/part1.html

    r2028 r2112  
    133133
    134134<p> This is a perfect example of a situation where colour accuracy does not
    135 help achieving a better result. </p>
     135help achieve a better result. </p>
     136
     137<h3> 1.3. Dynamic thresholding </h3>
     138
     139<p> Dynamic thresholding consists in studying the image before selecting
     140the threshold values. One strategy, for instance, is to choose the median
     141pixel value. This is done simply by computing a histogram of the image. </p>
     142
     143<p style="text-align: center;">
     144  <img src="out1-3-1.png" width="256" height="256"
     145       class="inline" alt="2-colour dynamic threshold" />
     146  <img src="grad1-3-1.png" width="32" height="256"
     147       class="inline" alt="2-colour dynamic threshold gradient" />
     148  <img src="out1-3-2.png" width="256" height="256"
     149       class="inline" alt="5-colour dynamic threshold" />
     150  <img src="grad1-3-2.png" width="32" height="256"
     151       class="inline" alt="5-colour dynamic threshold gradient" />
     152</p>
    136153
    137154<div style="float: right;">
  • www/study/study.py

    r2098 r2112  
    197197    test12y(gradient256bw, 3).save("grad1-2-3.png")
    198198    test12y(gradient256bw, 5).save("grad1-2-4.png")
     199
     200# Output 1.3.1: 2-colour threshold, dynamic thresholding
     201def test13x(src, n):
     202    (w, h) = src.size()
     203    dest = Image((w, h))
     204    # Compute histogram
     205    histo = [0] * 256
     206    for y in range(h):
     207        for x in range(w):
     208            histo[(int)(src.getGray(x, y) * 255.9999)] += 1
     209    thresholds = [1. * (1. + i) / n for i in range(n - 1)]
     210    values = [i / (n - 1.) for i in range(n)]
     211    # Parse histogram
     212    total = 0
     213    t = 0
     214    for i in range(256):
     215        total += histo[i]
     216        if total > thresholds[t] * w * h:
     217            thresholds[t] = i / 255.0
     218            t += 1
     219            if t + 1 > n - 1:
     220                break
     221    # Compute image
     222    for y in range(h):
     223        for x in range(w):
     224            c = src.getGray(x, y)
     225            for (i, t) in enumerate(thresholds):
     226                if c < t:
     227                    dest.setGray(x, y, values[i])
     228                    break
     229            else:
     230                dest.setGray(x, y, values[n - 1])
     231    return dest
     232
     233if chapter(1):
     234    test13x(lenna256bw, 2).save("out1-3-1.png")
     235    test13x(gradient256bw, 2).save("grad1-3-1.png")
     236    test13x(lenna256bw, 5).save("out1-3-2.png")
     237    test13x(gradient256bw, 5).save("grad1-3-2.png")
    199238
    200239##############################################################################
Note: See TracChangeset for help on using the changeset viewer.