Issue
For a given image mask matrix of shape H x W
containing only True
and False
values, I wish to convert all False
values to True
when they are entirely enclosed by True
values.
e.g.
mask = np.array([
[True, True, True, True, True ],
[True, False, True, False, True ],
[True, True, True, False, True ],
[True, False, False, False, True ],
[True, True, True, True, True ]
])
The result should be
np.array([
[True, True, True, True, True ],
[True, True, True, True, True ],
[True, True, True, True, True ],
[True, True, True, True, True ],
[True, True, True, True, True ]
])
I'm seeking an efficient approach to accomplish this, perhaps through a robust image processing library, rather than crafting a custom Python helper function.
Solution
Here's a solution that uses cv.findContours
.
This finds white islands, with whatever black ponds in them, then fills the islands.
In your example case, it would return a single contour that covers the entire domain. In my case, it will find three contours: the concave blob at the top, the small one on the left, and the large one on the right. It will not bother finding the little circle contained in the big circle.
tmp = src.astype(np.uint8).copy()
(cnts, _) = cv.findContours(
tmp, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(
image=tmp,
contours=cnts, contourIdx=-1,
color=1, thickness=cv.FILLED)
tmp = tmp.astype(bool)
Input, Output:
Answered By - Christoph Rackwitz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.