Goal:
I'd like to dilate a binary mask with a kernel size of N, then erode it with effectively the same kernel, but leave any connections intact that were formed by the dilation. When I do connectedComponentsWithStats, I want anything close together to have been merged into one component.
This has been a surprisingly challenging endeavor.
Example Image:
In this case, the goal is to have this stray pixel join the object on the left, but not change the size of the object on the left.
Effort put in:
At first glance, a "closing" operation seemed perfect for this, but I noticed that it was eroding the connection formed during the dilation, essentially not working at all for this use-case.
I've been thinking through other options of dilation, erosion, and potentially some creative use of contours to help.
I thought I was close if I were to do a close operation on the inversion of the mask as documented here, but that had the effect of eliminating significant parts of the source mask, not just closing the gaps.
One idea I had was to do a dilation, find the skeleton, erode, and then "or" the skeleton and original image together. I ran into two issues: 1) At least the implementations of finding mask skeletons I found online were extremely slow, and 2) I'm not sure this is the best idea in the first place (would the skeletal line go through my single pixel I'm trying to capture?)
I'm hoping I am just being silly and missing something obvious?