-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_stitch.py
1410 lines (1324 loc) · 68.1 KB
/
template_stitch.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from schema import Schema
from prims import Rect, Point, ROUNDING
import cv2
import numpy as np
import logging
from itertools import combinations
from utils import pad_images_to_same_size
from math import log10, ceil, floor
from datetime import datetime
from progressbar.bar import ProgressBar
import threading
from config import *
# Usage:
# Create the Profiler() object at the point where you want benchmarking to start
#
# Call profiler.lap('checkpoint name') at every point you want benchmarked. For
# best results, add a dummy 'top' and 'end' checkpoint at the top and end of the
# loop or routine.
#
# Call profiler.stats() to print the results.
class Profiler():
def __init__(self):
self.last_time = datetime.now()
self.timers = {
}
def lap(self, id):
current_time = datetime.now()
if id in self.timers:
delta = (current_time - self.last_time)
self.timers[id] += delta
else:
self.timers[id] = current_time - self.last_time
self.last_time = current_time
def stats(self):
sorted_times = dict(sorted(self.timers.items(), key=lambda x: x[1], reverse=True))
for i, (id, delta) in enumerate(sorted_times.items()):
logging.info(f' {id}: {delta}')
if i >= 2: # just list the top 3 items for now
break
class StitchState():
@staticmethod
def alloc_nested(inner, outer):
ret = []
for _o in range(outer):
ret += [
[None for _m in range(inner)]
]
return ret
@staticmethod
def alloc_list(length):
return [None for _m in range(length)]
def __init__(self, schema, ref_layers, moving_layer):
self.schema = schema
self.ref_layers = ref_layers
self.moving_layer = moving_layer
# base data
self.ref_imgs = []
self.ref_metas = []
self.ref_tiles = []
self.moving_img = None
self.moving_meta = None
self.moving_tile = None
for ref_layer in ref_layers:
tile = self.schema.schema['tiles'][ref_layer]
if tile is None:
logging.warning(f"layer {ref_layer} does not exist, skipping...")
self.ref_metas += [Schema.meta_from_tile(tile)]
self.ref_imgs += [self.schema.get_image_from_layer(ref_layer, thumb=False)]
self.ref_tiles += [tile]
tile = self.schema.schema['tiles'][moving_layer]
assert tile is not None, f"The layer to be stitched {moving_layer} is missing!"
self.moving_img = self.schema.get_image_from_layer(moving_layer, thumb=False)
self.moving_meta = Schema.meta_from_tile(tile)
self.moving_tile = tile
### derived data
self.num_refs = len(ref_layers)
# the proposed offset for the moving image for a given ref layer
self.stepping_vectors = [None] * self.num_refs
# best and current templates from the list of possible templates for a given index
self.best_templates = [None] * self.num_refs
# Current Template Selected list - shortened name because it's used everywhere
self.cts = [None] * self.num_refs
# the actual pixel data of the template used for template matching
self.templates = [[None] for x in range(self.num_refs)]
# the rectangle that defines the template, relative to the moving image origin
self.template_rects = [[None] for x in range(self.num_refs)]
# the backtrack vector for the template - the vector needed to reverse the match point into an image offset
self.template_backtrack = [[None] for x in range(self.num_refs)] # was named 'template_refs'
# the full region of intersection between the ref & moving images
self.intersection_rects = [None] * self.num_refs
self.ref_laplacians = [None] * self.num_refs
# the contours themselves
self.contours = [[None] for x in range(self.num_refs)]
# hierarchy of contours that match (to discover nested solutions, etc.)
self.hierarchies = [[None] for x in range(self.num_refs)]
# best match based on the current template convolution
self.match_pts = [[None] for x in range(self.num_refs)]
# template convolved with reference image
self.convolutions = [[None] for x in range(self.num_refs)] # was named 'results'
# tuple of (has_single_solution: bool, score: float, num_solns: int)
self.solutions = [[None] for x in range(self.num_refs)]
# the vector needed to get the reference and moving images to overlap (or so we hope)
self.adjustment_vectors = [[None] for x in range(self.num_refs)]
# reference data
# ASSUME: all frames are identical in size. This is a rectangle that defines the size of a single full frame.
self.full_frame = Rect(Point(0, 0), Point(X_RES, Y_RES))
# extract the initial template data
no_overlap = True
for i in range(self.num_refs):
if self.guess_template(i, multiple=True):
no_overlap = False
self.no_overlap = no_overlap
# other state
self.best_mses = [1e100] * self.num_refs
self.best_matches = [(0, 0)] * self.num_refs
def index_range(self):
return range(self.num_refs)
def num_indices(self):
return self.num_refs
def match_pt(self, i):
return self.match_pts[i][self.cts[i]]
def update_match_pt(self, i, match_pt):
self.match_pts[i][self.cts[i]] = match_pt
# returns the best MSE match seen so far. Only valid if we've done any MSE matching.
def best_mse_match_pt(self, i):
return self.best_matches[i]
def adjustment_vector(self, index):
return self.adjustment_vectors[index][self.cts[index]]
# Guess a template for a given reference image index
def guess_template(self, i, multiple=False):
# Determine the nominal offsets based upon the machine's programmed x/y coordinates
# for the image, based on the nominal stepping programmed into the imaging run.
# For a perfect mechanical system:
# moving_img + stepping_vector_px "aligns with" ref_img
self.stepping_vectors[i] = Point(
((self.ref_metas[i]['x'] * 1000)
- (self.moving_meta['x'] * 1000)) * Schema.PIX_PER_UM,
((self.ref_metas[i]['y'] * 1000)
- (self.moving_meta['y'] * 1000)) * Schema.PIX_PER_UM
)
# a negative -x stepping vector means that the sample image is to the right of the reference image
# a negative -y stepping vector means that the sample image is below the reference image
# create an initial "template" based on the region of overlap between the reference and moving images
self.intersection_rects[i] = self.full_frame.intersection(self.full_frame.translate(self.stepping_vectors[i]))
if self.intersection_rects[i] is None:
logging.warning(f"No overlap found between\ {self.ref_tiles[i]},\n {self.moving_tile}")
return False # no overlap at all
# set the current selection and best to the 0th index, as a starting point
self.best_templates[i] = 0
self.cts[i] = 0
if multiple is False:
# scale down the intersection template so we have a search space:
# It's a balance between search space (where you can slide the template around)
# and specificity (bigger template will have a chance of encapsulating more features)
if False:
self.template_rects[i][self.cts[i]] = self.intersection_rects[i].scale(SEARCH_SCALE)
else:
# turns out a smaller, square template works better in general?
squared_region = self.intersection_rects[i].scale(SEARCH_SCALE).to_square()
# heuristic: slide the template "up" just a little bit because we generally have
# more overlap toward the edge of the frame
if False:
up_offset = squared_region.tl.y / 2
self.template_rects[i][self.cts[i]] = squared_region.translate(Point(0, -up_offset))
else:
self.template_rects[i][self.cts[i]] = squared_region
self.templates[i][self.cts[i]] = self.moving_img[
round(self.template_rects[i][self.cts[i]].tl.y) \
: round(self.template_rects[i][self.cts[i]].br.y),
round(self.template_rects[i][self.cts[i]].tl.x) \
: round(self.template_rects[i][self.cts[i]].br.x)
].copy()
# trace the template's extraction point back to the moving rectangle's origin
scale_offset = self.template_rects[i][self.cts[i]].tl - self.intersection_rects[i].tl
self.template_backtrack[i][self.cts[i]] \
= (-self.stepping_vectors[i]).clamp_zero() + scale_offset
else:
intersection_w = self.intersection_rects[i].width()
intersection_h = self.intersection_rects[i].height()
template_dim = intersection_w
# find the smallest square that fits
if template_dim > intersection_h:
template_dim = intersection_h
template_dim = template_dim * SEARCH_SCALE
# limit the max template dimension
if template_dim > MAX_TEMPLATE_PX:
template_dim = MAX_TEMPLATE_PX
# adjust for high aspect ratio situations, but preferring square
if intersection_w / intersection_h > 2:
template_dim_x = template_dim * 2
template_dim_y = template_dim
elif intersection_w / intersection_h < 2:
template_dim_x = template_dim
template_dim_y = template_dim * 2
else:
template_dim_x = template_dim
template_dim_y = template_dim
# allocate placeholders for all the templates
x_range = range(int(self.intersection_rects[i].tl.x), int(self.intersection_rects[i].br.x), int(template_dim_x // 2))
y_range = range(int(self.intersection_rects[i].tl.y), int(self.intersection_rects[i].br.y), int(template_dim_y // 2))
self.templates[i] = StitchState.alloc_list(len(x_range) * len(y_range))
self.template_rects[i] = StitchState.alloc_list(len(x_range) * len(y_range))
self.template_backtrack[i] = StitchState.alloc_list(len(x_range) * len(y_range))
self.contours[i] = StitchState.alloc_list(len(x_range) * len(y_range))
self.hierarchies[i] = StitchState.alloc_list(len(x_range) * len(y_range))
self.match_pts[i] = StitchState.alloc_list(len(x_range) * len(y_range))
self.convolutions[i] = StitchState.alloc_list(len(x_range) * len(y_range))
self.solutions[i] = StitchState.alloc_list(len(x_range) * len(y_range))
self.adjustment_vectors[i] = StitchState.alloc_list(len(x_range) * len(y_range))
templ_index = 0
for x in x_range:
if x + template_dim_x >= self.intersection_rects[i].br.x:
x = self.intersection_rects[i].br.x - template_dim_x # align last iter to right edge
for y in y_range:
if y + template_dim_y >= self.intersection_rects[i].br.y:
y = self.intersection_rects[i].br.y - template_dim_y # align last iter to bottom edge
template_rect = Rect(
Point(int(x), int(y)),
Point(int(x + template_dim_x), int(y + template_dim_y))
)
self.template_rects[i][templ_index] = template_rect
self.templates[i][templ_index] = self.moving_img[
template_rect.tl.y : template_rect.br.y,
template_rect.tl.x : template_rect.br.x
].copy()
scale_offset = template_rect.tl - self.intersection_rects[i].tl
self.template_backtrack[i][templ_index] = (-self.stepping_vectors[i]).clamp_zero() + scale_offset
templ_index += 1
return True
def adjust_template(self, i, template_shift):
self.template_rects[i][self.cts[i]] = self.template_rects[i][self.cts[i]].saturating_translate(template_shift, self.full_frame)
self.templates[i][self.cts[i]] = self.moving_img[
round(self.template_rects[i][self.cts[i]].tl.y) \
: round(self.template_rects[i][self.cts[i]].br.y),
round(self.template_rects[i][self.cts[i]].tl.x) \
: round(self.template_rects[i][self.cts[i]].br.x)
].copy()
# trace the template's extraction point back to the moving rectangle's origin
scale_offset = self.template_rects[i][self.cts[i]].tl - self.intersection_rects[i].tl
self.template_backtrack[i][self.cts[i]] \
= (-self.stepping_vectors[i]).clamp_zero() + scale_offset
def update_adjustment_vector(self, i, template_index = None):
if template_index is None:
t = self.cts[i]
else:
t = template_index
self.adjustment_vectors[i][t] = Point(
self.match_pts[i][t][0] - self.template_backtrack[i][t][0],
self.match_pts[i][t][1] - self.template_backtrack[i][t][1]
)
# returns the match_pt that you would use to create an alignment as if no adjustments were made.
def unadjusted_machine_match_pt(self, i):
# the first Point is needed to compensate for the fact that self.template_backtrack is subtracted
# from match_pts to create the adjustment vector, as seen in the routine immediately above ^^^^
# the second Point is the "naive" offset based on the relative machine offsets of the two tiles
return Point(self.template_backtrack[i][self.cts[i]][0],
self.template_backtrack[i][self.cts[i]][1]) \
+ Point(
round(
(self.ref_tiles[i]['offset'][0] - self.moving_tile['offset'][0]) * Schema.PIX_PER_UM),
round(
(self.ref_tiles[i]['offset'][1] - self.moving_tile['offset'][1]) * Schema.PIX_PER_UM)
)
def get_after_vector(self, i):
return self.stepping_vectors[i] - self.adjustment_vectors[i][self.cts[i]]
# computes the final offset to store in the database. Also has to sum in the offset
# of the reference image, because we did all the computations based on 0-offset on
# the reference image. Returns a coordinate in Pixels, has to be converted to microns
# for storage into the Schema.
def finalize_offset(self, i):
self.update_adjustment_vector(i) # this should already be done...? should we do it again?
return Point(
self.adjustment_vectors[i][self.cts[i]].x + self.ref_tiles[i]['offset'][0] * Schema.PIX_PER_UM,
self.adjustment_vectors[i][self.cts[i]].y + self.ref_tiles[i]['offset'][1] * Schema.PIX_PER_UM,
)
# The auto-alignment kernel. This code must be thread-safe. The thread safety is "ensured"
# by having every operation only refer to its assigned (i,t) slot in memory. So long as
# (i, t) are unique, we should have no collisions.
def auto_align_kernel(self, template, i, t):
if Schema.FILTER_WINDOW > 0:
template_norm = cv2.GaussianBlur(template, (Schema.FILTER_WINDOW, Schema.FILTER_WINDOW), 0)
template_norm = template_norm.astype(np.float32)
else: # normalize instead of filter
template_norm = cv2.normalize(template, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
# find edges
template_laplacian = cv2.Laplacian(template_norm, -1, ksize=Schema.LAPLACIAN_WINDOW)
# apply template matching. If the ref image and moving image are "perfectly aligned",
# the value of `match_pt` should be equal to `template_ref`
# i.e. alignment criteria: match_pt - template_ref = (0, 0)
METHOD = cv2.TM_CCOEFF # convolutional matching
res = cv2.matchTemplate(self.ref_laplacians[i], template_laplacian, METHOD)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
self.match_pts[i][t] = max_loc
self.convolutions[i][t] = cv2.normalize(res, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
ret, thresh = cv2.threshold(self.convolutions[i][t], CONTOUR_THRESH, 255, 0)
# find contours of candidate matches
self.contours[i][t], self.hierarchies[i][t] = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
has_single_solution = True
score = None
num_solns = len(self.hierarchies[i][t][0])
if False:
for j, c in enumerate(self.contours[i][t]):
if self.hierarchies[i][t][0][j][3] == -1 and num_solns < MAX_SOLUTIONS:
if cv2.pointPolygonTest(c, self.match_pts[i][t], False) >= 0.0: # detect if point is inside or on the contour. On countour is necessary to detect cases of exact matches.
if score is not None:
has_single_solution = False
score = cv2.contourArea(c)
logging.debug(f"countour {c} contains {self.match_pts[i][t]} and has area {score}")
logging.debug(f" score: {score}")
else:
# print(f"countour {c} does not contain {top_left}")
pass
else:
if cv2.pointPolygonTest(c, self.match_pts[i][t], False) > 0:
logging.debug(f"{self.match_pts[i][t]} of {i} is contained within a donut-shaped region. Suspect blurring error!")
has_single_solution = False
else:
for j, c in enumerate(self.contours[i][t]):
if cv2.pointPolygonTest(c, self.match_pts[i][t], False) >= 0.0: # detect if point is inside or on the contour. On countour is necessary to detect cases of exact matches.
score = cv2.contourArea(c)
logging.debug(f"countour {c} contains {self.match_pts[i][t]} and has area {score}")
logging.debug(f" score: {score}")
else:
# print(f"countour {c} does not contain {top_left}")
pass
self.solutions[i][t] = (has_single_solution, score, num_solns)
if score is not None:
self.update_adjustment_vector(i, template_index=t)
# Compute an auto-alignment against a given index
def auto_align(self, i, multiple=False, multithreading=False):
if multiple is True:
template_range = range(len(self.templates[i]))
if not multithreading:
progress = ProgressBar(min_value=0, max_value=len(template_range), prefix='Matching ').start()
else:
template_range = range(self.cts[i], self.cts[i] + 1) # just 'iterate' over that one index
# recompute every call because the FILTER_WINDOW and LAPLACIAN_WINDOW can change on us
if Schema.FILTER_WINDOW > 0:
moving_norm = cv2.GaussianBlur(self.moving_img, (Schema.FILTER_WINDOW, Schema.FILTER_WINDOW), 0)
moving_norm = moving_norm.astype(np.float32)
else:
moving_norm = cv2.normalize(self.moving_img, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
if Schema.FILTER_WINDOW > 0:
ref_norm = cv2.GaussianBlur(self.ref_imgs[i], (Schema.FILTER_WINDOW, Schema.FILTER_WINDOW), 0)
ref_norm = ref_norm.astype(np.float32)
else:
ref_norm = cv2.normalize(self.ref_imgs[i], None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
self.ref_laplacians[i] = cv2.Laplacian(ref_norm, -1, ksize=Schema.LAPLACIAN_WINDOW)
if multithreading:
threads = []
for t in template_range:
if self.templates[i][t] is not None:
thread = threading.Thread(target=self.auto_align_kernel, args=(self.templates[i][t], i, t))
thread.start()
threads += [thread]
for thread in threads:
thread.join()
else:
for t in template_range:
if self.templates[i][t] is not None:
self.auto_align_kernel(self.templates[i][t], i, t)
if multiple:
progress.update(t)
if multiple:
progress.finish()
def mse_cleanup(self, picked):
self.reset_mse_tracker(picked)
step = 'SEARCH_COARSE'
was_coarse = True
mse_search = {
'n' : None,
'w' : None,
'e' : None,
's' : None,
# this should be last so that the final corr/log_mse value can be recalled on the last iteration
'current' : None,
}
offsets = {
'n': (0, -1),
'w': (-1, 0),
'e': (1, 0),
's': (0, 1),
'current': (0, 0),
}
coarse_offsets = {
'current': (0, 0),
'n': (0, -2*ceil(Schema.PIX_PER_UM)),
'w': (-2*ceil(Schema.PIX_PER_UM), 0),
'e': (2*ceil(Schema.PIX_PER_UM), 0),
's': (0, 2*ceil(Schema.PIX_PER_UM)),
}
steps = 1
current_point = self.match_pt(picked)
starting_point = current_point
corr = None
log_mse = None
key = -1
while step != 'DONE':
if steps >= MSE_SEARCH_LIMIT: # terminate if a limit cycle seems to be reached
logging.error("Step limit exceeded! aborting MSE cleanup.")
self.update_match_pt(picked, starting_point)
self.update_adjustment_vector(picked)
(corr, log_mse) = self.compute_mse(picked)
logging.debug(f"mse {log_mse}")
self.show_mse(picked, (corr, log_mse), console=False, draw=False)
cv2.waitKey() # force a redraw & pause
return steps
if key != -1:
# abort due to keystroke: return a negative steps closure result
return -1
if step == 'SEARCH' or step == 'SEARCH_COARSE':
for direction in mse_search.keys():
if step == 'SEARCH':
offset = offsets[direction]
else:
offset = coarse_offsets[direction]
test_point = (current_point[0] + offset[0], current_point[1] + offset[1])
self.update_match_pt(picked, test_point)
self.update_adjustment_vector(picked)
(corr, log_mse) = self.compute_mse(picked)
logging.debug(f"mse {log_mse}")
self.show_mse(picked, (corr, log_mse), console=False)
mse_search[direction] = log_mse
step = 'EVAL_SEARCH'
elif step == 'EVAL_SEARCH':
sorted_results = sorted(mse_search.items(), key=lambda kv: kv[1])
best_direction = sorted_results[0][0]
best_mse = sorted_results[0][1]
if best_direction == 'current':
# restore the starting point, we're done
self.update_match_pt(picked, current_point)
self.update_adjustment_vector(picked)
self.show_mse(picked, (corr, log_mse), console=False)
key = cv2.waitKey(1) # force a redraw
if was_coarse:
# now do a fine search
was_coarse = False
step = 'SEARCH'
else:
step = 'DONE'
else:
offset = offsets[best_direction]
# define the new starting point in the "best" direction
steps += 1
current_point = (current_point[0] + offset[0], current_point[1] + offset[1])
# follow gradient until it doesn't get better
follow_gradient = True
while follow_gradient:
test_point = (current_point[0] + offset[0], current_point[1] + offset[1])
self.update_match_pt(picked, test_point)
self.update_adjustment_vector(picked)
(corr, log_mse) = self.compute_mse(picked)
logging.debug(f"mse {log_mse}")
if log_mse > best_mse:
# reset back to our previous point
self.update_match_pt(picked, current_point)
self.update_adjustment_vector(picked)
# do a 4-way search if we hit a gradient end point
step = 'SEARCH_COARSE'
was_coarse = True
follow_gradient = False
else:
# adopt the new result as "best"
steps += 1
current_point = (current_point[0] + offset[0], current_point[1] + offset[1])
self.show_mse(picked, (corr, log_mse), console=False)
key = cv2.waitKey(1) # force a redraw
# fall through to next iteration, continuing the search
logging.info(f"MSE refinement pass finished in {steps} steps")
return steps
# computes the best score at the current template for each ref image
def best_scoring_index(self, multiple=False):
if multiple:
# go through each ref index and search the entire space of templates for the best match.
# best template is automatically selected by this, for each ref index
score = None
solns = None
picked = None
for i, solution_list in enumerate(self.solutions):
if self.intersection_rects[i] is None:
continue # can't score things that didn't intersect
best_template = None
for template_index, (ss, soln_score, num_solns) in enumerate(solution_list):
if ss is None:
continue # skip this selection because it's not valid
if soln_score is not None and round(soln_score, 1) == 0.0:
# HEURISITIC: extremely low score of 0 usually means the match is a normalization
# artifact -- nothing was really matching, this just happens to be the best matching
# point in a region of "meh"-ness. Reject these points.
continue
if score is None:
# first iteration, just pick the first thing we found
picked = i
best_template = template_index
score = soln_score
solns = num_solns
else:
if soln_score is not None:
# HEURISTIC: if we've already got something that passes our basic threshold,
# reject any solution that is less unique
if score < FAILING_SCORE:
if num_solns > solns:
continue
# if the score is better, OR if it is substantially more unique,
# pick the new solution.
if soln_score < score or num_solns < solns / 2:
score = soln_score
picked = i
best_template = template_index
solns = num_solns
if best_template is not None:
self.best_templates[i] = best_template
self.cts[i] = best_template
# else stick with the default, e.g. 0
return picked
else:
score = None
picked = None
for i, solution_list in enumerate(self.solutions):
(ss, soln_score, num_solns) = solution_list[self.cts[i]]
if ss is None:
continue # skip this selection because it's not valid
if score is None:
picked = i
score = soln_score
else:
if soln_score is not None:
if soln_score < score:
score = soln_score
picked = i
return picked
# this forces a valid score if one doesn't exist. Invoke during manual shifting
# and "going with whatever the user picked"
def force_score(self, i):
self.solutions[i][self.cts[i]] = (True, FAILING_SCORE - 1, -1)
# returns:
# 'GOOD' if the proposed index meets our quality criteria for an auto-match
# 'AMBIGUOUS' if the proposed index match quality is low
# 'OUT_OF_RANGE' if the proposed index match is out of range
def eval_index(self, i):
(single_solution, score, num_solns) = self.solutions[i][self.cts[i]]
if score is not None and single_solution and score < FAILING_SCORE:
if abs(self.adjustment_vectors[i][self.cts[i]].x) > X_REVIEW_THRESH_UM * Schema.PIX_PER_UM \
or abs(self.adjustment_vectors[i][self.cts[i]].y) > Y_REVIEW_THRESH_UM * Schema.PIX_PER_UM:
return f'OUT_OF_RANGE ({self.adjustment_vectors[i][self.cts[i]].x / Schema.PIX_PER_UM:0.1f}um, {self.adjustment_vectors[i][self.cts[i]].y / Schema.PIX_PER_UM:0.1f}um)'
else:
return 'GOOD'
else:
return 'AMBIGUOUS'
def has_single_solution(self, i):
(single_solution, score, num_solns) = self.solutions[i][self.cts[i]]
return single_solution
def score(self, i):
(single_solution, score, num_solns) = self.solutions[i][self.cts[i]]
return score
def num_solutions(self, i):
(single_solution, score, num_solns) = self.solutions[i][self.cts[i]]
return num_solns
def show_contours(self, i):
if self.convolutions[i][self.cts[i]] is not None and self.contours[i][self.cts[i]] is not None:
cv2.drawContours(self.convolutions[i][self.cts[i]], self.contours[i][self.cts[i]], -1, (0,255,0), 1)
cv2.imshow('contours', cv2.resize(self.convolutions[i][self.cts[i]], None, None, 0.5, 0.5))
def score_as_str(self, i):
msg = ''
(single_solution, score, num_solns) = self.solutions[i][self.cts[i]]
if score is not None:
msg += f' score {score:0.1f}, {num_solns} solns'
else:
msg += f' score NONE'
if single_solution != True:
msg += ' (donut topology)'
return msg
def show_debug(self, i, msg = ''):
# compute after vector without storing the result
after_vector_px = self.get_after_vector(i)
ref_overlap = self.full_frame.intersection(
self.full_frame.translate(Point(0, 0) - after_vector_px)
)
moving_overlap = self.full_frame.intersection(
self.full_frame.translate(after_vector_px)
)
if ref_overlap is None or moving_overlap is None:
after = np.zeros((500, 500), dtype=np.uint8)
else:
after = np.hstack(pad_images_to_same_size(
(
# int() not round() used because python's "banker's rounding" sucks.
cv2.resize(self.ref_imgs[i][
int(ref_overlap.tl.y) : int(ref_overlap.br.y),
int(ref_overlap.tl.x) : int(ref_overlap.br.x)
], None, None, PREVIEW_SCALE * SEARCH_SCALE, PREVIEW_SCALE * SEARCH_SCALE),
cv2.resize(self.moving_img[
int(moving_overlap.tl.y):int(moving_overlap.br.y),
int(moving_overlap.tl.x):int(moving_overlap.br.x)
], None, None, PREVIEW_SCALE * SEARCH_SCALE, PREVIEW_SCALE * SEARCH_SCALE)
)
))
cv2.putText(
after, msg,
org=(25, 50),
fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=1, color=(255, 255, 255), thickness=1, lineType=cv2.LINE_AA
)
cv2.putText(
after, f'REF',
org=(25, 25),
fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=1, color=(255, 255, 255), thickness=1, lineType=cv2.LINE_AA
)
cv2.putText(
after, f'SAMPLE (aligned)',
org=(after.shape[1] // 2 + 25, 25),
fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=1, color=(255, 255, 255), thickness=1, lineType=cv2.LINE_AA
)
if False:
overview = np.hstack(pad_images_to_same_size(
(
cv2.resize(ref_img, None, None, PREVIEW_SCALE / 2, PREVIEW_SCALE / 2),
cv2.resize(moving_img, None, None, PREVIEW_SCALE / 2, PREVIEW_SCALE / 2),
)
))
else:
overview = cv2.normalize(
np.hstack(pad_images_to_same_size(
(
cv2.resize(self.ref_imgs[i], None, None, PREVIEW_SCALE / 2, PREVIEW_SCALE / 2),
cv2.resize(self.moving_img, None, None, PREVIEW_SCALE / 2, PREVIEW_SCALE / 2),
)
)),
None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U
)
cv2.putText(
overview, f'REF ({self.ref_layers[i]}: {self.ref_metas[i]["x"]:0.2f}, {self.ref_metas[i]["y"]:0.2f}) (full/norm)', # full image & normalized
org=(25, 25),
fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=1, color=(255, 255, 255), thickness=1, lineType=cv2.LINE_AA
)
cv2.putText(
overview, f'SAMPLE ({self.moving_layer}: {self.moving_meta["x"]:0.2f}, {self.moving_meta["y"]:0.2f}) (full/norm/unaligned)', # full image, normalized, unaligned
org=(overview.shape[1] // 2 + 25, 25),
fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=1, color=(255, 255, 255), thickness=1, lineType=cv2.LINE_AA
)
before_after = np.vstack(
pad_images_to_same_size(
(cv2.resize(self.templates[i][self.cts[i]], None, None, PREVIEW_SCALE, PREVIEW_SCALE),
after, overview)
)
)
cv2.imshow('debug', before_after)
def show_stitch_preview(self, i, msg = '', help_msg=[]):
after_vector = self.get_after_vector(i)
ref_canvas = np.zeros(
(self.ref_imgs[i].shape[0] + ceil(abs(after_vector.y)),
self.ref_imgs[i].shape[1] + ceil(abs(after_vector.x))
), dtype=np.uint8
)
moving_canvas = np.zeros(
(self.ref_imgs[i].shape[0] + ceil(abs(after_vector.y)),
self.ref_imgs[i].shape[1] + ceil(abs(after_vector.x))
), dtype=np.uint8
)
ref_orig = Point(0, 0)
moving_orig = Point(0, 0)
rh, rw = self.ref_imgs[i].shape
if after_vector.x >= 0:
ref_orig.x = round(after_vector.x)
moving_orig.x = 0
else:
ref_orig.x = 0
moving_orig.x = round(-after_vector.x)
if after_vector.y >= 0:
ref_orig.y = round(after_vector.y)
moving_orig.y = 0
else:
ref_orig.y = 0
moving_orig.y = round(-after_vector.y)
ref_canvas[
ref_orig.y : ref_orig.y + rh,
ref_orig.x : ref_orig.x + rw
] = self.ref_imgs[i]
moving_canvas[
moving_orig.y : moving_orig.y + rh,
moving_orig.x : moving_orig.x + rw
] = self.moving_img
composite_canvas = cv2.addWeighted(ref_canvas, 0.5, moving_canvas, 0.5, 0.0)
cv2.putText(
composite_canvas, msg,
org=(max([moving_orig.x, ref_orig.x]), max([moving_orig.y, ref_orig.y])),
fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=2.0, color=(255, 255, 255), thickness=1, lineType=cv2.LINE_AA
)
current_line = 50
for m in help_msg:
(_text_width, text_height) = cv2.getTextSize('ALL CAPS', cv2.FONT_HERSHEY_PLAIN, 2.0, thickness=1)
text_height = text_height * 2 * 1.1
cv2.putText(
composite_canvas, m,
org=(20, int(current_line)),
fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=2.0, color=(255, 255, 255), thickness=1, lineType=cv2.LINE_AA
)
current_line += text_height
cv2.imshow('stitch preview',
cv2.resize(composite_canvas, None, None, 0.5, 0.5) # use different scale because resolution matters here
)
def reset_mse_tracker(self, i):
self.best_mses[i] = 1e100
self.best_matches[i] = (0, 0)
def compute_mse(self, i):
after_vector_px = self.get_after_vector(i)
ref_overlap = self.full_frame.intersection(
self.full_frame.translate(Point(0, 0) - after_vector_px)
)
moving_overlap = self.full_frame.intersection(
self.full_frame.translate(after_vector_px)
)
# display the difference of laplacians of the overlapping region
moving_roi = self.moving_img[
round(moving_overlap.tl.y):round(moving_overlap.br.y),
round(moving_overlap.tl.x):round(moving_overlap.br.x)
]
ref_roi = self.ref_imgs[i][
round(ref_overlap.tl.y) : round(ref_overlap.br.y),
round(ref_overlap.tl.x) : round(ref_overlap.br.x)
]
if Schema.FILTER_WINDOW > 0:
ref_norm = cv2.GaussianBlur(ref_roi, (Schema.FILTER_WINDOW, Schema.FILTER_WINDOW), 0)
moving_norm = cv2.GaussianBlur(moving_roi, (Schema.FILTER_WINDOW, Schema.FILTER_WINDOW), 0)
ref_norm = ref_norm.astype(np.float32)
moving_norm = moving_norm.astype(np.float32)
else:
ref_norm = cv2.normalize(ref_roi, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
moving_norm = cv2.normalize(moving_roi, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
ref_laplacian = cv2.Laplacian(ref_norm, -1, ksize=Schema.LAPLACIAN_WINDOW)
moving_laplacian = cv2.Laplacian(moving_norm, -1, ksize=Schema.LAPLACIAN_WINDOW)
corr = moving_laplacian - ref_laplacian
err = np.sum(corr**2)
h, w = ref_laplacian.shape
mse = err / (float(h*w))
log_mse = round(log10(mse), 5)
return (corr, log_mse)
def show_mse(self, i, computed = None, console=True, draw=True):
if computed is None:
(corr, log_mse) = self.compute_mse(i)
else:
(corr, log_mse) = computed
if log_mse <= self.best_mses[i]:
self.best_mses[i] = log_mse
mse_hint = 'best'
self.best_matches[i] = self.match_pts[i][self.cts[i]]
else:
mse_hint = ''
if console:
logging.info(f"MSE: {log_mse} {mse_hint}")
if draw:
corr_f32 = cv2.normalize(corr, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
(_retval, corr_f32) = cv2.threshold(corr_f32, 0.8, 0.8, cv2.THRESH_TRUNC) # toss out extreme outliers (e.g. bad pixels)
corr_f32 = cv2.normalize(corr_f32, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
# corr_u8 = cv2.normalize(corr_f32, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
cv2.putText(
corr_f32, f"MSE: {log_mse} {mse_hint}",
org=(100, 100),
fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=2, color=(255, 255, 255), thickness=2, lineType=cv2.LINE_AA
)
cv2.imshow("Find minimum MSE", cv2.resize(corr_f32, None, None, 0.5, 0.5))
# Use template matching of laplacians to do stitching
def stitch_one_template(self,
schema,
ref_layers,
moving_layer,
retrench = False,
full_review = False,
mse_cleanup = False,
):
state = StitchState(schema, ref_layers, moving_layer)
if state.no_overlap: # render an error message, and skip the stitching
self.schema.store_auto_align_result(moving_layer, 0, 0, None, False, solutions=0)
logging.warning("No overlap between any reference and moving frame")
preview = [state.moving_img]
for i in state.ref_imgs:
preview.append(i)
overview = cv2.resize(
np.vstack(pad_images_to_same_size(preview)),
None, None, PREVIEW_SCALE / 2, PREVIEW_SCALE / 2
)
cv2.putText(
overview, f"NO OVERLAP: {moving_layer} <-> {ref_layers}",
org=(50, 50),
fontFace=cv2.FONT_HERSHEY_PLAIN,
fontScale=1, color=(255, 255, 255), thickness=1, lineType=cv2.LINE_AA
)
cv2.imshow('before/after', overview)
cv2.waitKey() # pause because no delay is specified
return False, False
removed = False
# compute all the initial stitching guesses
for i in state.index_range():
state.auto_align(i, multiple=True, multithreading=True)
#### Stitching interaction loop
# options are 'AUTO', 'MOVE', 'TEMPLATE' or None
# - 'AUTO' is to try the full auto path
# - 'MOVE' is manual moving of the image itself
# - 'TEMPLATE' is adjusting the template and retrying the autostitch
# - None means to quit
#
# Note that the initial mode *must* be 'AUTO' because the first pass
# sets up a number of intermediates relied upon by the adjustment routines.
mode = 'AUTO'
picked = state.best_scoring_index(multiple=True)
while mode is not None:
msg = ''
if mode == 'TEMPLATE':
state.auto_align(picked, multiple=False)
##### Compute the putative adjustment vector
state.update_adjustment_vector(picked)
#### Decide if user feedback is needed
# use the contours and the matched point to measure the quality of the template match
if retrench:
logging.debug("Manual QC flagged")
msg = 'Manual QC flagged'
mode = 'TEMPLATE'
retrench = False # so we don't keep triggering this in the loop
elif mode == 'AUTO':
verdict = state.eval_index(picked)
if verdict == 'GOOD':
if full_review:
msg = 'Good (edge)'
mode = 'TEMPLATE' # force a review of everything
else:
msg = ''
mode = None
if mse_cleanup:
steps = state.mse_cleanup(picked)
if steps >= MSE_SEARCH_LIMIT:
msg = "Couldn't cleanup MSE! Limit cycle reached."
mode = 'MOVE'
elif steps < 0:
msg = "Manual pause due to keystroke in MSE search"
mode = 'MOVE'
else:
msg = f"Good (mse found in {steps} steps)"
elif verdict == 'AMBIGUOUS':
msg = 'Ambiguous'
mode = 'TEMPLATE'
elif verdict.startswith('OUT_OF_RANGE'):
msg = verdict
mode = 'TEMPLATE'
else:
logging.error(f"Internal error: unrecognized verdict on match: '{verdict}'")
msg = 'Internal Error!'
mode = 'TEMPLATE'
##### Render user feedback
state.show_contours(picked)
state.show_debug(picked)
msg += state.score_as_str(picked)
# preview moved to within each mode so we can display a help message
##### Handle UI cases
hint = state.eval_index(picked)
logging.info(f"{picked}[{state.cts[picked]}]: {state.score_as_str(picked)} ({hint})]")
if mode == 'MOVE':
state.show_mse(picked)
# get user feedback and adjust the match_pt accordingly
help_msg = [
'MOVE IMAGE',
' ',
"'wasd' to move",
'space to accept',
'1 switch to template mode',
'x remove from database',
'y snap to best point',
'Y snap to machine default',
'0 to abort stitching',
'm to undo to last column',
'3 to do auto MSE cleanup'
]
state.show_stitch_preview(picked, msg, help_msg)
key = cv2.waitKey()
COARSE_MOVE = 20
if key != -1:
new_match_pt = None
key_char = chr(key)
logging.debug(f'Got key: {key_char}')
if key_char == ',' or key_char == 'w':
new_match_pt = (state.match_pt(picked)[0] + 0, state.match_pt(picked)[1] - 1)
elif key_char == 'a':
new_match_pt = (state.match_pt(picked)[0] - 1, state.match_pt(picked)[1] + 0)
elif key_char == 'e' or key_char == 'd':
new_match_pt = (state.match_pt(picked)[0] + 1, state.match_pt(picked)[1] + 0)
elif key_char == 'o' or key_char == 's':
new_match_pt = (state.match_pt(picked)[0] + 0, state.match_pt(picked)[1] + 1)
# coarse movement
elif key_char == '<' or key_char == 'W':
new_match_pt = (state.match_pt(picked)[0] + 0, state.match_pt(picked)[1] - COARSE_MOVE)
elif key_char == 'A':
new_match_pt = (state.match_pt(picked)[0] - COARSE_MOVE, state.match_pt(picked)[1] + 0)
elif key_char == 'E' or key_char == 'D':
new_match_pt = (state.match_pt(picked)[0] + COARSE_MOVE, state.match_pt(picked)[1] + 0)
elif key_char == 'O' or key_char == 'S':
new_match_pt = (state.match_pt(picked)[0] + 0, state.match_pt(picked)[1] + COARSE_MOVE)
# reset match point to what a perfect machine would yield
elif key_char == 'Y' or key_char == 'T':
state.update_adjustment_vector(picked)
new_match_pt = state.unadjusted_machine_match_pt(picked)
state.reset_mse_tracker(picked) # reset the MSE score since we're in a totally different regime
elif key_char == 'y' or key_char == 't':
new_match_pt = state.best_mse_match_pt(picked)
elif key_char == ' ': # this accepts the current alignment
mode = None
state.force_score(picked)
elif key_char == 'x': # this rejects the alignment
self.schema.remove_tile(moving_layer)
logging.info(f"Removing tile {moving_layer} from the database")
removed = True
mode = None
elif key_char == '1':
mode = 'TEMPLATE'
elif key_char == '\t': # toggle to another solution
while True:
picked = (picked + 1) % state.num_indices()
if state.intersection_rects[picked] is not None: # make sure the picked index is not invalid
break
logging.info(f"Toggling to index {picked}")
continue # this is very important to ensure that the match_pt isn't side-effected incorrectly on the new solution
elif key_char == '0':
logging.info("Stitch abort!")
return False, True # don't restart, do abort
elif key_char == 'm':
logging.info("Undo to last checkpoint, and manually restitch")
self.schema.undo_to_checkpoint(manual_restitch=True)
return True, False # do restart, don't abort
elif key_char == '3':
state.mse_cleanup(picked)
else:
logging.info(f"Unhandled key: {key_char}, ignoring")
continue