main.py 854 B

12345678910111213141516171819202122232425262728293031
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from skimage.measure import label
  4. from pathlib import Path
  5. img = np.load(Path(Path.cwd(), 'figures', 'ps.npy.txt'))
  6. labeled = label(img)
  7. figures = {}
  8. # Частотный словарь
  9. for lbl in np.unique(labeled)[1:]:
  10. x, y = np.where(labeled == lbl)
  11. x = x - x.min()
  12. y = y - y.min()
  13. key = (str(x), str(y))
  14. if key not in figures.keys():
  15. figures[key] = 1
  16. else: figures[key] += 1
  17. for i, (figure, coun) in enumerate(figures.items()):
  18. plt.subplot(1,5,i+1)
  19. figure = (np.fromstring(figure[0][1:-1], dtype=int, sep=' '), np.fromstring(figure[1][1:-1], dtype=int, sep=' '))
  20. fig = np.zeros((max(figure[0])+1,max(figure[1])+1))
  21. fig[figure[0], figure[1]] = 1
  22. plt.title(f"{coun}")
  23. plt.imshow(fig)
  24. plt.suptitle(f"Всего: {labeled.max()}")
  25. plt.show()