Prediction thresholding - avoid scattered results

Hey!
I have a model for binary semantic segmentation for cables.
The results good enough for now:

Wehen i create the masks from the predictions, i apply a threshold to get a binary image.
As it can be seen here, the contours by the contours found by opencv are rather scattered; i can’t find a definite threshold that is “perfect” for all images.
image

My question is, maybe is there a library available that processes the predictions in a manner that exceeds the pure thresholding?
Like, finding contours depending on the maybe a bit too low values next to definitive truths?

I hope i could explain my problem well, thank you in advance!