I am writing a program in python to find "islands" of 1s, 0s or -1s in a L*L matrix. I need It to find these "regions" of connected components, label each one of them, and be capable of returning, for a given element of the matrix m[x][y] , the size of the island it belongs to.
import numpy as np
from scipy import ndimage
from scipy import misc
import matplotlib.pyplot as plt
m = np.random.randint(-1,2,(L,L))
mask1 = (m == -1)
mask2 = (m == 0)
mask3 = (m == 1)
label_m1, nb_labels1 = ndimage.label(mask1)
label_m2, nb_labels2 = ndimage.label(mask2)
label_m3, nb_labels3 = ndimage.label(mask3)
This should give me labelled islands for a random matrix m (just an example), but I do not know how to get the size of the "region" a given point belongs to. Could you help me? I have almost no programming experience, so forgive me if the question is silly.
Thanks