Re: Coordinate overlap in two labelled images
Suggestion number 3. I'll deal with each of three interpretations of your
question. Suppose your images are im1 and im2. Now, you might want 1) of
the regions in im1, the ones that intersect over at least 1 pixel with
regions of im2; 2) the regions that are part of regions in both im1 and im2
(ie the intersection of regions in im1 and im2); or 3) the union of regions
of im1 and im2 that have at least one pixel intersection with the other
image.
The last example assumes the union'ed regions don't touch each other.
Then:
In [1]: import scipy.ndimage as nd
In [2]: im1 = np.array([[0, 0, 0, 0], [1, 1, 0, 2], [0, 0, 0, 2]])
In [3]: im1
Out[3]:
array([[0, 0, 0, 0],
[1, 1, 0, 2],
[0, 0, 0, 2]])
In [4]: im2 = np.array([[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]])
In [5]: im2
Out[5]:
array([[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]])
In [6]: intersect = im1.astype(bool) * im2.astype(bool)
In [7]: intersect
Out[7]:
array([[False, False, False, False],
[False, True, False, False],
[False, False, False, False]], dtype=bool)
In [8]: overlap_labels1 = np.unique(im1[intersect])
In [9]: selector = np.zeros(im1.max() + 1, int)
In [10]: selector[overlap_labels1] = arange(1, len(overlap_labels1) + 1)
In [11]: reg1 = selector[im1]
In [12]: reg1
Out[12]:
array([[0, 0, 0, 0],
[1, 1, 0, 0],
[0, 0, 0, 0]])
In [13]: reg2 = nd.label(intersect)[0]
In [14]: reg2
Out[14]:
array([[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0]], dtype=int32)
In [15]: overlap_labels2 = np.unique(im2[intersect])
In [16]: selector2 = np.zeros(im2.max() + 1, int)
In [17]: selector2[overlap_labels2] = 1
In [18]: nonzero_pixels = reg1 + selector2[im2]
In [19]: reg3 = nd.label(nonzero_pixels)[0]
In [20]: reg3
Out[20]:
array([[0, 1, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0]], dtype=int32)
On Wed, Apr 3, 2013 at 5:52 AM, Stéfan van der Walt
Hi Stuart
I am using your (amazingly fast) label image routines and regionprops to extract the coordinates of the regions in two images. I need to filter
On Tue, Apr 2, 2013 at 7:04 PM, Stuart Mumford
wrote: these objects out if they do not overlap in both the images, then renumber all the remaining regions. At the moment I am doing something unspeakable with nested loops and sets which is pretty damn slow; I was hoping someone here would have a really clever idea on how to solve this. Failing that does anyone know how I can find the number of coordinates (output from regionprops) that overlap in two numpy arrays? You can use join_segmentations to find an intersection of your two label maps. Then, walk through the newly formed regions and check at each point whether the object exists in both images (that way, you don't have so many comparisons to do).
http://scikit-image.org/docs/dev/auto_examples/plot_join_segmentations.html
If that's not exactly what you had in mind, you can use this code to find all the connected regions in an image:
https://github.com/stefanv/lulu/blob/master/lulu/base.pyx#L21
These regions are encoded sparsely, and testing their intersection is very fast (see the algorithm section of this paper: https://scholar.sun.ac.za/handle/10019.1/4883).
I hope that helps! Stéfan
-- You received this message because you are subscribed to the Google Groups "scikit-image" group. To unsubscribe from this group and stop receiving emails from it, send an email to scikit-image+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
participants (1)
-
Juan Nunez-Iglesias