Attach islands to a nearby mass on a binnary image without using closing/dilate

I have a stack of 2D images where i identify islands and attach them to a nearby mass, for that i’m using the morph closing, problem is the small details that can matter are also glued, also i can’t use high iterations to attach some far masses or it will destroy the model.

Example:

On that planetary cog i have draw 3 shapes near the outer shell as my islands to illustrate the problem, now i need a way to attach the identified islands via some anchors to the nearby mass (if close in a certain range) without use the morph closing which will destroy the planetary gears and glue them.

Kinda of desired solution (Forgive my drawing skills)

What is the better/smart way to do this?

untested idea:

  • find the outer circle
  • draw a filled white circle into a seperate black image at same place/size
    (aka mask)
  • apply morphology to your image, no matter what happens to the gears
  • copy the original inside content of the circle back onto the result, using the mask made before (copyTo())

Well thought! that would kinda work, but will not do so well on anti-aliasing images where objects have a fade greys stroke (AA) that would still merge objects but the copyTo will paste those faded greys and create a weak bound on the joint, and i can’t threshold nor recreate the AA as the file come from a program that process that AA and don’t want to destroy it. To solve that maybe i can subtract the original from the modified, and set all pixels in common to white…
But the biggest problem is: if i apply a morph close with very high iterations that will invalidate the original shape, note that i have a circle there, but the shapes can be in any size/shape like a really wierd object. Morph close with very high iteration will not mantain the original shape after that, for example a U, some corners or curves will get cut or rounded. And then plaste the orginal content over the modified will have extra unwanted pixels? To hard things up, i can have multiple islands in different locations, distances and sizes :frowning:
Still i will explore your ideia.

Example for a complex image (Island at red):

1 Like

if I understand it correctly you can find contours with area limit and match via draw line function using distance limit. I think it would be most usable method for you.

Any example of this approach?

this is a slice of geometry as used by 3D printers.

your source data is 3D geometry data, i.e. vectors not pixels. you should look for ways to work on the original geometry instead of rasterized slices.

what you want is inconsistent: the little cogs inside the ring, why should they not be connected to the ring, but those shapes on the outside should? how should an algorithm know the difference?

this fundamentally requires an user to select volumes/features that should be connected.

the solution probably involves distance fields.

look for real-world applications/implementations. look around in “computer graphics” and game programming circles.

Correct

My software (UVtools) is post-process app, it works on output files → sliced images (pixels) produced by any slicer software, it don’t have any 3D information (STL) and the objective is heal/repair/mutate slices.

It does because i have island detection, i know exactly what are the blobs i need to anchor, and the blobs that are “safe masses”.
Example of the detection on the app:

They already can connect the islads to nearby solids manually, but that is not optimal as i’m seeing to improve my auto repair. Slicers are not perfect and some generate tons of islands even with auto supports, it fails to detect proper islands and you end with files with a tons of islands to repair, manually would take forever.

I think one way or another i can use the closing, but is inefficient the way i’m thinking, because i need to redraw 1 per 1 island and get the most nearby blob and run some closings util they merge, its a heavy loop

PS: This is my current repair method: Source it already does some good stuff

as I said, you could experiment with distance fields.

prepare a bitmap/array with all anchor shapes (as zero-value pixels, on white or whatever). calculate the distance transform.

now you can look up for any floating shape the distance to the nearest anchor, and from following the gradient, you can even find that anchor (a straight line towards it). given that, you can construct whatever width and shape of connection you like.

https://docs.opencv.org/master/d7/d1b/group__imgproc__misc.html#ga8a0b7fdfcb7a13dde018988ba3a43042

https://docs.opencv.org/master/d2/dbd/tutorial_distance_transform.html

1 Like

Thank you, i will have a look into that, never worked with distance fields before.
If i come to a solution i will post here.

Btw not related to this, but currently i’m using connectedComponentsWithStats because i need 4 connected and 8 connected options which contour detection doesnt allow to opt for (8 by default). Is there any way to get the contours from connectedComponents data without have to run the contour detection (findContours) over the image?