Algorithm to extract polygones with holes
You need to apply some image segmentation algorithms. You may read about A Comparison of Image Segmentation Algorithms and apply the ones that satisfies your needs better.

If you're developing with OpenCV, you can find some ready to use (implemented) algorithms that can save you lot of work as it is the case with the watershed algorithm.

Note that you can use OpenCV with the traditional programming language C/C++ but also many others such as Python and Java. Personally I experienced it with C/C++ and Python though.