Testing if a point lies within a labeled object with scipys ndi.label()

314 Views Asked by At

Labelled Image

Above is an image that has been put through ndi.label()and displayed with matplotlib with each coloured region representing a different feature. Plotted on top of the image are red points that represent a pair of coordinates each. All coordinates are stored and ndi.label returns the number of features. Does skimage, scipy or ndimage have a function that will test if a given set of coordinates lies within a labelled feature?

Initially I intended to use the binding box (left, right, top, bottom) of each feature but due to the regions not all being quadrilateral this won't work.

code to generate the image:

image = io.import("image path")
labelledImage, featureNumber = ndi.label(image)
plt.imshow(labelledImage)

for i in range(len(list))
    y, x = list[i]
    plt.scatter(y,x, c='r', s=40)
1

There are 1 best solutions below

1
Juan On

You can use ndi.map_coordinates to find the value at a particular coordinate (or group of coordinates) in an image:

labels_at_coords = ndi.map_coordinates(
        labelledImage, np.transpose(list), order=0
        )

Notes:

  • the coordinates array needs to be of shape (ndim, npoints), instead of the sometimes more intuitive (npoints, ndim), hence the transpose.
  • ideally, it would be best to rename your points list to something like points_list, so that you don't overwrite the Python built-in function list.