teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

追記

2018/09/20 08:34

投稿

trafalbad
trafalbad

スコア303

title CHANGED
File without changes
body CHANGED
@@ -5,24 +5,48 @@
5
5
  を使うと、クラスタリングで色を取得したあとになぜかRGBAに変換されてしまうので、
6
6
  RGB値を直接LAB値に変換するにはどのようにすればいいのでしょうか?
7
7
 
8
+ #追記
9
+
10
+ 以下のk-meansでlabの値を求めたのですが、LABは0〜100の間の値しか取らないのに、100を超えてしまいます。これはlABではないのでしょうか?
8
11
  ```python
9
-
10
12
  def load_image(image_file):
11
13
  # cv2 load images as BGR, convert it to LAB
12
14
  image_bgr = cv2.imread(image_file)
13
- image_Lab = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
15
+ image_lab = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2LAB)
14
- if image_Lab is not None:
15
- image_Lab = cv2.resize(image_Lab, (150, 150), interpolation=cv2.INTER_CUBIC)
16
+ image_lab = cv2.resize(image_lab, (150, 150))
16
- return image_Lab
17
+ return image_lab
17
18
 
18
19
 
20
+ def centroid_histogram(clt):
21
+ numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
22
+ (hist, _) = np.histogram(clt.labels_, bins = numLabels)
19
- img = load_image("210.jpg")
23
+ hist = hist.astype("float")
20
- w,h,c=img.shape
24
+ hist /= hist.sum()
21
- # 画像縮小
25
+ return hist
26
+ def plot_colors(hist, centroids):
27
+ bar = np.zeros((50, 300, 3), dtype = "uint8")
28
+ startX = 0
29
+ for (percent, color) in zip(hist, centroids):
30
+ endX = startX + (percent * 300)
22
- img=img[int(w/10):int(9*w/10), int(h/10):int(9*h/10)]
31
+ cv2.rectangle(bar, (int(startX), 0), (int(endX), 50),
32
+ color.astype("uint8").tolist(), -1)
33
+ startX = endX
23
- img[3:3]
34
+ return bar
24
35
 
36
+ image = image.reshape(-1,3)
37
+ clt = KMeans(n_clusters =3)
38
+ clt.fit(image)
39
+ hist = centroid_histogram(clt)
25
- >>array([], shape=(0, 120, 3), dtype=uint8) # RGB
40
+ bar = plot_colors(hist, clt.cluster_centers_)
41
+
26
- # => これをLABに変換したい
42
+ # show our color bart
43
+ plt.figure()
44
+ plt.axis("off")
45
+ plt.imshow(bar)
46
+ plt.show()
27
47
 
48
+ resized = cv2.resize(bar, (bar.shape[0], bar.shape[1]))
49
+ ch_imgs = cv2.split(resized)
50
+ ch_imgs
51
+ >>>136,144, 145
28
52
  ```