~vcs-imports/shotfactory/trunk

« back to all changes in this revision

Viewing changes to shotfactory03/image/hashmatch.py

  • Committer: johann
  • Date: 2007-05-21 18:10:08 UTC
  • Revision ID: vcs-imports@canonical.com-20070521181008-u6thzk83djf4y3p7
Merging cleanup from shotfactory-django branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
Efficient overlap matching for tall screenshots.
21
21
"""
22
22
 
23
 
__revision__ = '$Rev: 471 $'
24
 
__date__ = '$Date: 2006-06-14 17:31:39 +0100 (Wed, 14 Jun 2006) $'
 
23
__revision__ = '$Rev: 1254 $'
 
24
__date__ = '$Date: 2007-05-21 19:10:08 +0100 (Mon, 21 May 2007) $'
25
25
__author__ = '$Author: johann $'
26
26
 
27
27
import re
28
28
 
29
29
step = 3*64
30
 
 
31
30
header_match = re.compile(r'(P\d) (\d+) (\d+) (\d+)').match
 
31
 
 
32
 
32
33
def read_ppm_header(infile):
33
34
    """
34
35
    Read a PPM file header and return magic, width, height, maxval.
53
54
        elif len(header) >= 4:
54
55
            raise SyntaxError("could not parse PPM header")
55
56
 
 
57
 
56
58
def debug_values(hashtable, minimum = 1):
57
59
    """
58
60
    Print a hash table sorted by value.
68
70
        if value >= minimum:
69
71
            print value, key
70
72
 
 
73
 
71
74
def build_hash(pixels, start, height, row_skip):
72
75
    """
73
76
    Build a dict from a vertical column of detail markers.
90
93
            positions_pop(marker)
91
94
    return positions
92
95
 
 
96
 
93
97
def match_markers(pixels, start, height, row_skip, positions, votes):
94
98
    """
95
99
    Match markers and collect votes for different offset positions.
107
111
            offset = position - y
108
112
            votes[offset] = votes_get(offset, 0) + 1
109
113
 
 
114
 
110
115
def winner(votes, minimum):
111
116
    """
112
117
    Get the offset with the most votes, but 0 only if no other option exists.
130
135
            result = offset
131
136
    return result
132
137
 
 
138
 
133
139
def find_offset(filename1, filename2):
134
140
    """
135
141
    Find the best vertical match between two PPM files.
159
165
    return winner(votes, 3*width/step)
160
166
 
161
167
if __name__ == '__main__':
162
 
    import sys, doctest
 
168
    import doctest
163
169
    errors, tests = doctest.testmod()
164
170
    if errors:
 
171
        import sys
165
172
        sys.exit(1)