<p>From the two examples you've uploaded I can assume you are thresholding based on difference in color/intensity- I can suggest <a href="https://docs.opencv.org/trunk/d8/d83/tutorial_py_grabcut.html" rel="nofollow noreferrer">grabcut</a> as basic foreground separation- use the edges in the mask in that ROI as input to the algorithm.
Even better- if your thresholding is good as the first image, just skip the edge detection part and this will be the input to grabcut.</p>
<p>======= EDIT =======</p>
<p>@RoiMulia if you need production level I can suggest that you leave the threshold + edge detection direction completly and try background removal techniques (SOTA are currently neural networks such as <a href="https://arxiv.org/pdf/2004.00626.pdf" rel="nofollow noreferrer">Background Matting: The World is Your Green Screen</a> (<a href="https://www.youtube.com/watch?v=JE-7OcNrPao" rel="nofollow noreferrer">example</a>)).</p>
<p>You can also try some ready made background removal APIs such as <a href="https://www.remove.bg/" rel="nofollow noreferrer">https://www.remove.bg/</a> or <a href="https://clippingmagic.com/" rel="nofollow noreferrer">https://clippingmagic.com/</a></p>
Convert Edge Detection to Mask
<p>Giving an image that I applied an edge detection filter, what would be the way (hopefully efficient/performant one) to achieve a mask of the &quot;sum&quot; of the point in a marked segment?</p>
<p>Image for illustration:
<a href="https://i.stack.imgur.com/eQJvi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eQJvi.png" alt="enter image description here" /></a></p>
<p>Thank you in advance.</p>
<p><strong>UPDATE:</strong></p>
<p>Added example of a lighter image (<a href="https://imgur.com/a/MN0t3pH" rel="nofollow noreferrer">https://imgur.com/a/MN0t3pH</a>).
As you'll see in the below image, we assume that when the user marks a region (ROI), there will be an object that will &quot;stand out&quot; from its background. Our end goal is to get the most accurate &quot;mask&quot; of this object, so we can use it for ML processing.</p>
<p><a href="https://i.stack.imgur.com/h0TvZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h0TvZ.jpg" alt="enter image description here" /></a></p>