def line_up_colinears(lines): ''' Makes lines that are approximately colinear, colinear :param lines: list of lines ''' #TODO: this only handles the case of two colinear lines. It could occur with more if not lines: return o = lines[0].orien _range = lines[-1].pos[o] - lines[0].pos[o] thresh = _range / 500.0 + 5 # TODO: play with this. depends on skew estimate for idx in xrange(len(lines) - 1): cur_line = lines[idx] next_line = lines[idx + 1] diff = next_line.pos[o] - cur_line.pos[o] if diff < thresh: # check that they don't overlap if not utils.are_overlapping([cur_line.length_range(), next_line.length_range()]): # average the positions #print "Colinear" #print cur_line #print next_line val = (next_line.pos[o] + cur_line.pos[o]) / 2 tmp = list(next_line.pos) tmp[o] = val next_line.pos = tuple(tmp) tmp = list(cur_line.pos) tmp[o] = val cur_line.pos = tuple(tmp)
def test(): try: bbox = generate_background_bbox(image_shape, bbox_shape, existing_bboxes) except RuntimeError: return for existing_bbox in existing_bboxes: self.assertFalse(are_overlapping(bbox, existing_bbox))
def match_cost_type(self, i, j): line1 = self.lines1[i-1] line2 = self.lines2[j-1] o = line1.orien pos_offset = self.global_offsets[i-1][j-1] len_ratio = max(line1.length / float(line2.length), line2.length / float(line1.length)) # calculate distance and offset if pos_offset: dist = abs(line1.pos[o] - line2.pos[o] - pos_offset[o]) offset = abs(line1.pos[1-o] - line2.pos[1-o] - pos_offset[1-o]) else: dist = abs(line1.pos[o] - line2.pos[o]) offset = abs(line1.pos[1-o] - line2.pos[1-o]) if len_ratio < self.LEN_RATIO_THRESH: # small penaly for large divergence return (dist + offset) * line1.count, self.PERFECT else: return self.NO_MATCH_COST, self.NO_MATCH # anything not the proper distance away is not considered if dist > self.dist_thresh: return self.NO_MATCH_COST, self.NO_MATCH if offset < self.offset_thresh and len_ratio < self.LEN_RATIO_THRESH: return 0, self.PERFECT line1_range = line1.length_range() line2_range = line2.length_range(offset=pos_offset[1-o]) if utils.range_contains(line1_range, line2_range): return self.contains_cost(line2, line1), self.CONTAINS2 if utils.range_contains(line2_range, line1_range): return self.contains_cost(line1, line2), self.CONTAINS1 if utils.are_overlapping([line1_range, line2_range]): return self.overlap_cost(line1, line2, pos_offset), self.OVERLAP # in the case of similarly spaced lines, slight bias toward COFRAG over DEL1/DEL2 pair return self.cofrag_cost(line1, line2), self.COFRAG
def connect1_cost(self, i, j): ''' Assumes line i and line j are labeled as overlapping by self.match_cost_type(i, j) :return: marginal cost of assuming that line i connects line j and line j-1 ''' if j == 1: return self.NO_MATCH_COST, self.NO_MATCH line1 = self.lines1[i-1] line21 = self.lines2[j-1] line22 = self.lines2[j-2] offset = self.global_offsets[i-1][j-2] if offset is None: offset = (0, 0) o = line1.orien r1 = line1.length_range() r21 = line21.length_range(offset=offset[1-o]) r22 = line22.length_range(offset=offset[1-o]) if self.are_colinear(line21, line22) and utils.are_overlapping([r1, r21, r22]): return self.connect_cost(line1, line21, line22, offset), self.CONNECT1 else: return self.NO_MATCH_COST, self.NO_MATCH
def connect2_cost(self, i, j): ''' Assumes line i and line j are labeled as overlapping by self.match_cost_type(i, j) :return: marginal cost of assuming that line j connects line i and line i-1 ''' if i == 1: return self.NO_MATCH_COST, self.NO_MATCH line2 = self.lines2[j-1] line11 = self.lines1[i-1] line12 = self.lines1[i-2] offset = self.global_offsets[i-2][j-1] if offset is None: offset = (0, 0) o = line2.orien r2 = line2.length_range(offset=offset[1-o]) r11 = line11.length_range() r12 = line12.length_range() if self.are_colinear(line11, line12) and utils.are_overlapping([r2, r11, r12]): return self.connect_cost(line2, line11, line12, utils.tup_scale(offset, -1)), self.CONNECT2 else: return self.NO_MATCH_COST, self.NO_MATCH
def test_are_overlapping(self): # Bounding box is represented by a list # in the form [ymin, xmin, ymax, xmax] self.assertTrue(are_overlapping([2, 1, 7, 6], [5, 4, 11, 8])) self.assertFalse(are_overlapping([2, 1, 7, 6], [8, 6, 13, 9]))