0

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
    How are coordinates stored? Is the labeled image a numpy array? Can you show the code that generates it? – fdermishin Nov 24 '20 at 11:05
  • 1
    @user13044086 The image is a numpy array, this is default through skimage. Coordinates are stored in a list location such that: for i pairs of coordinates: x, y = list[i] – Charles Kelly Nov 24 '20 at 12:53
  • Can you just check that all elements of labelledImage are the same for the given coordinates? Like so: `[labelledImage[y, x] for x, y in list]` And then check ell elements are the same – fdermishin Nov 24 '20 at 13:05

1 Answers1

2

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.
Juan
  • 5,433
  • 21
  • 23