-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmean.py
47 lines (34 loc) · 1.1 KB
/
kmean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import cv2
import numpy as np
def main():
w = 160
h = 120
cap = cv2.VideoCapture('testx.avi')
cap.set(3, w)
cap.set(4, h)
if cap.isOpened():
ret, frame = cap.read()
else:
ret = False
while ret:
ret, frame = cap.read()
frame = cv2.resize(frame, (540, 540), interpolation = cv2.INTER_LINEAR)
(channel_b, channel_g, channel_r) = cv2.split(frame)
frame = channel_r
Z = frame.reshape((-1,3))
Z = np.float32(Z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K=2
ret, label1, center1 = cv2.kmeans(Z, K, None,
criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
center1 = np.uint8(center1)
res1 = center1[label1.flatten()]
output1 = res1.reshape((frame.shape))
cv2.imshow("Original", frame)
cv2.imshow("Quantized", output1)
if cv2.waitKey(1) == 27: # exit on ESC
break
cv2.destroyAllWindows()
cap.release()
#if __name__ == "__main__":
main()