- improved color detector
git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@311 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+49
-24
@@ -10,6 +10,8 @@ import imutils
|
|||||||
import time
|
import time
|
||||||
import cv2
|
import cv2
|
||||||
import pprint
|
import pprint
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
from mpl_toolkits.mplot3d import Axes3D
|
||||||
|
|
||||||
width = 320
|
width = 320
|
||||||
height = 240
|
height = 240
|
||||||
@@ -90,7 +92,6 @@ class FindObjects():
|
|||||||
|
|
||||||
objects.append({'thickness' : self.meanThickness, 'members' : obj})
|
objects.append({'thickness' : self.meanThickness, 'members' : obj})
|
||||||
|
|
||||||
# self.pp.pprint(objects)
|
|
||||||
return objects
|
return objects
|
||||||
|
|
||||||
def printStats(self):
|
def printStats(self):
|
||||||
@@ -114,20 +115,25 @@ time.sleep(2.0)
|
|||||||
fps = FPS().start()
|
fps = FPS().start()
|
||||||
findObjects = FindObjects()
|
findObjects = FindObjects()
|
||||||
|
|
||||||
|
beadColors = []
|
||||||
|
|
||||||
# loop over some frames...this time using the threaded stream
|
# loop over some frames...this time using the threaded stream
|
||||||
while fps._numFrames < args["num_frames"]:
|
while fps._numFrames < args["num_frames"]:
|
||||||
# grab the frame from the threaded video stream
|
# grab the frame from the threaded video stream
|
||||||
frame = vs.read()
|
frame = vs.read()
|
||||||
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
|
||||||
gray_bluured = cv2.medianBlur(gray,5)
|
gray_blurred = cv2.medianBlur(gray,5)
|
||||||
|
|
||||||
# Canny edge detection
|
# Canny edge detection
|
||||||
img1_canny = cv2.Canny(gray_bluured, 100, 50)
|
img1_canny = cv2.Canny(gray_blurred, 100, 50)
|
||||||
|
kernel = np.ones((1,1),np.uint8)
|
||||||
|
# img1_canny = cv2.blur(img1_canny,(3,3))
|
||||||
|
# img1_canny = cv2.dilate(img1_canny,kernel,iterations = 1)
|
||||||
|
|
||||||
# Thresholding
|
# Thresholding
|
||||||
# ret,img1_thr = cv2.threshold(gray1,120,255,cv2.THRESH_BINARY)
|
# ret,img1_thr = cv2.threshold(gray_blurred,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
|
||||||
# img1_thr = cv2.adaptiveThreshold(gray1,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
|
# img1_thr = cv2.adaptiveThreshold(gray_blurred,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
|
||||||
# img1_thr = cv2.adaptiveThreshold(gray1,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
|
# img1_thr = cv2.adaptiveThreshold(gray_blurred,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
|
||||||
|
|
||||||
# Contours
|
# Contours
|
||||||
(_, contours, hierachy) = cv2.findContours(img1_canny.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
|
(_, contours, hierachy) = cv2.findContours(img1_canny.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
|
||||||
@@ -138,8 +144,9 @@ while fps._numFrames < args["num_frames"]:
|
|||||||
|
|
||||||
img1_objects = frame.copy()
|
img1_objects = frame.copy()
|
||||||
img1_colors = np.zeros((height,width,3), np.uint8)
|
img1_colors = np.zeros((height,width,3), np.uint8)
|
||||||
maskCenter = 2
|
maskCenter = 4
|
||||||
roides = []
|
roides = []
|
||||||
|
beads = []
|
||||||
for obj in objects:
|
for obj in objects:
|
||||||
memberCount = 0
|
memberCount = 0
|
||||||
radius = 0
|
radius = 0
|
||||||
@@ -150,28 +157,26 @@ while fps._numFrames < args["num_frames"]:
|
|||||||
if member['isHole']:
|
if member['isHole']:
|
||||||
img1_objects = cv2.rectangle(img1_objects,(x,y),(x+w,y+h),(255,0,0),2)
|
img1_objects = cv2.rectangle(img1_objects,(x,y),(x+w,y+h),(255,0,0),2)
|
||||||
thickness = obj['thickness']-maskCenter
|
thickness = obj['thickness']-maskCenter
|
||||||
radius = int((member['diameter']+thickness+maskCenter)/2)
|
diameter = member['diameter']+thickness+maskCenter
|
||||||
roi = frame[y-thickness:y+2*radius, x-thickness:x+2*radius]
|
roi = frame[y-thickness:y+diameter, x-thickness:x+diameter]
|
||||||
roides.append({'roi' : roi, 'pos' : member['pos'], 'radius' : radius, 'thickness' : thickness})
|
pos = member['pos']
|
||||||
|
if diameter > 0:
|
||||||
|
mask = np.zeros((diameter,diameter,1), np.uint8)
|
||||||
|
mask = cv2.circle(mask,(int(diameter/2), int(diameter/2)),radius,255,thickness)
|
||||||
|
mean_color = cv2.mean(roi)
|
||||||
|
img1_colors = cv2.circle(img1_colors,(int(pos[0]),int(pos[1])),int(diameter/2),mean_color,-1)
|
||||||
|
beads.append({'pos' : pos, 'diameter' : diameter, 'color' : mean_color[0:3]})
|
||||||
|
img1_objects = cv2.circle(img1_objects,member['pos'],int(diameter/2),(255,255,255),thickness)
|
||||||
else:
|
else:
|
||||||
img1_objects = cv2.rectangle(img1_objects,(x,y),(x+w,y+h),(0,0,255),2)
|
img1_objects = cv2.rectangle(img1_objects,(x,y),(x+w,y+h),(0,0,255),2)
|
||||||
|
|
||||||
memberCount += 1
|
memberCount += 1
|
||||||
|
|
||||||
for r in roides:
|
for bead in beads:
|
||||||
if radius > 0:
|
beadColors.append(bead['color'])
|
||||||
radius = r['radius']
|
|
||||||
thickness = r['thickness']
|
|
||||||
roi = r['roi']
|
|
||||||
pos = r['pos']
|
|
||||||
mask = np.zeros((2*radius,2*radius,1), np.uint8)
|
|
||||||
mask = cv2.circle(mask,(radius, radius),radius,255,thickness)
|
|
||||||
mean_color = cv2.mean(roi)
|
|
||||||
center = (int(pos[0]),int(pos[1]))
|
|
||||||
img1_colors = cv2.circle(img1_colors,center,radius,mean_color,thickness)
|
|
||||||
|
|
||||||
cv2.imshow('detected colors', img1_colors)
|
|
||||||
# cv2.imshow('Thresholded',img1_thr)
|
# cv2.imshow('Thresholded',img1_thr)
|
||||||
|
cv2.imshow('detected colors', img1_colors)
|
||||||
cv2.imshow('Canny',img1_canny)
|
cv2.imshow('Canny',img1_canny)
|
||||||
cv2.imshow('Objects',img1_objects)
|
cv2.imshow('Objects',img1_objects)
|
||||||
cv2.waitKey(1)
|
cv2.waitKey(1)
|
||||||
@@ -179,8 +184,6 @@ while fps._numFrames < args["num_frames"]:
|
|||||||
# update the FPS counter
|
# update the FPS counter
|
||||||
fps.update()
|
fps.update()
|
||||||
|
|
||||||
findObjects.printStats()
|
|
||||||
|
|
||||||
# stop the timer and display FPS information
|
# stop the timer and display FPS information
|
||||||
fps.stop()
|
fps.stop()
|
||||||
vs.stop()
|
vs.stop()
|
||||||
@@ -189,6 +192,28 @@ print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
|
|||||||
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
|
||||||
|
findObjects.printStats()
|
||||||
|
|
||||||
|
# Scatter plot of colors
|
||||||
|
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]/255
|
||||||
|
green[i] = color[1]/255
|
||||||
|
red[i] = color[2]/255
|
||||||
|
plotColors[i] = [red[i], green[i], blue[i]]
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
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)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
# do a bit of cleanup
|
# do a bit of cleanup
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user