1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- from skimage.measure import label
- import matplotlib.pyplot as plt
- import numpy as np
- struct = np.ones((3,3))
- def dilation(arr, struct=struct):
- result = np.zeros_like(arr)
- for y in range(1, arr.shape[0]-1):
- for x in range(1, arr.shape[1]-1):
- sub = arr[y-1:y+2, x-1:x+2]
- rsub = np.logical_and(arr[y,x], struct)
- result[y-1:y+2, x-1:x+2] = np.logical_or(result[y-1:y+2, x-1:x+2], rsub)
- return result
- def errosion(arr, struct=struct):
- result = np.zeros_like(arr)
- for y in range(1, arr.shape[0]-1):
- for x in range(1, arr.shape[1]-1):
- sub = arr[y-1:y+2, x-1:x+2]
- if np.all(sub >= struct):
- result[y, x] = 1
- return result
- def closing(arr, struct=struct):
- return dilation(errosion(arr, struct), struct)
- def opening(arr, struct=struct):
- return errosion(dilation(arr, struct), struct)
- img = np.load('wires5.npy.txt')
- struct = np.array([[0,1,0],
- [0,1,0],
- [0,1,0]])
- plt.subplot(1,2,1)
- plt.imshow(img)
- plt.subplot(1,2,2)
- plt.imshow(closing(img, struct))
- for wire in np.unique(label(img))[1:]:
- # print(np.where(label(img)==wire))
- res = label(closing(img, struct))[label(img)==wire]
- match(len(np.unique(res)[1:])):
- case 1: print("Провод не порван")
- case 0: print("Провод уничтожен")
- case _: print(f"Провод {wire} порван на {len(np.unique(res)[1:])}")
- plt.show()
|