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_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 main() -> int: # dict_img = { # # "ex_simple": showKDiag_simple, # "ex_p_1_not_cut": showKDiag_case_not_cut, # # "ex_p1_cut": showKDiag_case_cut, # # "ex_p1_cut_2": showKDiag_case_cut_2, # # "revese k_diag": weirdKDiag # } # # for key, img in dict_img.items(): # binary_image_start = BinaryImage.create_img_from_array(img, black_connexity, white_connexity) # binary_image_final = BinaryImage.create_img_vertical(binary_image_start.size, black_connexity, white_connexity) # solver = B4W4_Solver(binary_image_start, binary_image_final) # print("\n----- Image binary ", key, " -----") # for p in solver.imageElementsStart.all_elbows: # k, p_1 = solver.imageElementsStart.lemme_5(p) # print(k, "-diagonal avec on pixel p ", p, " et mon p1 ", p_1) # p = solver.imageElementsStart.binary_image.get_pixel(1, 4) # p = solver.imageElementsStart.binary_image.get_pixel(1, 8) # k, p_1 = solver.imageElementsStart.lemme_5(p) # # nb_echange = solver.imageElementsStart.k_diagonal_interchange(p, p_1) # print(nb_echange) binary_image_start = BinaryImage.create_random_img(image_size, black_connexity, white_connexity, seed=seed) binary_image_final = BinaryImage.create_random_img(image_size, black_connexity, white_connexity, seed=seed * 2) solver = B4W4_Solver(binary_image_start, binary_image_final) ###### Affichage ###### binary_image_displayer = BinaryImageDisplayer(show_border=False, show_isolated=False, show_legend=True) binary_image_displayer.show(image=binary_image_start, subtitle="Depart") # binary_image_displayer.show(image=binary_image_final, subtitle="Arrivée") # Solver nb_echange = solver.solve() binary_image_displayer.create_gif( image=solver.imageElementsStart.get_saved_img(), array_interchage=solver.array_interchange, speed=1500) binary_image_displayer.show(image=solver.imageElementsFinal.binary_image, subtitle="AFTER Solve") print("Nombre d'échange total :", nb_echange) return 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)
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_test_spiral() -> int: displayer = BinaryImageDisplayer() binary_img = BinaryImage.create_img_spiral(50, 4, 4) binary_img_vertical = binary_img.create_img_vertical(binary_img.size, 4, 4) displayer.show(binary_img, subtitle="Image de départ") solver = B4W4_Solver(binary_img, binary_img_vertical) nb_echange = solver.solve() print("Nombre d'échange total : ", nb_echange) # displayer.create_gif(image=solver.imageElementsStart.get_saved_img(), # array_interchage=solver.array_interchange, speed=1000, name="B4_W4_Spirale.gif") displayer.show(binary_img, subtitle="Image Résultante") return 0
def main_b4_w4() -> int: image = BinaryImage.create_img_spiral(50, 4, 4) image_final = BinaryImage.create_img_vertical(image.size, 4, 4) displayer = BinaryImageDisplayer(show_legend=True) solver = B4W4_Solver(image, image_final) displayer.show(image, subtitle="Image de départ") nb_interchange = solver.solve() print("Nb interchange = ", nb_interchange) print("len interchange = ", len(solver.array_interchange)) displayer.show(image, subtitle="Image de fin") displayer.create_gif(image=solver.imageElementsStart.get_saved_img(), array_interchage=solver.array_interchange, speed=1000, name="B4_W8.gif") return 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) 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 become white") self.assertEqual(1, len(solver.imageElementsStart.array_interchange), "The number interchange should be 1")
def main_fonctionnel() -> int: binary_image_start = BinaryImage.create_img_from_array( showKDiag_case_cut_2, black_connexity, white_connexity) binary_image_final = BinaryImage.create_img_vertical( binary_image_start.size, black_connexity, white_connexity) displayer = BinaryImageDisplayer() displayer.show(binary_image_start, subtitle="image de départ") solver = B4W4_Solver(binary_image_start, binary_image_final) nb_echange = solver.solve() print("Nombre d'échange total :", nb_echange) # displayer.create_gif(image=solver.imageElementsStart.get_saved_img(), # array_interchage=solver.array_interchange, speed=500) displayer.show(image=solver.imageElementsFinal.binary_image, subtitle="AFTER Solve") return 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) 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 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.imageElementsStart.array_interchange), "The number interchange should be 5")