def test_lemme_6_case_1_k_0_NWp_and_NNWp_white_but_NNWWp_black_and_NWW_white(
            self):
        img_depart = [[0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0,
                                           0], [0, 1, 0, 0, 0, 0],
                      [0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0,
                                           0], [0, 1, 0, 1, 1, 0],
                      [0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(
            img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)
        vert = BinaryImage.create_img_vertical(binary_image.size,
                                               BLACK_CONNEXITY,
                                               WHITE_CONNEXITY)

        solver = B4W4_Solver(binary_image, vert)
        solver.lemme_6(solver.imageElementsStart)

        self.assertEqual(
            img_soluce, binary_image.convert_pixels_to_img(),
            "In case of lemma_6, if the lead elbow = p is not cut vertex and k=0 and NW(p) is black"
            "then the interchange is <p, N(p)>")
        self.assertEqual(2, len(solver.imageElementsStart.array_interchange),
                         "The number interchange should be 2")
    def test_lemme_6_p_cut_into_not_cut(self):
        img_depart = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0],
                      [0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 0, 0, 0],
                      [0, 0, 0, 1, 0, 0, 1, 0], [0, 0, 0, 1, 1, 1, 1, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0],
                      [0, 1, 0, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0, 0],
                      [0, 0, 0, 1, 0, 1, 1, 0], [0, 0, 0, 1, 1, 1, 1, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(
            img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)
        vert = BinaryImage.create_img_vertical(binary_image.size,
                                               BLACK_CONNEXITY,
                                               WHITE_CONNEXITY)

        solver = B4W4_Solver(binary_image, vert)
        solver.lemme_6(solver.imageElementsStart)

        self.assertEqual(
            img_soluce, binary_image.convert_pixels_to_img(),
            "In case of lemma_6, if the lead elbow = p is cut vertex we're applying a k-diag interchange"
            "on p. In this case p should not be cut vertex at the end of the interchanges"
        )
        self.assertEqual(2, len(solver.imageElementsStart.array_interchange),
                         "The number interchange should be 2")
    def test_p_cut_Wp_white_not_cut(self):
        img_depart = [[0, 0, 0, 0, 0, 0],
                      [0, 1, 1, 1, 1, 0],
                      [0, 0, 0, 0, 1, 0],
                      [0, 1, 1, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0],
                      [0, 1, 1, 1, 1, 0],
                      [0, 0, 0, 1, 0, 0],
                      [0, 1, 1, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)

        solver = B8W4_Solver(binary_image, BinaryImage.create_img_vertical(binary_image.size))

        solver.resolve_image(binary_image)

        self.assertEqual(img_soluce,
                         binary_image.convert_pixels_to_img(),
                         "In case of p cut vertex and W(p) white not cut,"
                         " the interchange should be <p, W(p)>")
        self.assertEqual(1,
                         len(solver.array_interchange),
                         "The number interchange should be 1")
    def test_first_condition_cut_NWp1_cut(self):
        img_depart = [[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0],
                      [0, 1, 0, 0, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0],
                      [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0],
                      [0, 0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0],
                      [0, 1, 0, 1, 0, 0, 0], [0, 1, 1, 1, 0, 1, 0],
                      [0, 0, 0, 1, 0, 1, 0], [0, 0, 0, 1, 1, 1, 0],
                      [0, 0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(
            img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)
        vert = BinaryImage.create_img_vertical(binary_image.size,
                                               BLACK_CONNEXITY,
                                               WHITE_CONNEXITY)

        solver = B4W4_Solver(binary_image, vert)

        solver.first_condition(solver.imageElementsStart)

        self.assertEqual(
            img_soluce, binary_image.convert_pixels_to_img(),
            "In case of NNW(t) black and NW(t) white you need to apply a k-vertical interchange on NNW(t)."
            "Here it's a 3 times a 1-diagonal interchange where p1 is cut vertex the first time"
            "then not cut vertex twice. Note : the top pixel change once after the second interchange"
        )
        self.assertEqual(3, len(solver.imageElementsStart.array_interchange),
                         "The number interchange should be 3")
    def test_p_cut_Wp_white_cut_G_through_Np_Sl_cut_SSWl_not_cut(self):
        img_depart = [[0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 1, 1, 0],
                      [0, 1, 0, 0, 1, 0],
                      [0, 0, 1, 0, 1, 0],
                      [0, 0, 0, 0, 1, 0],
                      [0, 0, 0, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 1, 1, 0],
                      [0, 1, 0, 1, 1, 0],
                      [0, 0, 0, 0, 1, 0],
                      [0, 0, 0, 0, 1, 0],
                      [0, 0, 0, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)

        solver = B8W4_Solver(binary_image, BinaryImage.create_img_vertical(binary_image.size))

        solver.resolve_image(binary_image)

        self.assertEqual(img_soluce,
                         binary_image.convert_pixels_to_img(),
                         "In case of p cut vertex and W(p) white cut, g goes to p through N(p)"
                         "and l is cut vertex but SSW(l) is not cut the interchange should be <SSW(l), S(l)>")
        self.assertEqual(1,
                         len(solver.array_interchange),
                         "The number interchange should be 1")
Esempio n. 6
0
    def test_p_cut_g_to_p_from_Np_NNWWp_white(self):
        img_depart = [[0, 0, 0, 0, 0, 0],
                      [0, 1, 1, 1, 1, 0],
                      [0, 1, 0, 1, 1, 0],
                      [0, 1, 1, 0, 1, 0],
                      [0, 0, 0, 1, 1, 0],
                      [0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0, 0],
                      [0, 1, 1, 1, 1, 0, 0],
                      [0, 1, 0, 1, 1, 1, 0],
                      [0, 1, 0, 1, 0, 0, 0],
                      [0, 0, 0, 1, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)

        solver = B4W8_Solver(binary_image, BinaryImage.create_img_vertical(binary_image.size))

        solver.resolve_image(binary_image)

        self.assertEqual(img_soluce,
                         binary_image.convert_pixels_to_img(),
                         "In case of p cut vertex and g=NWW(p) black and path from g to p is through N(p) "
                         "and NNW(p) is black and NNWW(p) is white, "
                         "the interchanges should be <g, E(g)> and <N(p), NNE(p)>")
        self.assertEqual(2,
                         len(solver.array_interchange),
                         "The number interchange should be 2")
Esempio n. 7
0
    def test_fct_image_expand(self):
        binary_image = BinaryImage.create_img_from_array(
            IMG_EXPAND_1, BLACK_CONNEXITY, WHITE_CONNEXITY)
        self.assertEqual(
            8, len(binary_image.white_pixels),
            "[[1]] image should create 8 white pixels around the black pixel")
        self.assertEqual(
            1, len(binary_image.black_pixels),
            "[[1]] should keep exactly 1 black pixel in its image")
        self.assertEqual(
            binary_image.get_pixel(1, 1).color, PixelColor.BLACK,
            "Pixel(1,1) should be black")

        binary_image = BinaryImage.create_img_from_array(
            IMG_EXPAND_2, BLACK_CONNEXITY, WHITE_CONNEXITY)
        self.assertEqual(
            16, len(binary_image.white_pixels), "[[1, 1, 1],\
                           [0, 1, 0]] image should create 14 white pixels around the black (total = 16 because "
            "already 2 white pixels in image)")
        self.assertEqual(
            4, len(binary_image.black_pixels), "[[1, 1, 1],\
                           [0, 1, 0]] should keep exactly 4 black pixel in its image"
        )
        self.assertEqual(
            binary_image.get_pixel(2, 1).color, PixelColor.BLACK,
            "Pixel(2,1) should be black")
        self.assertEqual(
            binary_image.get_pixel(2, 2).color, PixelColor.BLACK,
            "Pixel(2,2) should be black")
        self.assertEqual(
            binary_image.get_pixel(2, 3).color, PixelColor.BLACK,
            "Pixel(2,3) should be black")
        self.assertEqual(
            binary_image.get_pixel(1, 2).color, PixelColor.BLACK,
            "Pixel(1,2) should be black")
    def compute_l(self, p: Pixel, binary_image: BinaryImage) -> Pixel:
        w_n = binary_image.get_pixel_directional(p, [Direction.W, Direction.N])
        current_pixel = binary_image.get_pixel_adjacent(w_n, Direction.N)

        while current_pixel.color != PixelColor.BLACK:
            current_pixel = binary_image.get_pixel_adjacent(
                current_pixel, Direction.N)

        return current_pixel
    def test_compute_height(self):
        binary_image = BinaryImage.create_img_from_array(
            IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = B4W4_Elements(binary_image)

        self.assertEqual(2, algo.height, "height should be -1")

        binary_image = BinaryImage.create_img_from_array(
            IMG_TEST_CASE_1, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = B4W4_Elements(binary_image)
        self.assertEqual(None, algo.height,
                         "height should be 0 in vertical image")
    def compute_h(self, g: Pixel,
                  binary_image: BinaryImage) -> (Pixel, [Pixel]):
        pixels_between = [g]
        current_pixel = g

        while binary_image.get_pixel_adjacent(
                current_pixel, Direction.N).color != PixelColor.WHITE:
            current_pixel = binary_image.get_pixel_adjacent(
                current_pixel, Direction.N)
            pixels_between.append(current_pixel)

        return current_pixel, pixels_between
Esempio n. 11
0
    def statistics_from_random_images(self,
                                      black_connexity=4,
                                      white_connexity=4,
                                      nb_iter=10,
                                      initial_size_image=50,
                                      nb_tick=5,
                                      incr_size_image=10,
                                      seed_min=0,
                                      seed_max=500000):
        size_image = initial_size_image
        for i in range(nb_iter):
            total_interchange = 0
            seeds = []
            for j in range(nb_tick):
                seed = random.randint(seed_min, seed_max)
                binary_image = BinaryImage.create_random_img(
                    n=size_image,
                    black_connexity=black_connexity,
                    white_connexity=white_connexity,
                    seed=seed)
                solver = None

                if black_connexity == white_connexity == 4:
                    solver = B4W4_Solver(
                        binary_image,
                        BinaryImage.create_img_vertical(binary_image.size))
                elif black_connexity == 4 and white_connexity == 8:
                    solver = B4W8_Solver(
                        binary_image,
                        BinaryImage.create_img_vertical(binary_image.size))
                elif black_connexity == 8 and white_connexity == 4:
                    solver = B8W4_Solver(
                        binary_image,
                        BinaryImage.create_img_vertical(binary_image.size))
                elif black_connexity == white_connexity == 8:
                    solver = B8W8_Solver(
                        binary_image,
                        BinaryImage.create_img_vertical(binary_image.size))

                nb_interchange = solver.solve()
                total_interchange += nb_interchange
                seeds.append(seed)

            average_tick_interchange = total_interchange / nb_tick

            self.dic_interchanges[size_image] = [
                seeds, average_tick_interchange
            ]
            size_image += incr_size_image

        print("Total average for solver B", black_connexity, "W",
              white_connexity, " is ", self.dic_interchanges)
Esempio n. 12
0
    def test_compute_anchor(self):
        binary_image = BinaryImage.create_img_from_array(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = B4W4_Elements(binary_image)

        self.assertEqual(algo.binary_image.get_pixel(2, 4),
                         algo.compute_anchor(),
                         "Anchor should be (2, 4)")

        binary_image = BinaryImage.create_img_from_array(IMG_TEST_CASE_1, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = B4W4_Elements(binary_image)
        self.assertEqual(None,
                         algo.compute_anchor(),
                         "Anchor should be None on vertical image")
Esempio n. 13
0
    def test_number_elements_img(self):
        binary_image = BinaryImage.create_random_img(SIZE_IMAGE,
                                                     BLACK_CONNEXITY,
                                                     WHITE_CONNEXITY, SEED)
        total_pixels = binary_image.width * binary_image.height
        self.assertEqual(
            total_pixels,
            len(binary_image.black_pixels) + len(binary_image.white_pixels))

        binary_image = BinaryImage.create_img_from_array(
            IMG_TEST_CASE_1, BLACK_CONNEXITY, WHITE_CONNEXITY)
        self.assertEqual(
            9,
            len(binary_image.black_pixels) + len(binary_image.white_pixels))
Esempio n. 14
0
    def test_compute_top_pixel(self):
        binary_image = BinaryImage.create_img_from_array(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = B4W4_Elements(binary_image)

        self.assertEqual(algo.binary_image.get_pixel(5, 5),
                         algo.compute_top_pixel(),
                         "Anchor should be (5, 5)")
Esempio n. 15
0
    def test_first_condition_cut_NWp1_not_cut(self):
        img_depart = [[0, 0, 0, 0, 0, 0],
                      [0, 1, 0, 0, 0, 0],
                      [0, 1, 0, 1, 0, 0],
                      [0, 1, 1, 1, 0, 0],
                      [0, 1, 0, 0, 0, 0],
                      [0, 1, 1, 1, 1, 0],
                      [0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0],
                      [0, 1, 0, 0, 0, 0],
                      [0, 1, 1, 1, 0, 0],
                      [0, 1, 1, 0, 0, 0],
                      [0, 1, 0, 0, 0, 0],
                      [0, 1, 1, 1, 1, 0],
                      [0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)

        solver = B4W4_Elements(binary_image)

        solver.first_condition()

        self.assertEqual(img_soluce,
                         binary_image.convert_pixels_to_img(),
                         "In case of NNW(t) black and NW(t) white you need to apply a k-vertical interchange on NNW(t)."
                         "Here it's a 1-diagonal interchange where p1 is cut vertex.")
        self.assertEqual(1,
                         len(solver.array_interchange),
                         "The number interchange should be 1")
Esempio n. 16
0
    def test_lemme_6_p_cut_into_white(self):
        img_depart = [[0, 0, 0, 0, 0],
                      [0, 1, 0, 0, 0],
                      [0, 1, 0, 1, 0],
                      [0, 1, 1, 1, 0],
                      [0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0],
                      [0, 1, 0, 0, 0],
                      [0, 1, 1, 1, 0],
                      [0, 1, 1, 0, 0],
                      [0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)

        solver = B4W4_Elements(binary_image)

        solver.lemme_6()

        self.assertEqual(img_soluce,
                         binary_image.convert_pixels_to_img(),
                         "In case of lemma_6, if the lead elbow = p is cut vertex we're applying a k-diag interchange"
                         "on p. In this case p should become white")
        self.assertEqual(1,
                         len(solver.array_interchange),
                         "The number interchange should be 1")
Esempio n. 17
0
    def test_lemme_6_case_1_k_superior_0(self):
        img_depart = [[0, 0, 0, 0],
                      [0, 0, 1, 0],
                      [0, 0, 1, 0],
                      [0, 0, 1, 0],
                      [0, 1, 1, 0],
                      [0, 1, 1, 0],
                      [0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0],
                      [0, 0, 1, 0],
                      [0, 0, 1, 0],
                      [0, 0, 1, 0],
                      [0, 0, 1, 0],
                      [0, 1, 1, 0],
                      [0, 1, 0, 0],
                      [0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)

        solver = B4W4_Elements(binary_image)

        solver.lemme_6()

        self.assertEqual(img_soluce,
                         binary_image.convert_pixels_to_img(),
                         "In case of lemma_6, if the lead elbow = p is not cut vertex and k>0 we'll slide p upward the"
                         "east side of the frontier. The interchanges are is <p, NE(p)>, <NiE(p), Ni+1E(p)>"
                         "<NkE(p), Nk+1E(p)>")
        self.assertEqual(5,
                         len(solver.array_interchange),
                         "The number interchange should be 5")
Esempio n. 18
0
    def find_path(g: Pixel,
                  p: Pixel,
                  img: BinaryImage,
                  connexity=4,
                  pixel_bridge=None) -> (bool, bool):
        visited = [False] * len(img.black_pixels)

        queue = [g]

        visited[img.black_pixels.index(g)] = True

        pixel_bridge_bool = False

        while queue:
            n = queue.pop(0)

            if pixel_bridge is not None and n == pixel_bridge:
                pixel_bridge_bool = True

            if n == p:
                return True, pixel_bridge_bool

            ajd_pixels = img.get_neighbours(n,
                                            connexity,
                                            color=PixelColor.BLACK)
            for i in ajd_pixels:
                if not visited[img.black_pixels.index(i)]:
                    if i not in queue:
                        queue.append(i)
                    visited[img.black_pixels.index(n)] = True

        print(visited)

        return False, pixel_bridge_bool
Esempio n. 19
0
    def test_is_elbow(self):
        binary_image = BinaryImage.create_img_from_array(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = B4W4_Elements(binary_image)

        for pixel in algo.binary_image.get_black_pixels():
            self.assertEqual(pixel in algo.compute_all_elbows(),
                             algo.is_elbow(pixel))
    def direction_clear(p: Pixel,
                        direction: Direction,
                        img: BinaryImage,
                        p_included=False) -> bool:
        if p_included:
            current_pixel = p
        else:
            current_pixel = img.get_pixel_adjacent(p, direction)

        while 0 < current_pixel.x < img.height and 0 < current_pixel.y < img.width:
            if current_pixel.color != PixelColor.WHITE:
                return False
            else:
                current_pixel = img.get_pixel_adjacent(current_pixel,
                                                       direction)

        return True
Esempio n. 21
0
    def test_get_neighbours(self):
        binary_image = BinaryImage.create_img_from_array(
            IMG_B4W4, BLACK_CONNEXITY, WHITE_CONNEXITY)
        neighbours = binary_image.get_neighbours(binary_image.get_pixel(1, 1),
                                                 8)

        # Both pixel verification
        verify_neighbours = binary_image.get_pixel(0, 0) in neighbours \
                          and binary_image.get_pixel(0, 1) in neighbours \
                          and binary_image.get_pixel(0, 2) in neighbours \
                          and binary_image.get_pixel(1, 0) in neighbours \
                          and binary_image.get_pixel(1, 2) in neighbours \
                          and binary_image.get_pixel(2, 0) in neighbours \
                          and binary_image.get_pixel(2, 1) in neighbours \
                          and binary_image.get_pixel(2, 2) in neighbours \

        self.assertEqual(
            True, verify_neighbours,
            "8-neighbours with black and white pixels not working ")

        # white pixels verification
        neighbours = binary_image.get_neighbours(binary_image.get_pixel(1, 1),
                                                 4, PixelColor.WHITE)
        verify_neighbours = binary_image.get_pixel(1, 0) in neighbours \
                          and binary_image.get_pixel(0, 1) in neighbours

        self.assertEqual(True, verify_neighbours,
                         "4-neighbours with white pixels only not working")

        not_neighbours = binary_image.get_pixel(0, 2) in neighbours and binary_image.get_pixel(0, 0) in neighbours \
                        and binary_image.get_pixel(1, 0) in neighbours \
                        and binary_image.get_pixel(1, 2) in neighbours \
                        and binary_image.get_pixel(2, 0) in neighbours \
                        and binary_image.get_pixel(2, 1) in neighbours \
                        and binary_image.get_pixel(2, 2) in neighbours \

        self.assertEqual(False, not_neighbours,
                         "4-neighbours with white pixels only not working")

        # black pixels verification
        neighbours = binary_image.get_neighbours(binary_image.get_pixel(1, 1),
                                                 4, PixelColor.BLACK)
        verify_neighbours = binary_image.get_pixel(1, 2) in neighbours \
                          and binary_image.get_pixel(2, 1) in neighbours

        self.assertEqual(True, verify_neighbours,
                         "4-neighbours with white pixels only not working")

        not_neighbours = binary_image.get_pixel(0, 2) in neighbours \
                        and binary_image.get_pixel(0, 0) in neighbours \
                        and binary_image.get_pixel(1, 0) in neighbours \
                        and binary_image.get_pixel(1, 0) in neighbours \
                        and binary_image.get_pixel(2, 0) in neighbours \
                        and binary_image.get_pixel(0, 1) in neighbours \
                        and binary_image.get_pixel(2, 2) in neighbours

        self.assertEqual(False, not_neighbours,
                         "4-neighbours with white pixels only not working")
Esempio n. 22
0
    def get_p(img: BinaryImage) -> Pixel:
        p = None

        for pix in img.black_pixels:
            s = img.get_pixel_directional(pix, [Direction.S])
            if s.color == PixelColor.WHITE:
                if p is None or pix.x >= p.x:
                    east_clear = B4W8_Solver.direction_clear(
                        pix, Direction.E, img)
                    north_clear = B4W8_Solver.direction_clear(
                        img.get_pixel_adjacent(pix, Direction.E),
                        Direction.N,
                        img,
                        p_included=True)

                    if east_clear and north_clear:
                        p = pix
        return p
Esempio n. 23
0
    def statistics_spiral(self,
                          black_connexity=4,
                          white_connexity=4,
                          nb_iter=10,
                          initial_size_image=50,
                          nb_tick=5,
                          incr_size_image=10):
        size_image = initial_size_image
        for i in range(nb_iter):
            total_interchange = 0
            for j in range(nb_tick):
                begin = time.time()
                binary_image = BinaryImage.create_img_spiral(
                    size_image=size_image,
                    black_connexity=black_connexity,
                    white_connexity=white_connexity)
                solver = None

                if black_connexity == white_connexity == 4:
                    solver = B4W4_Solver(
                        binary_image,
                        BinaryImage.create_img_vertical(binary_image.size))
                elif black_connexity == 4 and white_connexity == 8:
                    solver = B4W8_Solver(
                        binary_image,
                        BinaryImage.create_img_vertical(binary_image.size))
                elif black_connexity == 8 and white_connexity == 4:
                    solver = B8W4_Solver(
                        binary_image,
                        BinaryImage.create_img_vertical(binary_image.size))
                elif black_connexity == white_connexity == 8:
                    solver = B8W8_Solver(
                        binary_image,
                        BinaryImage.create_img_vertical(binary_image.size))

                nb_interchange = solver.solve()
                total_interchange += nb_interchange
                print("size : ", size_image, ", timer : ", time.time() - begin)

            self.dic_interchanges[size_image] = [i, total_interchange]
            size_image += incr_size_image

        print("Total average for solver B", black_connexity, "W",
              white_connexity, " is ", self.dic_interchanges)
Esempio n. 24
0
    def create_gif(image: BinaryImage,
                   array_interchage: [(int, int), (int, int)],
                   name="GifInterchange.gif",
                   speed=2000) -> None:

        fig, ax = pyplot.subplots()

        M = image.convert_pixels_to_img()
        matrice = ax.matshow(M, cmap='Greys')
        # width = image.width
        # height = image.height

        p_bar = tqdm(total=len(array_interchage))  # progress bar in print
        p_bar.set_description(
            "Creating gif nb_interchange n = " +
            str(len(array_interchage)))  # Description for the loading bar

        def init():

            # Creates the lines of the grid
            for i in range(image.height + 1):
                BinaryImageDisplayer.__draw_line(-0.5, image.width - 0.5,
                                                 i - 0.5, i - 0.5)

            # Creates the columns of the grid
            for i in range(image.width + 1):
                BinaryImageDisplayer.__draw_line(i - 0.5, i - 0.5, -0.5,
                                                 image.height - 0.5)

        def update(i):
            p, q = array_interchage[i - 1]
            swap = image.swap_pixels(p, q)

            if swap:
                p_bar.update(1)  # Adding the first pixel

                M = image.convert_pixels_to_img()
                matrice = ax.matshow(M, cmap='Greys')

                matrice.set_array(image.convert_pixels_to_img())

        ani = FuncAnimation(fig,
                            update,
                            frames=len(array_interchage) + 1,
                            interval=speed)
        ani.save(name)

        #
        #
        # def update():
        #     for p, q in array_interchage:
        #         image.swap_pixels(p, q)
        #         BinaryImageDisplayer.__draw_pixel(x=p.x, y=p.y, color="#FFFFFF")
        #         BinaryImageDisplayer.__draw_pixel(x=q.x, y=q.y, color=BinaryImageDisplayer.BLACK_PIXEL_COLOR)

        return None
Esempio n. 25
0
    def test_get_pixel(self):
        binary_image = BinaryImage.create_img_from_array(
            IMG_B4W4, BLACK_CONNEXITY, WHITE_CONNEXITY)
        height = random.randint(0, binary_image.height - 1)
        width = random.randint(0, binary_image.width - 1)

        pixel = binary_image.get_pixel(height, width)
        self.assertEqual(
            True, pixel.x == height and pixel.y == width,
            "GetPixel coordinates (" + str(height) + ", " + str(width) + ")")
    def test_p_cut_Wp_white_cut_G_through_Np_Sl_cut_SSWl_cut_z_no_exist_and_path_through_SWg(self):
        img_depart = [[0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 1, 1, 0, 0],
                      [0, 0, 1, 0, 0, 1, 0],
                      [0, 0, 0, 1, 0, 1, 0],
                      [0, 0, 1, 0, 0, 1, 0],
                      [0, 1, 0, 0, 0, 1, 0],
                      [0, 1, 0, 1, 0, 1, 0],
                      [0, 1, 0, 1, 0, 1, 0],
                      [0, 0, 1, 0, 0, 1, 0],
                      [0, 0, 0, 0, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 1, 1, 0, 0],
                      [0, 0, 1, 0, 0, 1, 0],
                      [0, 0, 0, 1, 0, 1, 0],
                      [0, 0, 1, 0, 0, 1, 0],
                      [0, 1, 0, 0, 0, 1, 0],
                      [0, 1, 0, 1, 1, 1, 0],
                      [0, 1, 0, 0, 0, 1, 0],
                      [0, 0, 1, 0, 0, 1, 0],
                      [0, 0, 0, 0, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)

        solver = B8W4_Solver(binary_image, BinaryImage.create_img_vertical(binary_image.size))

        solver.resolve_image(binary_image)

        self.assertEqual(img_soluce,
                         binary_image.convert_pixels_to_img(),
                         "In case of p cut vertex and W(p) white cut, g goes to p through N(p)"
                         "and l is cut vertex and SSW(l) is cut. all z exist such as w(z) is white"
                         "if g != h and path is through SW(g) then the interchange is"
                         "<g, NE(g)>")
        self.assertEqual(1,
                         len(solver.array_interchange),
                         "The number interchange should be 1")
    def compute_z(self, array_pixel: [Pixel],
                  binary_image: BinaryImage) -> Pixel or None:
        current_pixel = None
        min_x = float("inf")
        for b in array_pixel:
            if binary_image.get_pixel_adjacent(
                    b, Direction.W).color == PixelColor.BLACK:
                if b.x < min_x:
                    current_pixel = b

        return current_pixel
    def test_p_cut_Wp_white_cut_G_through_Np_Sl_cut_SSWl_cut_z_exist_Nz_or_NNz_black_and_z_in_south_wssssl(self):
        img_depart = [[0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 1, 1, 0],
                      [0, 1, 0, 0, 1, 0],
                      [0, 0, 1, 0, 1, 0],
                      [0, 1, 0, 0, 1, 0],
                      [0, 1, 1, 0, 1, 0],
                      [0, 1, 1, 0, 1, 0],
                      [0, 0, 1, 0, 1, 0],
                      [0, 0, 0, 0, 1, 0],
                      [0, 0, 0, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0],
                      [0, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 0, 0, 1, 0],
                      [0, 1, 0, 1, 0, 1, 0],
                      [0, 1, 0, 0, 0, 1, 0],
                      [0, 1, 1, 0, 1, 0, 0],
                      [0, 0, 1, 0, 1, 0, 0],
                      [0, 0, 0, 0, 1, 0, 0],
                      [0, 0, 0, 1, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)

        solver = B8W4_Solver(binary_image, BinaryImage.create_img_vertical(binary_image.size))

        solver.resolve_image(binary_image)

        self.assertEqual(img_soluce,
                         binary_image.convert_pixels_to_img(),
                         "In case of p cut vertex and W(p) white cut, g goes to p through N(p)"
                         "and l is cut vertex and SSW(l) is cut. z exist such as w(z) is black and N(z) or NN(z) is "
                         "black. If z == wssss+(l) the 4-vertical interchange should be "
                         "<EE(z), EEE(z)>, <NEE(z), ZEEE(z), <NNEE(z), NNEEE(z)>, <z, NE(z)>")
        self.assertEqual(4,
                         len(solver.array_interchange),
                         "The number interchange should be 4")
    def test_p_cut_Wp_white_cut_G_through_Np_Sl_cut_SSWl_cut_z_exist_Nz_and_NNz_white(self):
        img_depart = [[0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 1, 1, 0],
                      [0, 1, 0, 0, 1, 0],
                      [0, 0, 1, 0, 1, 0],
                      [0, 1, 0, 0, 1, 0],
                      [0, 1, 0, 0, 1, 0],
                      [0, 1, 1, 0, 1, 0],
                      [0, 0, 1, 0, 1, 0],
                      [0, 0, 0, 0, 1, 0],
                      [0, 0, 0, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0]]

        img_soluce = [[0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 1, 1, 0],
                      [0, 1, 0, 0, 1, 0],
                      [0, 0, 1, 0, 1, 0],
                      [0, 1, 0, 0, 1, 0],
                      [0, 1, 0, 1, 1, 0],
                      [0, 1, 0, 0, 1, 0],
                      [0, 0, 1, 0, 1, 0],
                      [0, 0, 0, 0, 1, 0],
                      [0, 0, 0, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0]]

        binary_image = BinaryImage.create_img_from_array(img_depart, BLACK_CONNEXITY, WHITE_CONNEXITY)

        solver = B8W4_Solver(binary_image, BinaryImage.create_img_vertical(binary_image.size))

        solver.resolve_image(binary_image)

        self.assertEqual(img_soluce,
                         binary_image.convert_pixels_to_img(),
                         "In case of p cut vertex and W(p) white cut, g goes to p through N(p)"
                         "and l is cut vertex and SSW(l) is cut. z exist such as w(z) is black and N(z) and NN(z) are "
                         "white, the interchange should be <z, NE(z)>")
        self.assertEqual(1,
                         len(solver.array_interchange),
                         "The number interchange should be 1")
Esempio n. 30
0
    def test_compute_frontier(self):
        binary_image = BinaryImage.create_img_from_array(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = B4W4_Elements(binary_image)

        frontier_test = [algo.binary_image.get_pixel(1, 5),
                         algo.binary_image.get_pixel(2, 5),
                         algo.binary_image.get_pixel(4, 5),
                         algo.binary_image.get_pixel(5, 5)]

        frontier_algo, temp = algo.compute_frontier()

        self.assertEqual(frontier_test,
                         frontier_algo,
                         "Pixels that should be in frontier [(1, 5), (2, 5), (4, 5), (5, 5)]")