main.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from skimage.measure import label
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. struct = np.ones((3,3))
  5. def dilation(arr, struct=struct):
  6. result = 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. result[y-1:y+2, x-1:x+2] = np.logical_or(result[y-1:y+2, x-1:x+2], rsub)
  12. return result
  13. def errosion(arr, struct=struct):
  14. result = np.zeros_like(arr)
  15. for y in range(1, arr.shape[0]-1):
  16. for x in range(1, arr.shape[1]-1):
  17. sub = arr[y-1:y+2, x-1:x+2]
  18. if np.all(sub >= struct):
  19. result[y, x] = 1
  20. return result
  21. def closing(arr, struct=struct):
  22. return dilation(errosion(arr, struct), struct)
  23. def opening(arr, struct=struct):
  24. return errosion(dilation(arr, struct), struct)
  25. img = np.load('wires5.npy.txt')
  26. struct = np.array([[0,1,0],
  27. [0,1,0],
  28. [0,1,0]])
  29. plt.subplot(1,2,1)
  30. plt.imshow(img)
  31. plt.subplot(1,2,2)
  32. plt.imshow(closing(img, struct))
  33. for wire in np.unique(label(img))[1:]:
  34. # print(np.where(label(img)==wire))
  35. res = label(closing(img, struct))[label(img)==wire]
  36. match(len(np.unique(res)[1:])):
  37. case 1: print("Провод не порван")
  38. case 0: print("Провод уничтожен")
  39. case _: print(f"Провод {wire} порван на {len(np.unique(res)[1:])}")
  40. plt.show()