Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. import cv2
  2. from skimage import segmentation
  3. from skimage.util import img_as_float
  4. from matplotlib import pyplot as plt
  5. import numpy as np
  6.  
  7. image = cv2.imread("test_img.jpg")
  8.  
  9. segments = segmentation.slic(img_as_float(image), n_segments=300, compactness=20, sigma=10)
  10. segments += 1
  11. segment_ids = np.unique(segments)
  12. fig = plt.figure("Superpiksele")
  13. ax = fig.add_subplot(1, 1, 1)
  14. ax.imshow(segmentation.mark_boundaries(img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), segments))
  15. plt.axis("off")
  16. plt.show()
  17. def get_color_histogram(image, superpixels, index):
  18. indices = np.where(superpixels.ravel() == index)[0]
  19. r_hist = np.bincount(image[:, :, 0].ravel()[indices], minlength=256)
  20. g_hist = np.bincount(image[:, :, 1].ravel()[indices], minlength=256)
  21. b_hist = np.bincount(image[:, :, 2].ravel()[indices], minlength=256)
  22. histogram = (r_hist + g_hist + b_hist) / 3
  23. cv2.imshow(" ", histogram)
  24. cv2.waitKey(0)
  25. cv2.destroyAllWindows()
  26. return histogram
  27. get_color_histogram(image, segments, segment_ids)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement