-
Notifications
You must be signed in to change notification settings - Fork 7
/
cp.py
executable file
·190 lines (165 loc) · 5.45 KB
/
cp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
'''
pr0pto
.pto utilities
Copyright 2012 John McMaster
'''
import argparse
import sys
from xystitch.pto.project import PTOProject
from xystitch.optimizer import pto2icm
#from xystitch.pto.util import *
from xystitch.util import IOTimestamp, IOLog
from xystitch.benchmark import Benchmark
import os
import math
import matplotlib.pyplot as plt
def pto2cps(pto):
'''
Enumerate all control point differences
Map to a dictionary of (n, N) lists containing (dx, dy) tuples
Also create an index of all the entries?
'''
ret = {}
for cpl in pto.control_point_lines:
n = cpl.getv('n')
N = cpl.getv('N')
imgn = pto.image_lines[n]
imgN = pto.image_lines[N]
dx = (imgn.getv('d') - cpl.getv('x')) - (imgN.getv('d') -
cpl.getv('X'))
dy = (imgn.getv('e') - cpl.getv('y')) - (imgN.getv('e') -
cpl.getv('Y'))
# Make canonical
# Usually n < N
if N > n:
n, N = N, n
dx = -dx
dy = -dy
points = ret.get((n, N), [])
points.append((dx, dy))
ret[(n, N)] = points
return ret
def rm_image_cps(self, indexes):
'''
Given a list of n, N tuples, delete all control points matching indexes
'''
removed = 0
newls = []
for cpl in self.control_point_lines:
n = cpl.get_variable('n')
N = cpl.get_variable('N')
if not ((n, N) in indexes or (N, n) in indexes):
newls.append(cpl)
else:
removed += 1
self.control_point_lines = newls
return removed
def check_cp(pto):
print('Building CP map')
cps = pto2cps(pto)
errors = []
for cpk, diffs in sorted(cps.items()):
n, N = cpk
img1 = pto.i2img(n).get_name()
img2 = pto.i2img(N).get_name()
# Compute error
rms = 0.0
for diff in diffs:
dx, dy = diff
rms += math.sqrt(dx**2 + dy**2)
rms /= len(diffs)
print('%s - %s: % 6.1f:' % (img1, img2, rms))
for diff in diffs:
dx, dy = diff
print(' % 6.1f % 6.1f' % (dx, dy))
errors.append((rms, (img1, img2)))
errors = sorted(errors)
print('Worst:')
rms, (img1, img2) = errors[-1]
print(' %s - %s: % 6.1f' % (img1, img2, rms))
if 0:
rmss = []
for rms, (_img1, _img2) in errors:
rmss.append(rms)
plt.hist(rmss, normed=True, bins=32)
plt.show()
if 1:
# From histogram plot
# 1 translation error is about 25
# These are way outside valid range
thresh = 50.0
print()
bad = 0
print('Thresh = % 6.1f' % thresh)
toremove = set()
for rms, (img1, img2) in errors:
if rms > thresh:
bad += 1
print(' %s - %s: % 6.1f' % (img1, img2, rms))
i1 = pto.get_image_by_fn(img1).get_index()
i2 = pto.get_image_by_fn(img2).get_index()
toremove.add((i1, i2))
bad += 1
if 1:
print('Removing CPs between %d image pairs' % len(toremove))
removed = rm_image_cps(pto, toremove)
print('Removed %d CPs' % removed)
def parser_add_bool_arg(yes_arg, default=False, **kwargs):
dashed = yes_arg.replace('--', '')
dest = dashed.replace('-', '_')
parser.add_argument(yes_arg,
dest=dest,
action='store_true',
default=default,
**kwargs)
parser.add_argument('--no-' + dashed,
dest=dest,
action='store_false',
**kwargs)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Manipulate .pto files')
parser.add_argument('--verbose',
action="store_true",
help='Verbose output')
parser.add_argument('pto',
metavar='.pto in',
nargs=1,
help='project to work on')
parser.add_argument('out',
metavar='.pto out',
nargs='?',
help='output file, default to override input')
args = parser.parse_args()
pto_in = args.pto[0]
pto_out = args.out
if pto_out is None:
pto_out = pto_in
exist = os.path.exists('pr0npto.log')
# can easily be multiple invocations, save all data
_outlog = IOLog(obj=sys, name='stdout', out_fn='pr0npto.log', mode='a')
_errlog = IOLog(obj=sys, name='stderr', out_fd=_outlog.out_fd)
if True or args.stampout:
_outdate = IOTimestamp(sys, 'stdout')
_errdate = IOTimestamp(sys, 'stderr')
if exist:
_outlog.out_fd.write('\n')
_outlog.out_fd.write('\n')
_outlog.out_fd.write('\n')
_outlog.out_fd.write('*' * 80 + '\n')
_outlog.out_fd.write('*' * 80 + '\n')
_outlog.out_fd.write('*' * 80 + '\n')
print('pr0npto starting')
print('In: %s' % pto_in)
print('Out: %s' % pto_out)
bench = Benchmark()
pto = PTOProject.from_file_name(pto_in)
# Make sure we don't accidently override the original
pto.remove_file_name()
print('Checking for bad CPs')
check_cp(pto)
if 1:
print('Saving to %s' % pto_out)
pto.save_as(pto_out)
bench.stop()
print('Completed in %s' % bench)