I’m wonderign if it exists a function that does the thing, or a piece of code to do so.
you should maybe illustrate what you mean
Would be nice to have at least one image to understand your problem.
In general, you can use contours or connected component with stats to get the two biggest components on a binary image.
And simple blob detector can be handy to non binary images.