To complete this recipe, we need to perform the following steps:
- Import the modules:
import cv2import numpy as npimport matplotlib.pyplot as plt
- Read the test image:
image = cv2.imread('../data/Lena.png', 0)
- Estimate the threshold using Otsu's method:
otsu_thr, otsu_mask = cv2.threshold(image, -1, 1, cv2.THRESH_BINARY | cv2.THRESH_OTSU)print('Estimated threshold (Otsu):', otsu_thr)
- Visualize the results:
plt.figure()plt.subplot(121)plt.axis('off')plt.title('original')plt.imshow(image, cmap='gray')plt.subplot(122)plt.axis('off')plt.title('Otsu threshold')plt.imshow(otsu_mask, cmap='gray')plt.tight_layout()plt.show()