Label different objects in binary image

how can i label the squares in this binary image by row? For example the first two squares would be row 0, then next ones row 1 etc…

i thought about using label from scipy but i have no idea how to define my structure.

from scipy.ndimage import label

struct = [[0,1,0],

labels = label(Objects, structure=None, output=None)

You can use regionprops to get the centroid of each square in the image, then based on the centroid position you could determine the squares row and column.

from skimage.measure import regionprops

for region in regionprops(labels):
    print(region.centroid, region.label)

For your question on the structure parameter: In your case you don’t really need the structure parameter, which is only useful if you care about objects connected in very specific ways. Your squares are completely separated so the default parameters will label each of your squares with a single label. You don’t need the output parameter either. Note that you can also use the skimage labelling function. Then you can use regionprops as suggested in the first answer to find the coordinates of your labels. So:

import skimage.measure

labels = skimage.measure.label(Objects)

for region in skimage.measure.regionprops(labels):
    print(region.centroid, region.label)

Note that skimage has now a “nicer” output in the form of a dictionary for the region measurements where you can directly specify properties to measure. That would be e.g.:

props = skimage.measure.regionprops_table(labels, properties = ('label', 'centroid'))