質問編集履歴

1

追記

2018/09/20 08:34

投稿

trafalbad
trafalbad

スコア303

test CHANGED
File without changes
test CHANGED
@@ -12,9 +12,13 @@
12
12
 
13
13
 
14
14
 
15
- ```python
15
+ #追記
16
16
 
17
17
 
18
+
19
+ 以下のk-meansでlabの値を求めたのですが、LABは0〜100の間の値しか取らないのに、100を超えてしまいます。これはlABではないのでしょうか?
20
+
21
+ ```python
18
22
 
19
23
  def load_image(image_file):
20
24
 
@@ -22,34 +26,78 @@
22
26
 
23
27
  image_bgr = cv2.imread(image_file)
24
28
 
25
- image_Lab = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
29
+ image_lab = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2LAB)
26
30
 
27
- if image_Lab is not None:
31
+ image_lab = cv2.resize(image_lab, (150, 150))
28
32
 
29
- image_Lab = cv2.resize(image_Lab, (150, 150), interpolation=cv2.INTER_CUBIC)
30
-
31
- return image_Lab
33
+ return image_lab
32
34
 
33
35
 
34
36
 
35
37
 
36
38
 
37
- img = load_image("210.jpg")
39
+ def centroid_histogram(clt):
38
40
 
39
- w,h,c=img.shape
41
+ numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
40
42
 
41
- # 画像縮小
43
+ (hist, _) = np.histogram(clt.labels_, bins = numLabels)
42
44
 
43
- img=img[int(w/10):int(9*w/10), int(h/10):int(9*h/10)]
45
+ hist = hist.astype("float")
44
46
 
47
+ hist /= hist.sum()
48
+
49
+ return hist
50
+
51
+ def plot_colors(hist, centroids):
52
+
53
+ bar = np.zeros((50, 300, 3), dtype = "uint8")
54
+
45
- img[3:3]
55
+ startX = 0
56
+
57
+ for (percent, color) in zip(hist, centroids):
58
+
59
+ endX = startX + (percent * 300)
60
+
61
+ cv2.rectangle(bar, (int(startX), 0), (int(endX), 50),
62
+
63
+ color.astype("uint8").tolist(), -1)
64
+
65
+ startX = endX
66
+
67
+ return bar
46
68
 
47
69
 
48
70
 
49
- >>array([], shape=(0, 120, 3), dtype=uint8) # RGB
71
+ image = image.reshape(-1,3)
50
72
 
73
+ clt = KMeans(n_clusters =3)
74
+
75
+ clt.fit(image)
76
+
77
+ hist = centroid_histogram(clt)
78
+
79
+ bar = plot_colors(hist, clt.cluster_centers_)
80
+
81
+
82
+
51
- # => これをLABに変換したい
83
+ # show our color bart
84
+
85
+ plt.figure()
86
+
87
+ plt.axis("off")
88
+
89
+ plt.imshow(bar)
90
+
91
+ plt.show()
52
92
 
53
93
 
54
94
 
95
+ resized = cv2.resize(bar, (bar.shape[0], bar.shape[1]))
96
+
97
+ ch_imgs = cv2.split(resized)
98
+
99
+ ch_imgs
100
+
101
+ >>>136,144, 145
102
+
55
103
  ```