title: Detecting comic strip dialogue bubble regions in images
body: <p>I have an grayscale image of a comic strip page that features several dialogue bubbles (=speech baloons, etc), that are enclosed areas with white background and solid black borders that contain text inside, i.e. something like that:</p>
  
  <p><a href="https://i.stack.imgur.com/gIEXY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gIEXY.png" alt="Sample comic strip image"></a></p>
  
  <p>I want to detect these regions and create a mask (binary is ok) that will cover all the inside regions of dialogue bubbles, i.e. something like:</p>
  
  <p><a href="https://i.stack.imgur.com/bIbYp.png" rel="noreferrer"><img src="https://i.stack.imgur.com/bIbYp.png" alt="Sample resulting mask image"></a></p>
  
  <p>The same image, mask overlaid, to be totally clear:</p>
  
  <p><a href="https://i.stack.imgur.com/Ru9ip.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Ru9ip.png" alt="Sample image with transparent mask overlay"></a></p>
  
  <p>So, my basic idea of the algorithm was something like:</p>
  
  <ol>
  <li>Detect where the text is — plant at least one pixel in every bubble. Dilate these regions somewhat and apply threshold to get a better starting ground; I've done this part:</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/YFRh9.png" rel="noreferrer"><img src="https://i.stack.imgur.com/YFRh9.png" alt="Text positions outlined"></a></p>
  
  <ol start="2">
  <li><p>Use a flood fill or some sort of graph traversal, starting from every white pixel detected as a pixel-inside-bubble on step 1, but working on initial image, flooding white pixels (which are supposed to be inside the bubble) and stopping on dark pixels (which are supposed to be borders or text).</p></li>
  <li><p>Use some sort of <a href="http://scikit-image.org/docs/dev/api/skimage.morphology.html#binary-closing" rel="noreferrer">binary_closing</a> operation to remove dark areas (i.e. regions that correspond to text) inside bubbles). This part works ok.</p></li>
  </ol>
  
  <p>So far, steps 1 and 3 work, but I'm struggling with step 2. I'm currently working with <a href="http://scikit-image.org" rel="noreferrer">scikit-image</a>, and I don't see any ready-made algorithms like flood fill implemented there. Obviously, I can use something trivial like breadth-first traversal, basically <a href="http://arcgisandpython.blogspot.ru/2012/01/python-flood-fill-algorithm.html" rel="noreferrer">as suggested here</a>, but it's really slow when done in Python. I suspect that intricate morphology stuff like <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.morphology.binary_erosion.html#scipy.ndimage.morphology.binary_erosion" rel="noreferrer">binary_erosion</a> or <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.morphology.generate_binary_structure.html#scipy.ndimage.morphology.generate_binary_structure" rel="noreferrer">generate_binary_structure</a> in ndimage or scikit-image, but I struggle to understand all that morphology terminology and basically how do I implement such a custom flood fill with it (i.e. starting with step 1 image, working on original image and producing output to separate output image).</p>
  
  <p>I'm open to any suggestions, including ones in OpenCV, etc.</p>