Exemple #1
0
    def __get_t_neighbors(self, point, dot_img, show_preview=False):
        height, width, channels = self.__img.shape
        neighbors = [point]

        left = point.copy().add(Point(-GRID_SIZE, 0))
        if left.inBounds(width, height) and (dot_img[left.y, left.x]
                                             == WALL_FLAG).all():
            neighbors.append(left)

        right = point.copy().add(Point(GRID_SIZE, 0))
        if right.inBounds(width, height) and (dot_img[right.y, right.x]
                                              == WALL_FLAG).all():
            neighbors.append(right)

        down = point.copy().add(Point(0, GRID_SIZE))
        if down.inBounds(width, height) and (dot_img[down.y, down.x]
                                             == WALL_FLAG).all():
            neighbors.append(down)

        up = point.copy().add(Point(0, -GRID_SIZE))
        if up.inBounds(width, height) and (dot_img[up.y, up.x]
                                           == WALL_FLAG).all():
            neighbors.append(up)

        if show_preview:
            preview = dot_img.copy()
            preview = self.__draw_points(neighbors, img=preview)
            self.show_image(1, fullscreen=True, img=preview)

        return neighbors
Exemple #2
0
    def __line_length_vertical(self, point, dot_img, show_preview=False):
        height, width, channels = self.__img.shape
        far_top_point = point.copy()
        while far_top_point.y - GRID_SIZE > 0 and (
                dot_img[far_top_point.y - GRID_SIZE,
                        far_top_point.x] == WALL_FLAG).all():
            far_top_point.add(Point(0, -GRID_SIZE))

        far_bottom_point = far_top_point.copy()
        count = 0
        while far_bottom_point.y + GRID_SIZE < height and (
                dot_img[far_bottom_point.y + GRID_SIZE,
                        far_bottom_point.x] == WALL_FLAG).all():
            count += 1
            far_bottom_point.add(Point(0, GRID_SIZE))

        if show_preview:
            preview = dot_img.copy()
            cv2.line(preview, far_top_point.asTuple(),
                     far_bottom_point.asTuple(), GREEN)
            cv2.rectangle(preview,
                          point.copy().add(Point(-1, -1)).asTuple(),
                          point.copy().add(Point(1, 1)).asTuple(), RED)
            preview = cv2.addWeighted(self.__img, 0.2, preview, 0.9, 0)
            self.show_image(1,
                            fullscreen=True,
                            img=preview,
                            text='Point %s' % point)

        return count
Exemple #3
0
    def __line_length_horizontal(self, point, dot_img, show_preview=False):
        height, width, channels = self.__img.shape
        far_left_point = point.copy()
        while far_left_point.x - GRID_SIZE > 0 and (
                dot_img[far_left_point.y,
                        far_left_point.x - GRID_SIZE] == WALL_FLAG).all():
            far_left_point.add(Point(-GRID_SIZE, 0))

        far_right_point = far_left_point.copy()
        count = 0
        while far_right_point.x + GRID_SIZE < width and (
                dot_img[far_right_point.y,
                        far_right_point.x + GRID_SIZE] == WALL_FLAG).all():
            count += 1
            far_right_point.add(Point(GRID_SIZE, 0))

        if show_preview:
            preview = dot_img.copy()
            cv2.line(preview, far_left_point.asTuple(),
                     far_right_point.asTuple(), GREEN)

            p1 = point.copy().add(Point(-1, -1)).asTuple()
            p2 = point.copy().add(Point(1, 1)).asTuple()
            cv2.rectangle(preview, p1, p2, RED)

            preview = cv2.addWeighted(self.__img, 0.2, preview, 0.9, 0)
            self.show_image(1,
                            fullscreen=True,
                            img=preview,
                            text='Point %s' % point)

        return count
Exemple #4
0
    def __get_x_neighbors(self, point, dot_img):
        height, width, channels = self.__img.shape
        neighbors = [point]

        top_left = point.copy().add(Point(-GRID_SIZE, -GRID_SIZE))
        if top_left.inBounds(width, height) and (dot_img[top_left.y,
                                                         top_left.x]
                                                 == WALL_FLAG).all():
            neighbors.append(top_left)

        top_right = point.copy().add(Point(GRID_SIZE, -GRID_SIZE))
        if top_right.inBounds(width, height) and (dot_img[top_right.y,
                                                          top_right.x]
                                                  == WALL_FLAG).all():
            neighbors.append(top_right)

        down_left = point.copy().add(Point(-GRID_SIZE, GRID_SIZE))
        if down_left.inBounds(width, height) and (dot_img[down_left.y,
                                                          down_left.x]
                                                  == WALL_FLAG).all():
            neighbors.append(down_left)

        down_right = point.copy().add(Point(GRID_SIZE, GRID_SIZE))
        if down_right.inBounds(width, height) and (dot_img[down_right.y,
                                                           down_right.x]
                                                   == WALL_FLAG).all():
            neighbors.append(down_right)

        return neighbors
Exemple #5
0
    def __draw_points(self, points, img, size=1):
        p1 = points[0].copy().add(Point(-size, -size)).asTuple()
        p2 = points[0].copy().add(Point(size, size)).asTuple()
        cv2.rectangle(img, p1, p2, RED)
        del points[0]

        for point in points:
            p1 = point.copy().add(Point(-size, -size)).asTuple()
            p2 = point.copy().add(Point(size, size)).asTuple()
            cv2.rectangle(img, p1, p2, YELLOW)
        return img
Exemple #6
0
    def __get_tx_neighbors(self, point, dot_img):
        height, width, channels = self.__img.shape
        neighbors = [point]

        left = point.copy().add(Point(-GRID_SIZE, 0))
        if left.inBounds(width, height) and (dot_img[left.y, left.x]
                                             == WALL_FLAG).all():
            neighbors.append(left)

        right = point.copy().add(Point(GRID_SIZE, 0))
        if right.inBounds(width, height) and (dot_img[right.y, right.x]
                                              == WALL_FLAG).all():
            neighbors.append(right)

        down = point.copy().add(Point(0, GRID_SIZE))
        if down.inBounds(width, height) and (dot_img[down.y, down.x]
                                             == WALL_FLAG).all():
            neighbors.append(down)

        up = point.copy().add(Point(0, -GRID_SIZE))
        if up.inBounds(width, height) and (dot_img[up.y, up.x]
                                           == WALL_FLAG).all():
            neighbors.append(up)

        top_left = point.copy().add(Point(-GRID_SIZE, -GRID_SIZE))
        if top_left.inBounds(width, height) and (dot_img[top_left.y,
                                                         top_left.x]
                                                 == WALL_FLAG).all():
            neighbors.append(top_left)

        top_right = point.copy().add(Point(GRID_SIZE, -GRID_SIZE))
        if top_right.inBounds(width, height) and (dot_img[top_right.y,
                                                          top_right.x]
                                                  == WALL_FLAG).all():
            neighbors.append(top_right)

        down_left = point.copy().add(Point(-GRID_SIZE, GRID_SIZE))
        if down_left.inBounds(width, height) and (dot_img[down_left.y,
                                                          down_left.x]
                                                  == WALL_FLAG).all():
            neighbors.append(down_left)

        down_right = point.copy().add(Point(GRID_SIZE, GRID_SIZE))
        if down_right.inBounds(width, height) and (dot_img[down_right.y,
                                                           down_right.x]
                                                   == WALL_FLAG).all():
            neighbors.append(down_right)

        return neighbors
Exemple #7
0
    def __get_square_neighbors(self, point, dot_img):
        height, width, channels = self.__img.shape
        neighbors = [point]

        right = point.copy().add(Point(GRID_SIZE, 0))
        if right.inBounds(width, height) and (dot_img[right.y, right.x]
                                              == WALL_FLAG).all():
            neighbors.append(right)

        down = point.copy().add(Point(0, GRID_SIZE))
        if down.inBounds(width, height) and (dot_img[down.y, down.x]
                                             == WALL_FLAG).all():
            neighbors.append(down)

        down_right = point.copy().add(Point(GRID_SIZE, GRID_SIZE))
        if down_right.inBounds(width, height) and (dot_img[down_right.y,
                                                           down_right.x]
                                                   == WALL_FLAG).all():
            neighbors.append(down_right)

        # print('Found point %s with %s neightbors' % (point, len(neighbors)))

        return neighbors
Exemple #8
0
    def __wall_builder(self, dot_img, show_preview=False):
        height, width, channels = self.__img.shape
        wall_img = dot_img.copy()
        wall_img[:] = WHITE
        for x in range(width - 1):
            for y in range(height - 1):
                if (dot_img[y, x] == WALL_FLAG).all():
                    point = Point(x, y)
                    neighbors = self.__get_t_neighbors(point, dot_img)
                    wall_img = self.__draw_t_lines(point, neighbors, wall_img)

                    if show_preview:
                        preview = cv2.addWeighted(self.__img, 0.5, dot_img,
                                                  0.5, 0)
                        preview = cv2.addWeighted(preview, 0.5, wall_img, 0.5,
                                                  0)
                        self.show_image(100, fullscreen=True, img=preview)
        return wall_img
Exemple #9
0
    def __four_corners(self, dot_img, show_preview=False):
        height, width, channels = self.__img.shape
        for x in range(width - 1):
            for y in range(height - 1):
                if (dot_img[y, x] == WALL_FLAG).all():
                    neighbors = self.__get_square_neighbors(
                        Point(x, y), dot_img)
                    if (len(neighbors) == 4):
                        dot_img = self.__kill_shorty(neighbors, self.__img,
                                                     dot_img)

                        with_removed = dot_img.copy()
                        with_removed[y, x] = GREEN

                        if show_preview:
                            preview = cv2.addWeighted(self.__img, 0.2,
                                                      with_removed, 0.9, 0)
                            self.show_image(20,
                                            fullscreen=True,
                                            img=preview,
                                            text='(%s, %s)' % (x, y))
        return dot_img
Exemple #10
0
    def get_perspective_transform(self, img_width, img_height):
        top_left_point = Point(0, 0)
        top_right_point = Point(img_width, 0)
        bottom_left_point = Point(0, img_height)
        bottom_right_point = Point(img_width, img_height)

        center = Point(round(img_width / 2), round(img_width / 2))

        top_left_ref = (top_left_point.distance(center), None)
        top_right_ref = (top_right_point.distance(center), None)
        bottom_left_ref = (bottom_left_point.distance(center), None)
        bottom_right_ref = (bottom_right_point.distance(center), None)

        for point in self.__points:
            top_left_dist = top_left_point.distance(point.src)
            top_right_dist = top_right_point.distance(point.src)
            bottom_left_dist = bottom_left_point.distance(point.src)
            bottom_right_dist = bottom_right_point.distance(point.src)

            if top_left_dist < top_left_ref[0]:
                top_left_ref = (top_left_dist, point)
            if top_right_dist < top_right_ref[0]:
                top_right_ref = (top_right_dist, point)
            if bottom_left_dist < bottom_left_ref[0]:
                bottom_left_ref = (bottom_left_dist, point)
            if bottom_right_dist < bottom_right_ref[0]:
                bottom_right_ref = (bottom_right_dist, point)

        src = np.float32(
            [[top_left_ref[1].src.x, top_left_ref[1].src.y],
             [top_right_ref[1].src.x, top_right_ref[1].src.y],
             [bottom_left_ref[1].src.x, bottom_left_ref[1].src.y],
             [bottom_right_ref[1].src.x, bottom_right_ref[1].src.y]])

        dst = np.float32(
            [[top_left_ref[1].dst.x, top_left_ref[1].dst.y],
             [top_right_ref[1].dst.x, top_right_ref[1].dst.y],
             [bottom_left_ref[1].dst.x, bottom_left_ref[1].dst.y],
             [bottom_right_ref[1].dst.x, bottom_right_ref[1].dst.y]])

        matrix = cv2.getPerspectiveTransform(src, dst)

        print('Transfrom based on %s to %s\n%s' % (src, dst, matrix))

        return matrix
Exemple #11
0
 def __init__(self, grid_size, grid_offset):
     self.__points = []
     self.__grid_size = grid_size
     self.__grid_offset = grid_offset
     self.top_left = Point(99999, 99999)
     self.bottom_right = Point(0, 0)
Exemple #12
0
    def __erode(self, dot_img, show_preview=False):
        height, width, channels = self.__img.shape
        for x in range(width - 1):
            for y in range(height - 1):
                if (dot_img[y, x] == WALL_FLAG).all():
                    point = Point(x, y)
                    neighbors = self.__get_t_neighbors(point, dot_img)

                    if len(neighbors) == 1:
                        dot_img[y, x] = WHITE

                        if show_preview:
                            preview = dot_img.copy()
                            p1 = point.copy().add(Point(-1, -1)).asTuple()
                            p2 = point.copy().add(Point(1, 1)).asTuple()
                            cv2.rectangle(preview, p1, p2, RED)
                            self.show_image(1000,
                                            fullscreen=True,
                                            img=preview,
                                            text='Removing island point %s' %
                                            point)

                    elif len(neighbors) == 2:
                        is_on_wall = (self.__img[y, x] == BLACK).all()
                        if not is_on_wall:
                            neighbor = neighbors[1]
                            if self.__is_corner(neighbor, dot_img):
                                dot_img[y, x] = WHITE
                            elif self.__line_lengths(
                                    point, dot_img) < RELEVANT_LINE_LENGTH:
                                dot_img[y, x] = WHITE

                        if show_preview:
                            preview = dot_img.copy()
                            p1 = point.copy().add(Point(-1, -1)).asTuple()
                            p2 = point.copy().add(Point(1, 1)).asTuple()
                            cv2.rectangle(preview, p1, p2, ORANGE)
                            self.show_image(1000,
                                            fullscreen=True,
                                            img=preview,
                                            text='Outlier %s' % point)
                    else:
                        if show_preview:
                            preview = dot_img.copy()
                            p1 = point.copy().add(Point(-1, -1)).asTuple()
                            p2 = point.copy().add(Point(1, 1)).asTuple()
                            cv2.rectangle(preview, p1, p2, GREEN)
                            self.show_image(
                                1,
                                fullscreen=True,
                                img=preview,
                                text='Point %s passed erosion with %s neighbors'
                                % (point, len(neighbors)))
        return dot_img