Advertisement
dan-masek

Untitled

Oct 14th, 2018
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3.  
  4. img = cv2.imread('watermelon.jpg')
  5. Z = np.float32(img.reshape((-1,3)))
  6.  
  7. # define criteria, number of clusters(K) and apply kmeans()
  8. criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
  9. K = 4
  10. ret,labels,centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
  11. labels = labels.reshape((img.shape[:-1]))
  12.  
  13. for i, c in enumerate(centers):
  14.     mask = cv2.inRange(labels, i, i)
  15.     res = cv2.bitwise_and(img, np.dstack([mask]*3))
  16.     cv2.imshow('', res)
  17.     cv2.waitKey()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement