/iterative-grabcut

This algorithm uses a rectangle made by the user to identify the foreground item. Then, the user can edit to add or remove objects to the foreground. Then, it removes the background and makes it transparent.

Primary LanguageJupyter NotebookMIT LicenseMIT

iterative-grabcut: Using simple rectangle for identifying foreground items

This algorithm uses a rectangle made by the user to identify the foreground item. Then, the user can edit to add or remove objects to the foreground. Once done, it removes the background and makes it transparent (adds alpha channel) and saves it.

This is a simple notebook implementing the GrabCut algorithm shown in the High-Quality Background Removal Without Green Screens | State of the Art Approach Explained video on What's AI's channel.

How does it work? With example

  1. Clone the repo and install the requirements in your environment using pip install -r requirements.txt
  2. Upload your image.
  3. Create a rectangle over your image including all your foreground objects.
  4. Remove the background using GrabCut algorithm.
  5. If improvement is needed, draw on the image, left click (white) = foreground and right click(black) = background.
  6. Remove the background using GrabCut algorithm.
  7. Return to step 4 or end.

Video demos

Example with one iteration to correct the first pass of the algorithm:

2020-08-22_18-02-12_1.mp4

Example with harder picture and multiple iterations:

2020-08-22_18-02-12.mp4

Results:

References:

Interactive Foreground Extraction using GrabCut Algorithm, OpenCV, (2013), https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_grabcut/py_grabcut.html