Issue
Similar to my question about eroding a scipy label array, I'm trying to dilate label regions.
The tricky part is keeping the different regions from "invading each other."
Starting with A
, how can I get something like B
?
A = array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 2, 2, 0, 0, 0],
[0, 1, 1, 2, 2, 0, 3, 3],
[0, 0, 0, 0, 0, 0, 3, 3],
[0, 0, 0, 0, 0, 0, 3, 3],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
B = array([[1, 1, 1, 2, 2, 2, 0, 0],
[1, 1, 1, 2, 2, 2, 3, 3],
[1, 1, 1, 2, 2, 2, 3, 3],
[1, 1, 1, 2, 2, 3, 3, 3],
[0, 0, 0, 0, 0, 3, 3, 3],
[0, 0, 0, 0, 0, 3, 3, 3],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
There are some cases that are ambiguous, and I'm not quite sure how it should behave, but I think what I'm after is clear.
Solution
One option is to just use a maximum filter and then reset any regions that were "invaded".
As an example:
import numpy as np
import scipy.ndimage as ndimage
A = np.array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 2, 2, 0, 0, 0],
[0, 1, 1, 2, 2, 0, 3, 3],
[0, 0, 0, 0, 0, 0, 3, 3],
[0, 0, 0, 0, 0, 0, 3, 3],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
B = ndimage.maximum_filter(A, 3)
B[A != 0] = A[A != 0]
print B
This yields:
[[1 1 2 2 2 2 0 0]
[1 1 1 2 2 3 3 3]
[1 1 1 2 2 3 3 3]
[1 1 2 2 2 3 3 3]
[0 0 0 0 0 3 3 3]
[0 0 0 0 0 3 3 3]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]]
In the dilated areas, the biggest number always "wins". It's one approach, at any rate.
Answered By - Joe Kington
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.