Advertisement
davydovdmitry

Untitled

Nov 22nd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. import numpy as np
  2. from matplotlib.pyplot import imread
  3.  
  4. from kmeans_one import kmeans
  5. from kmeans_one import assign_clusters
  6.  
  7.  
  8. def rgb_to_h(image):
  9.     h = np.empty(shape=image.shape[0:2])
  10.     maxc = image.max(axis=2)
  11.     minc = image.min(axis=2)
  12.  
  13.     h[minc == maxc] = 0
  14.     rc = (maxc - image[:, :, 0]) / (maxc - minc)
  15.     gc = (maxc - image[:, :, 1]) / (maxc - minc)
  16.     bc = (maxc - image[:, :, 2]) / (maxc - minc)
  17.  
  18.     h[:, image[:, :, 0] == maxc] = bc[image[:, :, 0] == maxc] - gc[[image[:, :, 0] == maxc]]
  19.     h[:, image[:, :, 1] == maxc] = 2.0 + rc[image[:, :, 1] == maxc] - bc[image[:, :, 1] == maxc]
  20.     h[:, image[:, :, 2] == maxc] = 4.0 + gc[image[:, :, 2] == maxc] - rc[image[:, :, 2] == maxc]
  21.     h = (h / 6.0) % 1.0
  22.     return h
  23.  
  24.  
  25. if __name__ == '__main__':
  26.     in_image_label = "image/in.png"
  27.  
  28.     clusters_number = 3
  29.     image = imread(in_image_label)
  30.     out_image = rgb_to_h(image)
  31.  
  32.     clusters = kmeans(out_image[:, :].ravel(), clusters_number)
  33.     out_image[:, :] = assign_clusters(out_image[:, :].ravel(), clusters).reshape(out_image.shape)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement