def mergeCollections( self, from_anno, to_anno ): "merge from_anno into to_anno if possible" # check that they have compatable scales scale_diff = to_anno.scale / from_anno.scale if scale_diff>2.5 or scale_diff<0.4: return False # check that they are not overlapping bb_from = GeomUtils.strokelistBoundingBox( from_anno.Strokes ) bb_to = GeomUtils.strokelistBoundingBox( to_anno.Strokes ) if GeomUtils.boundingboxOverlap( bb_from, bb_to ): return False # bb[0]-------+ # | | # | | # | (0,0) | # +--------bb[1] # check that they are next to each other if abs( bb_from[1].X - bb_to[0].X ) > to_anno.scale * 0.75 \ and abs( bb_from[0].X - bb_to[1].X ) > to_anno.scale * 0.75 : return False # check y's overlap if bb_from[0].Y - bb_to[1].Y < 0 \ or bb_to[0].Y - bb_from[1].Y < 0 : return False # now we know that we want to merge these text annotations if bb_from[0].X - bb_to[0].X > 0 : to_anno.text = to_anno.text + from_anno.text else : to_anno.text = from_anno.text + to_anno.text to_anno.scale = max( to_anno.scale, from_anno.scale ) return True
def onStrokeAdded( self, stroke ): "Compare this stroke to all templates, and annotate those matching within some threshold." logger.debug("Scoring stroke") #strokeVector = ( len(stroke.Points), GeomUtils.pointListOrientationHistogram(GeomUtils.strokeNormalizeSpacing(stroke, numpoints = len(stroke.Points) / 3.0).Points) ) strokeVector = generateFeatureVector(stroke) logger.debug("Stroke Vector: %s" % (str(strokeVector))) if self._matchVector == None: self._matchVector = strokeVector else: bb1 = GeomUtils.strokelistBoundingBox([stroke]) for prevStk in self._features.keys(): bb2 = GeomUtils.strokelistBoundingBox([prevStk]) if GeomUtils.boundingboxOverlap(bb1, bb2): self.overlaps.setdefault(stroke, set()).add(prevStk) self.overlaps.setdefault(prevStk, set()).add(stroke) self._features[stroke] = strokeVector score = scoreVector(self._matchVector, strokeVector) logger.debug(" Distance %s from baseline" % (score) ) if score < 0.02: self.getBoard().AnnotateStrokes([stroke], MultiStrokeAnnotation("Match")) for stk in self.overlaps.get(stroke, []): multiVect = addVectors( [self._features[stroke], self._features[stk] ] ) logger.debug("Multiple vector: %s" % (str(multiVect))) score = scoreVector(self._matchVector, multiVect) logger.debug(" Distance %s from baseline" % (score) ) if score < 0.02: self.getBoard().AnnotateStrokes([stroke, stk], MultiStrokeAnnotation("Match"))