Book Image

OpenCV Computer Vision Application Programming Cookbook Second Edition

By : Robert Laganiere
Book Image

OpenCV Computer Vision Application Programming Cookbook Second Edition

By: Robert Laganiere

Overview of this book

Table of Contents (18 chapters)
OpenCV Computer Vision Application Programming Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Extracting foreground objects with the GrabCut algorithm


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 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).

How to do it...

The cv::grabCut function is easy to use. You just need to input an image, and label some of its pixels as belonging to the background or to the foreground. Based on this partial labeling, the algorithm will then determine a foreground/background segmentation for the complete image.

One way to specify a partial foreground/background labeling...