def _findHamcodesRectangles(self): hamcodes_id_to_rect_centres = {} for hamcode in self._hamcodes: hamcode.contours = geom.sort_rectangle_corners(hamcode.contours) ham_long_vector = geom.vectorize(hamcode.contours[0], hamcode.contours[1]) for rect_centre in self._filtered_rectangle_centres: ((long_vector, long_norm), (_, _)) = geom.get_box_info(self._boxes[self._centres_to_indices[rect_centre]]) if geom.are_vectors_parallel(long_vector, ham_long_vector, 0.25): hamcode_vector = geom.vectorize(hamcode.contours[0], hamcode.contours[1]) centers_vector = geom.vectorize(hamcode.center, rect_centre) model_ratio = (self._model.hamcode_v_margin + (self._model.hamcode_side / 2.) - self._model.hole_v_margin - (self._model.hole_width / 2.)) / self._model.hamcode_side image_ratio = np.linalg.norm(centers_vector) / np.linalg.norm(hamcode_vector) if geom.are_ratio_similar(image_ratio, model_ratio, 1.3): hamcodes_id_to_rect_centres[hamcode.id] = rect_centre return hamcodes_id_to_rect_centres
def _filterOtherRectangles(self): filtered_rectangle_centres = [] contains_map = {} # The ratio between the hole length + the horizontal space and the hole length model_hole_space_ratio = (self._model.hole_length + self._model.hole_h_space) / self._model.hole_length model_length_width_ratio = (self._model.hole_length / self._model.hole_width) for centre in self._filtered_rectangle_centres: if not contains_map.get(centre, False): box = self._boxes[self._centres_to_indices[centre]] ((box_vector, box_length), (_, box_width)) = geom.get_box_info(box) max_distance = 0.5 * box_length # If the rectangle is not too small and the length/width ratio is the ratio expected by the _model if box_length > 30 and geom.are_ratio_similar(box_length / box_width, model_length_width_ratio, 1.75): for other_centre in self._filtered_rectangle_centres: if other_centre is not centre: other_box = self._boxes[self._centres_to_indices[other_centre]] ((other_box_vector, other_box_length),(_, other_box_width)) = geom.get_box_info(other_box) centres_vector = geom.vectorize(centre, other_centre) # If the other rectangle is not too small and the length/width ratio # is the ratio expected by the _model # and : the two rectangle have the same length # and : the distance between the _rectangles is the distance expected by the _model # and : the vector of the long_side of the rectangle and the vector that binds the # two centres is approximately parallel if other_box_length > 30 \ and geom.are_ratio_similar(other_box_length / other_box_width, model_length_width_ratio, 1.75) \ and geom.are_vectors_similar(box_vector, other_box_vector, max_distance, signed=False) \ and geom.are_ratio_similar(np.linalg.norm(centres_vector) / box_length, model_hole_space_ratio, 0.25) \ and geom.are_vectors_parallel(box_vector, centres_vector, 0.4): if not contains_map.get(centre, False): filtered_rectangle_centres.append(centre) contains_map[centre] = True if not contains_map.get(other_centre, False): filtered_rectangle_centres.append(other_centre) contains_map[other_centre] = True return filtered_rectangle_centres