Skip to content

Commit 7676d21

Browse files
committed
Kmeans
1 parent 6b1b484 commit 7676d21

23 files changed

+332
-2
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,8 @@ code_105 | [(Detection Case)-HOG+SVM Predict](python/code_105/opencv_105.py) |
147147
code_106 | [AKAZE Features and Descriptors](python/code_106) | ✔️
148148
code_107 | [Brisk Features and Descriptors](python/code_107) | ✔️
149149
code_108 | [GFTT Detector](python/code_108) | ✔️
150-
code_109 | [BLOB Feature Analysis](python/code_109) | ✔️
150+
code_109 | [BLOB Feature Analysis](python/code_109) | ✔️
151+
code_110 | [KMeans Data Classification](python/code_110) | ✔️
152+
code_111 | [KMeans Image Segmentation](python/code_111) | ✔️
153+
code_112 | [KMeans Background Change](python/code_112) | ✔️
154+
code_113 | [KMeans Extract Image Color Card](python/code_113) | ✔️

README_CN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,8 @@ code_105 | [(检测案例)-HOG+SVM 预测](python/code_105/opencv_105.py) |
146146
code_106 | [AKAZE 特征与描述子](python/code_106) | ✔️
147147
code_107 | [Brisk 特征与描述子](python/code_107) | ✔️
148148
code_108 | [GFTT关键点检测](python/code_108) | ✔️
149-
code_109 | [BLOB 特征分析](python/code_109) | ✔️
149+
code_109 | [BLOB 特征分析](python/code_109) | ✔️
150+
code_110 | [KMeans 数据分类](python/code_110) | ✔️
151+
code_111 | [KMeans 图像分割](python/code_111) | ✔️
152+
code_112 | [KMeans 图像替换](python/code_112) | ✔️
153+
code_113 | [KMeans 图像色卡提取](python/code_113) | ✔️

python/code_110/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理-KMean图像处理](https://zhuanlan.zhihu.com/p/79518281)

python/code_110/opencv_110.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import numpy as np
2+
import cv2
3+
from matplotlib import pyplot as plt
4+
5+
# 读取数据
6+
def loadDataSet(fileName):
7+
data = []
8+
with open(fileName) as f:
9+
for line in f.readlines():
10+
curLine = line.strip().split("\t")
11+
fltLine = list(map(float, curLine)) # 转换为float
12+
data.append(fltLine)
13+
return np.array(data, dtype=np.float32)
14+
15+
# 导入数据
16+
data = loadDataSet('testSet2.txt')
17+
18+
# 定义停止条件
19+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
20+
21+
# kmeans计算
22+
ret,label,center=cv2.kmeans(data, 3, None, criteria, 2, cv2.KMEANS_RANDOM_CENTERS)
23+
24+
print(len(label))
25+
print(center)
26+
27+
# 获取不同标签的点
28+
A = data[label.ravel()==0]
29+
B = data[label.ravel()==1]
30+
C = data[label.ravel()==2]
31+
32+
# 可视化
33+
plt.scatter(A[:,0],A[:,1])
34+
plt.scatter(B[:,0],B[:,1],c = 'r')
35+
plt.scatter(C[:,0],C[:,1],c = 'purple')
36+
plt.scatter(center[:,0],center[:,1],s = 80,c = 'black', marker = '*')
37+
plt.xlabel('Height'),plt.ylabel('Weight')
38+
plt.show()

python/code_110/testSet.txt

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
1.658985 4.285136
2+
-3.453687 3.424321
3+
4.838138 -1.151539
4+
-5.379713 -3.362104
5+
0.972564 2.924086
6+
-3.567919 1.531611
7+
0.450614 -3.302219
8+
-3.487105 -1.724432
9+
2.668759 1.594842
10+
-3.156485 3.191137
11+
3.165506 -3.999838
12+
-2.786837 -3.099354
13+
4.208187 2.984927
14+
-2.123337 2.943366
15+
0.704199 -0.479481
16+
-0.392370 -3.963704
17+
2.831667 1.574018
18+
-0.790153 3.343144
19+
2.943496 -3.357075
20+
-3.195883 -2.283926
21+
2.336445 2.875106
22+
-1.786345 2.554248
23+
2.190101 -1.906020
24+
-3.403367 -2.778288
25+
1.778124 3.880832
26+
-1.688346 2.230267
27+
2.592976 -2.054368
28+
-4.007257 -3.207066
29+
2.257734 3.387564
30+
-2.679011 0.785119
31+
0.939512 -4.023563
32+
-3.674424 -2.261084
33+
2.046259 2.735279
34+
-3.189470 1.780269
35+
4.372646 -0.822248
36+
-2.579316 -3.497576
37+
1.889034 5.190400
38+
-0.798747 2.185588
39+
2.836520 -2.658556
40+
-3.837877 -3.253815
41+
2.096701 3.886007
42+
-2.709034 2.923887
43+
3.367037 -3.184789
44+
-2.121479 -4.232586
45+
2.329546 3.179764
46+
-3.284816 3.273099
47+
3.091414 -3.815232
48+
-3.762093 -2.432191
49+
3.542056 2.778832
50+
-1.736822 4.241041
51+
2.127073 -2.983680
52+
-4.323818 -3.938116
53+
3.792121 5.135768
54+
-4.786473 3.358547
55+
2.624081 -3.260715
56+
-4.009299 -2.978115
57+
2.493525 1.963710
58+
-2.513661 2.642162
59+
1.864375 -3.176309
60+
-3.171184 -3.572452
61+
2.894220 2.489128
62+
-2.562539 2.884438
63+
3.491078 -3.947487
64+
-2.565729 -2.012114
65+
3.332948 3.983102
66+
-1.616805 3.573188
67+
2.280615 -2.559444
68+
-2.651229 -3.103198
69+
2.321395 3.154987
70+
-1.685703 2.939697
71+
3.031012 -3.620252
72+
-4.599622 -2.185829
73+
4.196223 1.126677
74+
-2.133863 3.093686
75+
4.668892 -2.562705
76+
-2.793241 -2.149706
77+
2.884105 3.043438
78+
-2.967647 2.848696
79+
4.479332 -1.764772
80+
-4.905566 -2.911070

python/code_110/testSet2.txt

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
3.275154 2.957587
2+
-3.344465 2.603513
3+
0.355083 -3.376585
4+
1.852435 3.547351
5+
-2.078973 2.552013
6+
-0.993756 -0.884433
7+
2.682252 4.007573
8+
-3.087776 2.878713
9+
-1.565978 -1.256985
10+
2.441611 0.444826
11+
-0.659487 3.111284
12+
-0.459601 -2.618005
13+
2.177680 2.387793
14+
-2.920969 2.917485
15+
-0.028814 -4.168078
16+
3.625746 2.119041
17+
-3.912363 1.325108
18+
-0.551694 -2.814223
19+
2.855808 3.483301
20+
-3.594448 2.856651
21+
0.421993 -2.372646
22+
1.650821 3.407572
23+
-2.082902 3.384412
24+
-0.718809 -2.492514
25+
4.513623 3.841029
26+
-4.822011 4.607049
27+
-0.656297 -1.449872
28+
1.919901 4.439368
29+
-3.287749 3.918836
30+
-1.576936 -2.977622
31+
3.598143 1.975970
32+
-3.977329 4.900932
33+
-1.791080 -2.184517
34+
3.914654 3.559303
35+
-1.910108 4.166946
36+
-1.226597 -3.317889
37+
1.148946 3.345138
38+
-2.113864 3.548172
39+
0.845762 -3.589788
40+
2.629062 3.535831
41+
-1.640717 2.990517
42+
-1.881012 -2.485405
43+
4.606999 3.510312
44+
-4.366462 4.023316
45+
0.765015 -3.001270
46+
3.121904 2.173988
47+
-4.025139 4.652310
48+
-0.559558 -3.840539
49+
4.376754 4.863579
50+
-1.874308 4.032237
51+
-0.089337 -3.026809
52+
3.997787 2.518662
53+
-3.082978 2.884822
54+
0.845235 -3.454465
55+
1.327224 3.358778
56+
-2.889949 3.596178
57+
-0.966018 -2.839827
58+
2.960769 3.079555
59+
-3.275518 1.577068
60+
0.639276 -3.412840

python/code_111/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理-KMean图像处理](https://zhuanlan.zhihu.com/p/79518281)
87.9 KB
Loading

python/code_111/kmeans-image-demo.jpg

44.1 KB
Loading

python/code_111/opencv_111.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
import cv2 as cv
3+
4+
image = cv.imread('toux.jpg')
5+
6+
# 构建图像数据
7+
data = image.reshape((-1,3))
8+
data = np.float32(data)
9+
10+
# MAX_ITER最大迭代次数,EPS最高精度
11+
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)
12+
num_clusters = 4
13+
ret,label,center=cv.kmeans(data, num_clusters, None, criteria, num_clusters, cv.KMEANS_RANDOM_CENTERS)
14+
15+
center = np.uint8(center)
16+
17+
# 颜色label
18+
color = np.uint8([[255, 0, 0],
19+
[0, 0, 255],
20+
[128, 128, 128],
21+
[0, 255, 0]])
22+
23+
res = color[label.flatten()]
24+
print(res.shape)
25+
# 显示
26+
result = res.reshape((image.shape))
27+
cv.imshow('kmeans-image-demo',result)
28+
cv.imwrite('kmeans-image-demo-t.jpg',np.hstack((image,result)))
29+
30+
31+
cv.waitKey(0)
32+
cv.destroyAllWindows()

python/code_111/toux.jpg

21.2 KB
Loading

python/code_112/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理-KMean图像处理](https://zhuanlan.zhihu.com/p/79518281)

python/code_112/opencv_112.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import numpy as np
2+
import cv2 as cv
3+
4+
image = cv.imread('toux.jpg')
5+
cv.imshow("input", image)
6+
h, w ,ch = image.shape
7+
# 构建图像数据
8+
data = image.reshape((-1,3))
9+
data = np.float32(data)
10+
11+
# 图像分割
12+
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)
13+
num_clusters = 4
14+
ret,label,center=cv.kmeans(data, num_clusters, None, criteria, num_clusters, cv.KMEANS_RANDOM_CENTERS)
15+
16+
# 生成mask区域
17+
index = label[0][0]
18+
center = np.uint8(center)
19+
color = center[0]
20+
mask = np.ones((h, w), dtype=np.uint8)*255.
21+
label = np.reshape(label, (h, w))
22+
# alpha图
23+
mask[label == index] = 0
24+
25+
# 高斯模糊
26+
se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
27+
# 膨胀,防止背景出现
28+
cv.erode(mask, se, mask)
29+
#边缘模糊
30+
mask = cv.GaussianBlur(mask, (5, 5), 0)
31+
cv.imshow('background-mask',mask)
32+
33+
# 白色背景
34+
bg = np.ones(image.shape, dtype=np.float)*255.
35+
36+
# 粉丝背景
37+
purle = np.array([255, 0, 255])
38+
bg_color = np.tile(purle, (image.shape[0], image.shape[1], 1))
39+
40+
alpha = mask.astype(np.float32) / 255.
41+
fg = alpha[..., None] * image
42+
new_image = fg + (1 - alpha[..., None])*bg
43+
new_image_purle = fg + (1 - alpha[..., None])*bg_color
44+
45+
# # blend image
46+
# result = np.zeros((h, w, ch), dtype=np.uint8)
47+
# for row in range(h):
48+
# for col in range(w):
49+
# w1 = mask[row, col] / 255.0
50+
# b, g, r = image[row, col]
51+
# b = w1 * 255.0 + b * (1.0 - w1)
52+
# g = w1 * 0 + g * (1.0 - w1)
53+
# r = w1 * 255 + r * (1.0 - w1)
54+
# result[row, col] = (b, g, r)
55+
cv.imshow("background substitution", bg_color.astype(np.uint8))
56+
cv.imwrite("white.jpg", np.hstack((image, new_image.astype(np.uint8))))
57+
cv.imwrite("purle.jpg", np.hstack((image, new_image_purle.astype(np.uint8))))
58+
cv.waitKey(0)
59+
cv.destroyAllWindows()

python/code_112/purle.jpg

93.5 KB
Loading

python/code_112/toux.jpg

21.2 KB
Loading

python/code_112/white.jpg

92.9 KB
Loading

python/code_113/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理-KMean图像处理](https://zhuanlan.zhihu.com/p/79518281)

python/code_113/opencv_113.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
import cv2 as cv
3+
4+
image = cv.imread('result1.jpg')
5+
image2 = cv.imread('result2.jpg')
6+
img = cv.resize(image,(300,510))
7+
img2 = cv.resize(image2,(300,510))
8+
cv.imshow("input", img)
9+
cv.imwrite('result1_1.jpg',img)
10+
cv.imwrite('result2_2.jpg',img2)
11+
# h, w ,ch = image.shape
12+
# print(image.shape)
13+
# # 构建图像数据
14+
# data = image.reshape((-1,3))
15+
# data = np.float32(data)
16+
17+
# # 图像分割
18+
# criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)
19+
# num_clusters = 5
20+
# ret,label,center=cv.kmeans(data, num_clusters, None, criteria, num_clusters, cv.KMEANS_RANDOM_CENTERS)
21+
# print(label[300])
22+
23+
# # 生成主色彩条形卡片
24+
# card = np.zeros((50, w, 3), dtype=np.uint8)
25+
# clusters = np.zeros([5], dtype=np.int32)
26+
# # 统计每一类的数目
27+
# for i in range(len(label)):
28+
# clusters[label[i]] += 1
29+
# # 比重
30+
# clusters = np.float32(clusters) / float(h*w)
31+
# center = np.int32(center)
32+
# x_offset = 0
33+
34+
# # 绘制色卡
35+
# for c in range(num_clusters):
36+
# dx = np.int(clusters[c] * w)
37+
# b = center[c][0]
38+
# g = center[c][1]
39+
# r = center[c][2]
40+
# cv.rectangle(card, (x_offset, 0), (x_offset+dx, 50), (int(b), int(g), int(r)), -1)
41+
# x_offset += dx
42+
43+
# cv.imshow("color table", card)
44+
cv.waitKey(0)
45+
cv.destroyAllWindows()

python/code_113/result1_1.jpg

40.3 KB
Loading

python/code_113/result2_2.jpg

32.4 KB
Loading

python/code_113/yuan_test.png

630 KB
Loading

python/code_113/yuner.jpg

28.9 KB
Loading

python/code_113/yuner1.jpg

94.7 KB
Loading

0 commit comments

Comments
 (0)