diff --git a/PiVideoStream.py b/PiVideoStream.py index ff3478b..e7ad1d3 100755 --- a/PiVideoStream.py +++ b/PiVideoStream.py @@ -11,6 +11,27 @@ class PiVideoStream: self.camera = PiCamera() self.camera.resolution = resolution 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.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) @@ -22,6 +43,7 @@ class PiVideoStream: self.fps = None self.thread = None self.startup = True + def start(self): # start the thread to read frames from the video stream self.thread = Thread(target=self.update, args=()) @@ -39,7 +61,7 @@ class PiVideoStream: self.frame = f.array self.rawCapture.truncate(0) self.fps.update() - + # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: diff --git a/beadDetect.py b/beadDetect.py index e88368a..0e83b48 100755 --- a/beadDetect.py +++ b/beadDetect.py @@ -21,7 +21,20 @@ def colordistance(color1, color2): d2 = (color1[2]-color2[2]) 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(): def __init__(self): self.innerOuterRatio = 0.6 # const @@ -135,6 +148,7 @@ fps = FPS().start() findObjects = FindObjects() beadColors = [] +numColorClasses = 0 # loop over some frames...this time using the threaded stream while fps._numFrames < args["num_frames"]: @@ -144,14 +158,13 @@ while fps._numFrames < args["num_frames"]: else: 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_blurred = cv2.medianBlur(gray,5) # Canny edge detection 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 # 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) thickness = obj['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'] 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]}) + 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) else: img1_objects = cv2.rectangle(img1_objects,(x,y),(x+w,y+h),(0,0,255),2) @@ -199,9 +212,27 @@ while fps._numFrames < args["num_frames"]: for bead in beads: 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('Contours', img1_contours) + cv2.imshow('Contours', frame_dilated) cv2.imshow('Colors', img1_colors) cv2.imshow('Canny',img1_canny) cv2.imshow('Objects',img1_objects) @@ -237,9 +268,9 @@ 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 + blue[i] = color[0] + green[i] = color[1] + red[i] = color[2] plotColors[i] = [red[i], green[i], blue[i]] i += 1 @@ -275,3 +306,4 @@ plt.show() # do a bit of cleanup cv2.destroyAllWindows() +