How it works...

Otsu's method estimates the threshold for grayscale images in such a way that after binarization and converting the original image to a binary mask, the total intra-class variance for two classes is minimal. Otsu's method can be used with the help of the cv2.threshold function, having specified the flag cv2.THRESH_OTSU.

The following output is expected from the preceding code:

Estimated threshold (Otsu): 116.0

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.