How To Extract Foreground From Images Interactively Using GrabCut?

Mask R CNNimage segmentation etc are all algorithms that have become extremely useful in today‚Äôs world. These algorithms perform well because of the concept involving separation between the foreground and the background. Doing this is quite simple when it comes to OpenCV and this process can be done interactively by drawing the outline ourselves.

In this article, we will use an algorithm called GrabCut to interactively segregate the foreground from the background in an image.

Working of the GrabCut algorithm

  1. The user either chooses to manually select the region of interest or adjusts the mask values in the algorithm to automatically do so. Since we are doing it interactively, we will manually draw the regions. Once the region is selected the area outside the region of interest will be turned black.
  2. A gaussian mixture model (GMM) is applied over the image and this model understands the user input and starts creating labels for unknown pixel values. These pixels are then grouped into one based on the colour statistics.
  3. Based on the above pixel distribution, a graph is generated where these pixels are considered as nodes. Apart from this, there are two other nodes called Source and Sink node that is generated.
  4. All the foreground pixels will be connected to the source node and all the background pixels with the sink node. The edge connecting the source node with the sink node contains the weights which is basically the probability of whether the pixel is a foreground one or background one.
  5. Now the algorithm segments the image into two with the help of a cost function and separates the source and sink node into two. After multiple iterations of this, the algorithm can finally extract the foreground part of the image and make the background black.

All of these are combined into cv2.grabcut() function in OpenCV.

#developers corner #background #foreground #grabcut #image segmentation #opencv

How To Extract Foreground From Images Interactively Using GrabCut?