From 3ddd89068ff9580ea73976744bed19f7fb8da226 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 17 Sep 2016 13:58:07 +0000 Subject: [PATCH] - added kmeans clustering git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@313 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- beadDetect.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/beadDetect.py b/beadDetect.py index f1ddb32..829bde8 100755 --- a/beadDetect.py +++ b/beadDetect.py @@ -195,7 +195,7 @@ print("[INFO] Camera: approx. FPS: {:.2f}".format(vs.getfps().fps())) # Output stats findObjects.printStats() -# Scatter plot of colors +# Analyze colors numObjects = len(beadColors); blue = np.zeros(numObjects) green = np.zeros(numObjects) @@ -209,11 +209,24 @@ for color in beadColors: plotColors[i] = [red[i], green[i], blue[i]] i += 1 +# Kmeans create color classes +Z = np.float32(plotColors) + +# Define criteria = ( type, max_iter, epsilon ) +criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.1) +ret,label,center=cv2.kmeans(Z,10,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS) + +print (center) + +# Scatter plot of colors fig = plt.figure() ax = fig.add_subplot(111, projection='3d') -ax.scatter(blue, green, red, zdir='z', s=50, facecolors=plotColors, lw = 0, depthshade=True) +ax.scatter(blue, green, red, zdir='z', s=10, c=(0,0,0), lw = 0, depthshade=True) +ax.scatter(center[:,2], center[:,1], center[:,0], zdir='z', s=250, facecolors=center, lw = 2, depthshade=True) + plt.show() + # do a bit of cleanup cv2.destroyAllWindows()