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")
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)
def main_debug(): img_depart = [[0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0]] binary_image = BinaryImage.create_random_img(n=50, black_connexity=4, white_connexity=8, seed=432432) displayer = BinaryImageDisplayer() displayer.show(binary_image, subtitle="Départ") solver = B4W8_Solver(binary_image, BinaryImage.create_img_vertical(binary_image.size)) solver.solve() displayer.show(binary_image, subtitle="After resolver")
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)
def main_b4_w8() -> int: image = BinaryImage.create_img_spiral(50, 4, 8) image_final = BinaryImage.create_img_vertical(image.size, 4, 8) displayer = BinaryImageDisplayer(show_legend=True) solver = B4W8_Solver(image, image_final) displayer.show(solver.imageStart, subtitle="Image de départ") nb_interchange = solver.solve() print("Nb interchange = ", nb_interchange) print("len interchange = ", len(solver.array_interchange)) displayer.show(solver.imageStart, subtitle="Image de fin") displayer.create_gif(image=solver.get_image_save(), array_interchage=solver.array_interchange, speed=1000, name="B4_W8.gif") return 0
def test_p_not_cut_Np_black(self): img_depart = [[0, 0, 0], [0, 1, 0], [0, 1, 0], [0, 0, 0]] img_soluce = [[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 not cut vertex and N(p) black, the interchange should be <p, NE(p)>") self.assertEqual(1, len(solver.array_interchange), "In case of p not cut vertex and N(p) black, the number interchange should be 1")
def resolve_image(self, binary_image: BinaryImage) -> int: nb_interchange = 0 p = self.get_p(binary_image) array_interchange = [] if not binary_image.is_cut_vertex(p): n = binary_image.get_pixel_adjacent(p, Direction.N) w = binary_image.get_pixel_adjacent(p, Direction.W) n_w = binary_image.get_pixel_directional( p, [Direction.N, Direction.W]) s_w = binary_image.get_pixel_directional( p, [Direction.S, Direction.W]) if n.color == PixelColor.BLACK: n_e = binary_image.get_pixel_directional( p, [Direction.N, Direction.E]) array_interchange.append((p.get_coords(), n_e.get_coords())) binary_image.swap_pixels(p.get_coords(), n_e.get_coords()) nb_interchange += 1 elif n_w.color == PixelColor.BLACK or w.color == PixelColor.BLACK: array_interchange.append((p.get_coords(), n.get_coords())) binary_image.swap_pixels(p.get_coords(), n.get_coords()) nb_interchange += 1 elif n_w.color == PixelColor.WHITE and w.color == PixelColor.WHITE and s_w.color == PixelColor.BLACK: if not binary_image.is_cut_vertex(w): array_interchange.append((p.get_coords(), w.get_coords())) binary_image.swap_pixels(p.get_coords(), w.get_coords()) nb_interchange += 1 else: array_interchange.append( (p.get_coords(), n_w.get_coords())) binary_image.swap_pixels(p.get_coords(), n_w.get_coords()) nb_interchange += 1 else: w = binary_image.get_pixel_adjacent(p, Direction.W) if not binary_image.is_cut_vertex(w): array_interchange.append((p.get_coords(), w.get_coords())) binary_image.swap_pixels(p.get_coords(), w.get_coords()) nb_interchange += 1 else: #g potentiellement blanc ?! g = n_w_w = binary_image.get_pixel_directional( p, [Direction.N, Direction.W, Direction.W]) n = binary_image.get_pixel_adjacent(p, Direction.N) s_w = binary_image.get_pixel_directional( p, [Direction.S, Direction.W]) path_found_sw, south_west_path = B4W8_Solver.find_path( g, p, binary_image, pixel_bridge=s_w, connexity=binary_image.black_connexity) path_found_n, north_path = B4W8_Solver.find_path( g, p, binary_image, pixel_bridge=n, connexity=binary_image.black_connexity) if path_found_sw and south_west_path: n_w = binary_image.get_pixel_directional( p, [Direction.N, Direction.W]) array_interchange.append( (p.get_coords(), n_w.get_coords())) binary_image.swap_pixels(p.get_coords(), n_w.get_coords()) nb_interchange += 1 elif path_found_n and north_path: l = self.compute_l(p, binary_image) s_l = binary_image.get_pixel_adjacent(l, Direction.S) if not binary_image.is_cut_vertex(s_l): s_e_l = binary_image.get_pixel_directional( l, [Direction.S, Direction.E]) array_interchange.append( (s_e_l.get_coords(), s_l.get_coords())) binary_image.swap_pixels(s_e_l.get_coords(), s_l.get_coords()) nb_interchange += 1 else: s_s_w_l = binary_image.get_pixel_directional( l, [Direction.S, Direction.S, Direction.W]) if not binary_image.is_cut_vertex(s_s_w_l): array_interchange.append( (s_s_w_l.get_coords(), s_l.get_coords())) binary_image.swap_pixels(s_s_w_l.get_coords(), s_l.get_coords()) nb_interchange += 1 else: h, B = self.compute_h(g, binary_image) z = self.compute_z(B, binary_image) if z is not None: n_z = binary_image.get_pixel_adjacent( z, Direction.N) n_n_z = binary_image.get_pixel_directional( z, [Direction.N, Direction.N]) if n_z.color == n_n_z.color == PixelColor.WHITE: n_e_z = binary_image.get_pixel_directional( z, [Direction.N, Direction.E]) array_interchange.append( (z.get_coords(), n_e_z.get_coords())) binary_image.swap_pixels( z.get_coords(), n_e_z.get_coords()) nb_interchange += 1 elif n_z.color == PixelColor.BLACK or n_n_z.color == PixelColor.BLACK: s_s_s_w_l = binary_image.get_pixel_directional( l, [ Direction.S, Direction.S, Direction.S, Direction.W ]) if z.get_coords() == s_s_s_w_l.get_coords( ): w_h = binary_image.get_pixel_adjacent( h, Direction.W) array_interchange.append( (h.get_coords(), w_h.get_coords())) binary_image.swap_pixels( h.get_coords(), w_h.get_coords()) nb_interchange += 1 else: e_e_z = binary_image.get_pixel_directional( z, [Direction.E, Direction.E]) e_e_e_z = binary_image.get_pixel_directional( e_e_z, [Direction.E]) n_e_e_z = binary_image.get_pixel_directional( e_e_z, [Direction.N]) n_e_e_e_z = binary_image.get_pixel_directional( n_e_e_z, [Direction.E]) n_n_e_e_z = binary_image.get_pixel_directional( n_e_e_z, [Direction.N]) n_n_e_e_e_z = binary_image.get_pixel_directional( n_n_e_e_z, [Direction.E]) n_e_z = binary_image.get_pixel_directional( z, [Direction.N, Direction.E]) array_interchange.append( (e_e_z.get_coords(), e_e_e_z.get_coords())) array_interchange.append( (n_e_e_z.get_coords(), n_e_e_e_z.get_coords())) array_interchange.append( (n_n_e_e_z.get_coords(), n_n_e_e_e_z.get_coords())) array_interchange.append( (z.get_coords(), n_e_z.get_coords())) nb_interchange += binary_image.multiple_swap_pixels( array_interchange) else: n_w_h = binary_image.get_pixel_directional( h, [Direction.N, Direction.W]) s_w_g = binary_image.get_pixel_directional( g, [Direction.S, Direction.W]) if h.get_coords() == g.get_coords(): if n_w_h.color == PixelColor.BLACK and s_w_g.color == PixelColor.BLACK: w_g = binary_image.get_pixel_directional( g, [Direction.W]) array_interchange.append( (g.get_coords(), w_g.get_coords())) binary_image.swap_pixels( g.get_coords(), w_g.get_coords()) nb_interchange += 1 else: e_g = binary_image.get_pixel_directional( g, [Direction.E]) e_e_g = binary_image.get_pixel_directional( e_g, [Direction.E]) e_e_e_n_g = binary_image.get_pixel_directional( e_e_g, [Direction.E, Direction.N]) array_interchange.append( (g.get_coords(), e_g.get_coords())) array_interchange.append( (e_e_g.get_coords(), e_e_e_n_g.get_coords())) nb_interchange += binary_image.multiple_swap_pixels( array_interchange) else: s_w_g = binary_image.get_pixel_directional( g, [Direction.S, Direction.W]) n_w_h = binary_image.get_pixel_directional( h, [Direction.N, Direction.W]) path, s_w_path = B4W8_Solver.find_path( g, p, binary_image, binary_image.black_connexity, s_w_g) path_2, n_w_path = B4W8_Solver.find_path( g, p, binary_image, binary_image.black_connexity, n_w_h) if path and s_w_path: n_e_g = binary_image.get_pixel_directional( g, [Direction.N, Direction.E]) array_interchange.append( (g.get_coords(), n_e_g.get_coords())) binary_image.swap_pixels( g.get_coords(), n_e_g.get_coords()) nb_interchange += 1 elif path_2 and n_w_path: e_h = binary_image.get_pixel_directional( h, [Direction.E]) e_e_h = binary_image.get_pixel_directional( e_h, [Direction.E]) e_e_e_n_h = binary_image.get_pixel_directional( e_e_h, [Direction.E, Direction.N]) array_interchange.append( (h.get_coords(), e_h.get_coords())) array_interchange.append( (e_e_h.get_coords(), e_e_e_n_h.get_coords())) nb_interchange += binary_image.multiple_swap_pixels( array_interchange) if nb_interchange > 0: self.array_interchange = [ *self.array_interchange, *array_interchange ] binary_image.expand_image() binary_image.reduce_image() return nb_interchange