Download this project.


Written by Philipp Rudiger and Maxime Liquet
Created: November 16, 2018
Last updated: January 7, 2021

Extract a coast- and shoreline contour with GrabCut

The GrabCut algorithm provides a way to annotate an image using polygons or lines to demark the foreground and background. The algorithm estimates the color distribution of the target object and that of the background using a Gaussian mixture model. This is used to construct a Markov random field over the pixel labels, with an energy function that prefers connected regions having the same label, and running a graph-cut-based optimization to infer their values. This procedure is repeated until convergence, resulting in an image mask denoting the foreground and background.

In this example this algorithm is applied to map tiles to automatically extract a coast or shoreline contour. First we specify a rectangular region in which to download the map tiles using the SelectRegionPanel, then we can declare the GrabCutPanel to annotate the region and let the algorithm compute a contour.

In [1]:
import numpy as np
import panel as pn
import holoviews as hv
import geoviews as gv
from grabcut import paths_to_polys, GrabCutPanel, SelectRegionPanel