main.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import sys
  2. from skimage.measure import label
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. def dil(arr, struct):
  6. res = np.zeros_like(arr)
  7. for y in range(1, arr.shape[0] - 1):
  8. for x in range(1, arr.shape[1] - 1):
  9. sub = arr[y - 1 : y + 2, x - 1 : x + 2]
  10. rsub = np.logical_and(arr[y, x], struct)
  11. res[y - 1 : y + 2, x - 1 : x + 2] = np.logical_or(
  12. res[y - 1 : y + 2, x - 1 : x + 2], rsub
  13. )
  14. return res
  15. def erros(arr, struct):
  16. result = np.zeros_like(arr)
  17. for y in range(1, arr.shape[0] - 1):
  18. for x in range(1, arr.shape[1] - 1):
  19. sub = arr[y - 1 : y + 2, x - 1 : x + 2]
  20. if np.all(sub >= struct):
  21. result[y, x] = 1
  22. return result
  23. def cl(arr, struct):
  24. return dil(erros(arr, struct), struct)
  25. def op(arr, struct):
  26. return erros(dil(arr, struct), struct)
  27. if len(sys.argv) < 2:
  28. print("No path provided")
  29. exit()
  30. num = np.load(sys.argv[1])
  31. m = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
  32. plt.subplot(1, 2, 1)
  33. plt.imshow(num)
  34. plt.subplot(1, 2, 2)
  35. plt.imshow(cl(num, m))
  36. for w in np.unique(label(num))[1:]:
  37. res = label(cl(num, m))[label(num) == w]
  38. match len(np.unique(res)[1:]):
  39. case 1:
  40. print("Провод не порван")
  41. case 0:
  42. print("Провод уничтожен")
  43. case _:
  44. print(f"Провод {w} порван на {len(np.unique(res)[1:])}")
  45. plt.show()