def find_label_clusters(kitti_base, kittiLabels, shape, num_clusters, descriptors=None):
if descriptors is None:
progressbar = ProgressBar('Computing descriptors', max=len(kittiLabels))
descriptors = []
for label in kittiLabels:
progressbar.next()
img = getCroppedSampleFromLabel(kitti_base, label)
# img = cv2.resize(img, (shape[1], shape[0]), interpolation=cv2.INTER_AREA)
img = resizeSample(img, shape, label)
hist = get_hog(img)
descriptors.append(hist)
progressbar.finish()
else:
print 'find_label_clusters,', 'Using supplied descriptors.'
print len(kittiLabels), len(descriptors)
assert(len(kittiLabels) == len(descriptors))
# X = np.random.randint(25,50,(25,2))
# Y = np.random.randint(60,85,(25,2))
# Z = np.vstack((X,Y))
# convert to np.float32
Z = np.float32(descriptors)
# define criteria and apply kmeans()
K = num_clusters
print 'find_label_clusters,', 'kmeans:', K
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
attempts = 10
ret,label,center=cv2.kmeans(Z,K,None,criteria,attempts,cv2.KMEANS_RANDOM_CENTERS)
# ret,label,center=cv2.kmeans(Z,2,criteria,attempts,cv2.KMEANS_PP_CENTERS)
print 'ret:', ret
# print 'label:', label
# print 'center:', center
# # Now separate the data, Note the flatten()
# A = Z[label.ravel()==0]
# B = Z[label.ravel()==1]
clusters = partition(kittiLabels, label)
return clusters
# # Plot the data
# from matplotlib import pyplot as plt
# plt.scatter(A[:,0],A[:,1])
# plt.scatter(B[:,0],B[:,1],c = 'r')
# plt.scatter(center[:,0],center[:,1],s = 80,c = 'y', marker = 's')
# plt.xlabel('Height'),plt.ylabel('Weight')
# plt.show()
评论列表
文章目录