원본 : https://github.com/sabin5105/openCV-tutorial
소스코드
""" plot_3d.py: Plotting the data distribution into 3 dimensions.
Quick tutorial on how to use kmeans with openCV.
__author__: sabin lee
__license__: MIT
__version__: 0.1
"""
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate random 3D points
X = np.random.randint(25,50,(25,3))
Y = np.random.randint(60,85,(25,3))
Z = np.random.randint(100,150,(25,3))
L = np.vstack((X,Y,Z))
# convert to np.float32
L = np.float32(L)
K = 3
# define criteria and apply kmeans()
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret,label,center=cv.kmeans(L,K,None,criteria,10,cv.KMEANS_RANDOM_CENTERS)
# Now separate the data, Note the flatten()
A = L[label.ravel()==0]
B = L[label.ravel()==1]
C = L[label.ravel()==2]
# plot into 3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(A[:,0], A[:,1], A[:,2], label='Cluster 1')
ax.scatter(B[:,0], B[:,1], B[:,2], label='Cluster 2')
ax.scatter(C[:,0], C[:,1], C[:,2], label='Cluster 3')
ax.scatter(center[:,0], center[:,1], center[:,2], s=80, c='y', marker='s', label='Centroids')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()
결과
반응형
'MACHINE LEARNING' 카테고리의 다른 글
통계 / Chi-Squared distribution VS Poisson distribution (멍청일기) (0) | 2023.03.23 |
---|---|
openCV / image blur 처리 예제 (0) | 2023.03.15 |
Approximate inference 정리 (0) | 2023.02.21 |
Bias-Variance tradeoff 관계 해석 (0) | 2023.02.19 |
Word2Vec 개념 정리 (0) | 2022.12.30 |