- use online color classification

- show camera settings



git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@318 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2016-09-18 16:06:42 +00:00
parent b7e833bbc4
commit 92082e5e5e
2 changed files with 66 additions and 12 deletions
+22
View File
@@ -11,6 +11,27 @@ class PiVideoStream:
self.camera = PiCamera() self.camera = PiCamera()
self.camera.resolution = resolution self.camera.resolution = resolution
self.camera.framerate = framerate self.camera.framerate = framerate
self.camera.exposure_mode = 'auto'
awb_gains = self.camera.awb_gains
print ("sensor_mode : " + str(self.camera.sensor_mode))
print ("resolution : " + str(self.camera.resolution))
print ("framerate : " + str(self.camera.framerate))
print ("awb_mode : " + str(self.camera.awb_mode))
print ("awb_gains : " + str((float(awb_gains[0]), float(awb_gains[1]))))
print ("analog_gain : " + str(float(self.camera.analog_gain)))
print ("digital_gain : " + str(float(self.camera.digital_gain)))
print ("iso : " + str(self.camera.iso))
print ("brightness : " + str(self.camera.brightness))
print ("contrast : " + str(self.camera.contrast))
print ("saturation : " + str(self.camera.saturation))
print ("exposure_mode : " + str(self.camera.exposure_mode))
print ("exposure_speed: " + str(self.camera.exposure_speed))
print ("shutter_speed : " + str(self.camera.shutter_speed))
print ("meter_mode : " + str(self.camera.meter_mode))
print ("image_effect : " + str(self.camera.image_effect))
print ("sharpness : " + str(self.camera.sharpness))
self.rawCapture = PiRGBArray(self.camera, size=resolution) self.rawCapture = PiRGBArray(self.camera, size=resolution)
self.stream = self.camera.capture_continuous(self.rawCapture, self.stream = self.camera.capture_continuous(self.rawCapture,
format="bgr", use_video_port=True) format="bgr", use_video_port=True)
@@ -22,6 +43,7 @@ class PiVideoStream:
self.fps = None self.fps = None
self.thread = None self.thread = None
self.startup = True self.startup = True
def start(self): def start(self):
# start the thread to read frames from the video stream # start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=()) self.thread = Thread(target=self.update, args=())
+41 -9
View File
@@ -21,6 +21,19 @@ def colordistance(color1, color2):
d2 = (color1[2]-color2[2]) d2 = (color1[2]-color2[2])
return np.math.sqrt(d0*d0 + d1*d1 + d2*d2) return np.math.sqrt(d0*d0 + d1*d1 + d2*d2)
def mindistance(colorList, color):
minDist = 1000
minIndex = 0
index = 0
for c in colorList:
dist = colordistance(c, color)
if dist < minDist:
minDist = dist
minIndex = index
index += 1
return (minDist, minIndex)
class FindObjects(): class FindObjects():
def __init__(self): def __init__(self):
@@ -135,6 +148,7 @@ fps = FPS().start()
findObjects = FindObjects() findObjects = FindObjects()
beadColors = [] beadColors = []
numColorClasses = 0
# 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"]:
@@ -144,14 +158,13 @@ while fps._numFrames < args["num_frames"]:
else: else:
ret, frame = vs.read() ret, frame = vs.read()
kernel = np.ones((3,3),np.uint8)
frame_dilated = cv2.dilate(frame,kernel,iterations = 1)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray_blurred = cv2.medianBlur(gray,5) gray_blurred = cv2.medianBlur(gray,5)
# Canny edge detection # Canny edge detection
img1_canny = cv2.Canny(gray_blurred, 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(gray_blurred,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) # ret,img1_thr = cv2.threshold(gray_blurred,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
@@ -183,14 +196,14 @@ while fps._numFrames < args["num_frames"]:
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
diameter = member['diameter']+thickness+maskCenter diameter = member['diameter']+thickness+maskCenter
roi = frame[y-thickness:y+diameter, x-thickness:x+diameter] roi = frame_dilated[y-thickness:y+diameter, x-thickness:x+diameter]
pos = member['pos'] pos = member['pos']
if diameter > 0: if diameter > 0:
mask = np.zeros((diameter,diameter,1), np.uint8) mask = np.zeros((diameter,diameter,1), np.uint8)
mask = cv2.circle(mask,(int(diameter/2), int(diameter/2)),radius,255,thickness) mask = cv2.circle(mask,(int(diameter/2), int(diameter/2)),radius,255,thickness)
mean_color = cv2.mean(roi) mean_color = cv2.mean(roi)
img1_colors = cv2.circle(img1_colors,(int(pos[0]),int(pos[1])),int(diameter/2),mean_color,-1) 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]}) beads.append({'pos' : pos, 'diameter' : diameter, 'color' : [mean_color[0]/255, mean_color[1]/255, mean_color[2]/255]})
# img1_objects = cv2.circle(img1_objects,member['pos'],int(diameter/2),(255,255,255),thickness) # 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)
@@ -200,8 +213,26 @@ while fps._numFrames < args["num_frames"]:
for bead in beads: for bead in beads:
beadColors.append(bead['color']) beadColors.append(bead['color'])
colorClasses = []
for bead in beads:
if not colorClasses:
colorClasses.append(bead['color'])
else:
d, i = mindistance(colorClasses, bead['color'])
if d > 0.14:
colorClasses.append(bead['color'])
else:
colorClasses[i][0] = 0.5*colorClasses[i][0] + 0.5*bead['color'][0]
colorClasses[i][1] = 0.5*colorClasses[i][1] + 0.5*bead['color'][1]
colorClasses[i][2] = 0.5*colorClasses[i][2] + 0.5*bead['color'][2]
if numColorClasses != len(colorClasses):
numColorClasses = len(colorClasses)
print("Found " + str(numColorClasses) + " color classes")
# cv2.imshow('Thresholded',img1_thr) # cv2.imshow('Thresholded',img1_thr)
cv2.imshow('Contours', img1_contours) cv2.imshow('Contours', frame_dilated)
cv2.imshow('Colors', img1_colors) cv2.imshow('Colors', img1_colors)
cv2.imshow('Canny',img1_canny) cv2.imshow('Canny',img1_canny)
cv2.imshow('Objects',img1_objects) cv2.imshow('Objects',img1_objects)
@@ -237,9 +268,9 @@ red = np.zeros(numObjects)
plotColors = np.zeros((numObjects,3)) plotColors = np.zeros((numObjects,3))
i = 0 i = 0
for color in beadColors: for color in beadColors:
blue[i] = color[0]/255 blue[i] = color[0]
green[i] = color[1]/255 green[i] = color[1]
red[i] = color[2]/255 red[i] = color[2]
plotColors[i] = [red[i], green[i], blue[i]] plotColors[i] = [red[i], green[i], blue[i]]
i += 1 i += 1
@@ -275,3 +306,4 @@ plt.show()
# do a bit of cleanup # do a bit of cleanup
cv2.destroyAllWindows() cv2.destroyAllWindows()