OpenCV proposes the implementation of another popular algorithm for image segmentation—the GrabCut algorithm. This algorithm is not based on mathematical morphology, but we have presented it here since it shows some similarities in its use with the watershed segmentation algorithm we presented earlier in this chapter. GrabCut is computationally more expensive than watershed, but it generally produces more accurate results. It is the best algorithm to use when you want to extract a foreground object in a still image (for example, to cut and paste an object from one picture to another).
Extracting foreground objects with the GrabCut algorithm
How to do it...
The cv::grabCut function is easy to use. You just need to input...