Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- from skimage import segmentation
- from skimage.util import img_as_float
- from matplotlib import pyplot as plt
- import numpy as np
- image = cv2.imread("test_img.jpg")
- segments = segmentation.slic(img_as_float(image), n_segments=300, compactness=20, sigma=10)
- segments += 1
- segment_ids = np.unique(segments)
- fig = plt.figure("Superpiksele")
- ax = fig.add_subplot(1, 1, 1)
- ax.imshow(segmentation.mark_boundaries(img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), segments))
- plt.axis("off")
- plt.show()
- def get_color_histogram(image, superpixels, index):
- indices = np.where(superpixels.ravel() == index)[0]
- r_hist = np.bincount(image[:, :, 0].ravel()[indices], minlength=256)
- g_hist = np.bincount(image[:, :, 1].ravel()[indices], minlength=256)
- b_hist = np.bincount(image[:, :, 2].ravel()[indices], minlength=256)
- histogram = (r_hist + g_hist + b_hist) / 3
- cv2.imshow(" ", histogram)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- return histogram
- get_color_histogram(image, segments, segment_ids)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement