Please see the following blog post for more details about this code
Filling holes in an image using OpenCV ( Python / C++ )