Hi! I would do this in four steps:
- Extract connected component of text (using your binary mask)
- For each connected component, look for the surrounding class (i.e. object of interest or background)
- Paint each connected component in white or black, depending on the surrounding class...
- extract contour of the new binary mask
It's probably not the fastest way to do that, but it will probably work!