Search code examples
pythonopencvcomputer-visionmetaledge-detection

Convert Edge Detection to Mask


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 "sum" of the point in a marked segment?

Image for illustration: enter image description here

Thank you in advance.

UPDATE:

Added example of a lighter image (https://i.sstatic.net/hGgLg.jpg). 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 "stand out" from its background. Our end goal is to get the most accurate "mask" of this object, so we can use it for ML processing.

enter image description here


Solution

  • 1.
    Given the "ROI" supervision you have, I strongly recommend you to explore GrabCut (as proposed by YoniChechnik):
    Rother C, Kolmogorov V, Blake A. "GrabCut" interactive foreground extraction using iterated graph cuts. ACM transactions on graphics (TOG). 2004.

    To get a feeling of how this works, you can use power-point's "background removal" tool:
    enter image description here
    which is based on GrabCut algorithm.

    This is how it looks in power point:
    enter image description here

    GrabCut segment the foreground object in a selected ROI mainly based on its foreground/background color distributions, and less on edge/boundary information though this extra information can be integrated into the formulation.

    It seems like opencv has a basic implementation of GrabCut, see here.


    2.
    If you are seeking a method that uses only the boundary information, you might find this answer useful.


    3.
    An alternative method is to use NCuts: Shi J, Malik J. Normalized cuts and image segmentation. IEEE Transactions on pattern analysis and machine intelligence. 2000.

    If you have very reliable edge map, you can modify the "affinity matrix" NCuts works with to be a binary matrix

             0  if there is a boundary between i and j
    w_ij =   1  if there is no boundary between i and j
             0  if i and j are not neighbors of each other
    

    NCuts can be viewed as a way to estimate "robust connected components".