- fixed runtime errors if no objects are present
git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@321 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+42
-38
@@ -62,7 +62,7 @@ class FindObjects():
|
|||||||
temp_objects = []
|
temp_objects = []
|
||||||
objectsCandIds = []
|
objectsCandIds = []
|
||||||
|
|
||||||
if hierachy.any():
|
if hierachy is not None:
|
||||||
while count != -1:
|
while count != -1:
|
||||||
family = FindObjects.getFamily([], count, hierachy)
|
family = FindObjects.getFamily([], count, hierachy)
|
||||||
|
|
||||||
@@ -294,51 +294,55 @@ if videoFile == "piCamera":
|
|||||||
print("[INFO] Camera : elasped time: {:.2f}".format(vs.getfps().elapsed()))
|
print("[INFO] Camera : elasped time: {:.2f}".format(vs.getfps().elapsed()))
|
||||||
print("[INFO] Camera: approx. FPS: {:.2f}".format(vs.getfps().fps()))
|
print("[INFO] Camera: approx. FPS: {:.2f}".format(vs.getfps().fps()))
|
||||||
|
|
||||||
# Output stats
|
if numColorClasses:
|
||||||
findObjects.printStats()
|
|
||||||
|
|
||||||
# Analyze colors
|
# Output stats
|
||||||
numObjects = len(beadColors);
|
findObjects.printStats()
|
||||||
blue = np.zeros(numObjects)
|
|
||||||
green = np.zeros(numObjects)
|
|
||||||
red = np.zeros(numObjects)
|
|
||||||
plotColors = np.zeros((numObjects,3))
|
|
||||||
i = 0
|
|
||||||
for color in beadColors:
|
|
||||||
blue[i] = color[0]
|
|
||||||
green[i] = color[1]
|
|
||||||
red[i] = color[2]
|
|
||||||
plotColors[i] = [red[i], green[i], blue[i]]
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
# Kmeans create color classes
|
# Analyze colors
|
||||||
Z = np.float32(plotColors)
|
numObjects = len(beadColors);
|
||||||
|
blue = np.zeros(numObjects)
|
||||||
|
green = np.zeros(numObjects)
|
||||||
|
red = np.zeros(numObjects)
|
||||||
|
plotColors = np.zeros((numObjects,3))
|
||||||
|
i = 0
|
||||||
|
for color in beadColors:
|
||||||
|
blue[i] = color[0]
|
||||||
|
green[i] = color[1]
|
||||||
|
red[i] = color[2]
|
||||||
|
plotColors[i] = [red[i], green[i], blue[i]]
|
||||||
|
i += 1
|
||||||
|
|
||||||
# Define criteria = ( type, max_iter, epsilon )
|
# Kmeans create color classes
|
||||||
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.1)
|
Z = np.float32(plotColors)
|
||||||
ret,label,center=cv2.kmeans(Z,numColorClasses,None,criteria,10,cv2.KMEANS_PP_CENTERS)
|
|
||||||
|
|
||||||
print (center)
|
# Define criteria = ( type, max_iter, epsilon )
|
||||||
|
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.1)
|
||||||
|
ret,label,center=cv2.kmeans(np.float32(plotColors), numColorClasses, None, criteria, 10, cv2.KMEANS_PP_CENTERS)
|
||||||
|
|
||||||
cnt1 = 0
|
print (center)
|
||||||
for c1 in center:
|
|
||||||
cnt2 = 0
|
|
||||||
for c2 in center:
|
|
||||||
print ("Color distance["+ str(cnt1) + "," + str(cnt2) + "] = " + str(colordistance(c1, c2)))
|
|
||||||
cnt2 += 1
|
|
||||||
cnt1 += 1
|
|
||||||
|
|
||||||
# Scatter plot of colors
|
cnt1 = 0
|
||||||
fig = plt.figure()
|
for c1 in center:
|
||||||
ax = fig.add_subplot(111, projection='3d')
|
cnt2 = 0
|
||||||
ax.set_xlabel('blue')
|
for c2 in center:
|
||||||
ax.set_ylabel('green')
|
print ("Color distance["+ str(cnt1) + "," + str(cnt2) + "] = " + str(colordistance(c1, c2)))
|
||||||
ax.set_zlabel('red')
|
cnt2 += 1
|
||||||
ax.scatter(blue, green, red, zdir='z', s=10, c=(0,0,0), lw = 0, depthshade=True)
|
cnt1 += 1
|
||||||
ax.scatter(center[:,2], center[:,1], center[:,0], zdir='z', s=500, facecolors=center, lw = 1, depthshade=True)
|
|
||||||
|
|
||||||
plt.show()
|
# Scatter plot of colors
|
||||||
|
fig = plt.figure()
|
||||||
|
ax = fig.add_subplot(111, projection='3d')
|
||||||
|
ax.set_xlabel('blue')
|
||||||
|
ax.set_ylabel('green')
|
||||||
|
ax.set_zlabel('red')
|
||||||
|
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=500, facecolors=center, lw = 1, depthshade=True)
|
||||||
|
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("Sorry, no colors have been detected")
|
||||||
|
|
||||||
# do a bit of cleanup
|
# do a bit of cleanup
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
|
|||||||
Reference in New Issue
Block a user