예제 #1
0
 def prepareConnection(self, circles, bounds=None):
     """
     :param circles: The circles in which a circle grid will be detected
     :type circles: list
     :param bounds: The boundaries of the image in which the circles have been detected
     :type bounds: list
     Method to call before self.connectCircles to set the object parameters properly
     """
     self._circles = circles
     for i in range(len(circles)):
         self._noise_circles.append(False)
     if bounds is None:
         tuple_max = geom.max_tuple(circles)
         tuple_min = geom.min_tuple(circles)
         # bounds = (int(tuple_min[1]), int(tuple_min[0]), int(tuple_max[1]) + 1, int(tuple_max[0]) + 1)
         bounds = (int(tuple_min[0]), int(tuple_min[1]), int(tuple_max[0]) + 1, int(tuple_max[1]) + 1)
     self._bounds = bounds
예제 #2
0
    def checkForGrid(self):
        """
        Checks for a grid inside a mapping done with bfsMarking.
        bfsMarking can detect multiple possible grids, this method will check if there is at least one possible grid.

        It will also decide, in the case of multiple possible grids, which grid is the good one by counting the
          number of vectors detected earlier inside the grid (the bigger amount of vectors, the better the grid)
        """
        (max_x, max_y) = geom.max_tuple(self._relative_coordinates.keys())
        if max_x + 1 < self._grid_shape[1] or max_y + 1 < self._grid_shape[0]:
            raise self._exception
        elif max_x + 1 != self._grid_shape[1] or max_y + 1 != self._grid_shape[0]:
            # Rectangle too big, need to consider inner _rectangles
            rectangles = geom.get_inner_rectangles([[(0, max_y), (max_x, max_y)], [(0, 0), (max_x, 0)]],
                                                   self._grid_shape[0], self._grid_shape[1])
            max_connection = -np.infty
            max_rectangle = None
            unsure = False
            for rectangle in rectangles:
                [[(_, _), (max_x, _)], [(_, min_y), (_, _)]] = rectangle
                # Count the number of connection inside the rectangle
                nb_connection = self.countRectangleConnections(rectangle)
                if nb_connection == max_connection:
                    unsure = True
                elif nb_connection > max_connection and nb_connection != -1:
                    unsure = False
                    max_connection = nb_connection
                    max_rectangle = rectangle
            # If two _rectangles could be a circle grid or no correct rectangle was found,
            #   then we decide to reject this analysis
            if unsure or max_rectangle is None:
                raise self._exception
            [[(min_x, max_y), (max_x, _)], [(_, min_y), (_, _)]] = max_rectangle
            # Returns the rectangle that has the more connection inside (filters the dict with the values of the rect)
            new_mapping = {(x, y): v for (x, y), v in self._relative_coordinates.iteritems()
                           if min_x <= x <= max_x and min_y <= y <= max_y}
            self._relative_coordinates = new_mapping
            self.normalizeRelativeCoordinates()