In order to execute this recipe, we will perform the following steps:
- First, we import all of the modules we need:
import cv2import numpy as np
- Open an image and find the connected components in it:
img = cv2.imread('../data/BnW.png', cv2.IMREAD_GRAYSCALE)connectivity = 8num_labels, labelmap = cv2.connectedComponents(img, connectivity, cv2.CV_32S)
- Show the original image with the scaled image with labels:
img = np.hstack((img, labelmap.astype(np.float32)/(num_labels - 1)))cv2.imshow('Connected components', img)cv2.waitKey()cv2.destroyAllWindows()
- Open another image, find its Otsu mask, and get the connected components with their statistics:
img = cv2.imread('../data/Lena.png', cv2.IMREAD_GRAYSCALE)otsu_thr, otsu_mask ...