Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. from sklearn.cluster import MeanShift, estimate_bandwidth
  4.  
  5. #Loading original image
  6. original_img = cv2.imread('Swimming_Pool.jpg')
  7.  
  8. #Shape of original image
  9. original_shape = original_img.shape
  10.  
  11. #Converting image into array of dimension [nb of pixels in original_img, 3] feature array based on r g b intensities
  12. flat_img=np.reshape(original_img, [-1, 3])
  13.  
  14. #Estimate bandwidth for meanshift algorithm
  15. bandwidth = estimate_bandwidth(flat_img, quantile=0.1, n_samples=20)
  16. ms = MeanShift(bandwidth = bandwidth, bin_seeding=True)
  17.  
  18. #Performing meanshift on flatImg
  19. ms.fit(flat_img)
  20.  
  21. #(r,g,b) vectors corresponding to the different clusters after meanshift
  22. labels=ms.labels_
  23.  
  24. #Remaining colors after
  25. cluster_centers = ms.cluster_centers_
  26.  
  27. #Finding and diplaying the number of clusters
  28. labels_unique = np.unique(labels)
  29. n_clusters_ = len(labels_unique)
  30. print("number of estimated clusters : %d" % n_clusters_)
  31.  
  32. #Displaying segmented image
  33. segmented_img = np.reshape(labels, original_shape[:2])
  34. cv2.imshow('Image',segmented_img)
  35. cv2.waitKey(0)
  36. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement