diff --git a/PiVideoStream.py b/PiVideoStream.py index e7ad1d3..185417f 100755 --- a/PiVideoStream.py +++ b/PiVideoStream.py @@ -12,25 +12,31 @@ class PiVideoStream: self.camera.resolution = resolution self.camera.framerate = framerate self.camera.exposure_mode = 'auto' +# self.camera.image_effect = 'colorbalance' +# self.camera.image_effect_params = (0,1,1,1,0,0) + 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)) + 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 ("exposure_compensation: " + str(self.camera.exposure_compensation)) + print ("shutter_speed : " + str(self.camera.shutter_speed)) + print ("meter_mode : " + str(self.camera.meter_mode)) + print ("color_effects : " + str(self.camera.color_effects)) + print ("image_effect : " + str(self.camera.image_effect)) + print ("image_effect_params : " + str(self.camera.image_effect_params)) + print ("sharpness : " + str(self.camera.sharpness)) self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, diff --git a/beadDetect.py b/beadDetect.py index 0e83b48..77de481 100755 --- a/beadDetect.py +++ b/beadDetect.py @@ -3,6 +3,7 @@ from __future__ import print_function from imutils.video import FPS +import sys import numpy as np import argparse import time @@ -83,7 +84,7 @@ class FindObjects(): perimeter = cv2.arcLength(contours[member['id']], True) pi = 3.14159265359 Q = 0 - if (perimeter > 0): + if (area > 10): Q = 4*pi*area/(perimeter*perimeter) if Q > 0.7: @@ -124,6 +125,8 @@ ap.add_argument("-r", "--framerate", type=int, default=30, help="# of frames to loop over for FPS test") ap.add_argument("-f", "--filename", type=str, default='piCamera', help="Whether or not frames should be displayed") +ap.add_argument("-c", "--calibrate", type=str, default='off', + help="Whether or not frames should be displayed") args = vars(ap.parse_args()) videoFile = args["filename"] @@ -135,7 +138,7 @@ if videoFile == "piCamera": from PiVideoStream import PiVideoStream from picamera.array import PiRGBArray from picamera import PiCamera - vs = PiVideoStream(resolution=(width,height), framerate=framerate).start() + vs = PiVideoStream(resolution=(2*width,2*height), framerate=framerate).start() time.sleep(5.0) else: vs = cv2.VideoCapture(videoFile) @@ -150,13 +153,62 @@ findObjects = FindObjects() beadColors = [] numColorClasses = 0 -# loop over some frames...this time using the threaded stream -while fps._numFrames < args["num_frames"]: - # grab the frame from the threaded video stream +if args["calibrate"] == 'on': + lens_corr = np.ones((height, width, 3), np.float32) if videoFile == "piCamera": frame = vs.read() else: ret, frame = vs.read() + + Z = np.float32(frame) + print (frame.dtype) + print (frame.shape) + print (lens_corr.dtype) + print (lens_corr.shape) + print (Z.dtype) + print (Z.shape) + ref_color = Z[height/2, width/2, :] + print (ref_color) + + for c in range (0, 3): + lens_corr[:,:,c] = cv2.divide(ref_color[c], Z[:,:,c]) + + params = list() + params.append(cv2.IMWRITE_PNG_COMPRESSION) + params.append(0) + + for c in range (0, 3): + lens_corr[:,:,c] = cv2.multiply(128, lens_corr[:,:,c]) + cv2.imwrite("lens_corr.png", np.uint8(lens_corr), params) + + sys.exit() + + +frame = np.ones((height, width, 3), np.float32) +lens_corr = np.float32(cv2.imread("lens_corr.png")) + +# loop over some frames...this time using the threaded stream +while fps._numFrames < args["num_frames"]: + # grab the frame from the threaded video stream + + if videoFile == "piCamera": + frame8 = vs.read()[int(height/2):int(height/2+height), int(width/2):int(width/2)+width] + else: + ret, frame8 = vs.read() + + if 0: + frame32 = np.float32(frame8) + print (frame.dtype) + print (frame.shape) + print (lens_corr.dtype) + print (lens_corr.shape) + + for c in range (0, 3): + frame32[:,:,c] = cv2.multiply(lens_corr[:,:,c], frame32[:,:,c], 1.0/255) + + frame = np.uint8(frame32) + else: + frame = frame8 kernel = np.ones((3,3),np.uint8) frame_dilated = cv2.dilate(frame,kernel,iterations = 1) @@ -231,7 +283,9 @@ while fps._numFrames < args["num_frames"]: numColorClasses = len(colorClasses) print("Found " + str(numColorClasses) + " color classes") -# cv2.imshow('Thresholded',img1_thr) + + cv2.imshow('Raw',frame8) + cv2.imshow('Corrected', frame) cv2.imshow('Contours', frame_dilated) cv2.imshow('Colors', img1_colors) cv2.imshow('Canny',img1_canny) @@ -279,7 +333,7 @@ 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_PP_CENTERS) +ret,label,center=cv2.kmeans(Z,numColorClasses,None,criteria,10,cv2.KMEANS_PP_CENTERS) print (center)