import numpy as np
import matplotlib.pyplot as plt
from skimage.measure import label
from pathlib import Path

img = np.load(Path(Path.cwd(), 'figures', 'ps.npy.txt'))

labeled = label(img)

figures = {}

# Частотный словарь

for lbl in np.unique(labeled)[1:]:
    x, y = np.where(labeled == lbl)
    x = x - x.min()
    y = y - y.min()
    key = (str(x), str(y))
    if key not in figures.keys():
        figures[key] = 1
    else: figures[key] += 1

for i, (figure, coun) in enumerate(figures.items()):
    plt.subplot(1,5,i+1)
    figure = (np.fromstring(figure[0][1:-1], dtype=int, sep=' '), np.fromstring(figure[1][1:-1], dtype=int, sep=' '))
    fig = np.zeros((max(figure[0])+1,max(figure[1])+1))
    fig[figure[0], figure[1]] = 1
    plt.title(f"{coun}")
    plt.imshow(fig)
plt.suptitle(f"Всего: {labeled.max()}")
plt.show()