Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2
- img = cv2.imread('watermelon.jpg')
- Z = np.float32(img.reshape((-1,3)))
- # define criteria, number of clusters(K) and apply kmeans()
- criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
- K = 4
- ret,labels,centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
- labels = labels.reshape((img.shape[:-1]))
- for i, c in enumerate(centers):
- mask = cv2.inRange(labels, i, i)
- res = cv2.bitwise_and(img, np.dstack([mask]*3))
- cv2.imshow('', res)
- cv2.waitKey()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement