Search code examples
pythonscikit-imageneighbours

How to find neighbors in binary image with given horizontal and vertical distance (Python)


I have an Image (or several hundreds of them) that need to be analyzed. The goal is to find all black spots close to each other.

For example all black spots with a Horizontal distance of 160 pixel and vertical 40 pixel.

Just a small part of an image

For now I just look at each Pixel and if there is a black pixel I call a recursive Method to find its neighbours (i can post the code too if you want to)

It works, but its very slow. At the moment the script runs about 3-4 minutes depending on image size.

Is there some easy/fast way to accomplish this (best would be a scikit-image method to help out here) I'm using Python.

edit: I tried to use scikit.measure.find_contours, now i have an array with arrays containing the contours of the black spots. Now I only need to find the contours in the neighbourhood of these contours.


Solution

  • Disclaimer: I'm not proficient with the scikit image library at all, but I've tackled similar problems using MATLAB so I've searched for the equivalent methods in scikit, and I hope my findings below help you.

    First you can use skimage.measure.label which returns label_image, i.e. an image where all connected regions are labelled with the same number. I believe you should call this function with background=255 because from your description it seems that the background in your images is the while region (hence the value 255).

    This is essentially an image where the background pixels are assigned the value 0 and the pixels that make up each (connected) spot are assigned the value of an integer label, so all the pixels of one spot will be labelled with the value 1, the pixels of another spot will be labelled with the value 2, and so on. Below I'll refer to "spots" and "labelled regions" interchangeably.

    You can then call skimage.measure.regionprops, that takes as input the label_image obtained in the previous step. This function returns a list of RegionProperties (one for each labelled region), which is a summary of properties of a labelled region.

    Depending on your definition of

    The goal is to find all black spots close to each other.

    there are different fields of the RegionProperties that you can use to help solve your problem:

    • bbox gives you the set of coordinates of the bounding box that contains that labelled region,
    • centroid gives you the coordinates of the centroid pixel of that labelled region,
    • local_centroid gives you the centroid relative to the bounding box bbox

    (Note there are also area and bbox_area properties which you can use to decide whether to throw away very small spots that you might not be interested in, thus reducing computation time when it comes to comparing proximity of each pair of spots)

    If you're looking for a coarse comparison, then comparing the centroid or local_centroid between each pair of labelled regions might be enough.

    Otherwise you can use the bbox coordinates to measure the exact distance between the outer bounds of any two regions.

    If you want to base the decision on the precise distance between the pixel(s) of each pair of regions that are closest to each other, then you'll likely have to use the coords property.