- improved
git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@323 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+21
-12
@@ -14,8 +14,24 @@ class PiVideoStream:
|
|||||||
self.camera.exposure_mode = 'auto'
|
self.camera.exposure_mode = 'auto'
|
||||||
# self.camera.image_effect = 'colorbalance'
|
# self.camera.image_effect = 'colorbalance'
|
||||||
# self.camera.image_effect_params = (0,1,1,1,0,0)
|
# self.camera.image_effect_params = (0,1,1,1,0,0)
|
||||||
|
self.camera.exposure_mode = 'auto'
|
||||||
|
self.camera.awb_mode = 'off'
|
||||||
|
self.camera.awb_gains = (1.5, 1.5)
|
||||||
|
# self.camera.analog_gain = 1.75
|
||||||
|
# self.camera.digital_gain = 1.00
|
||||||
|
self.rawCapture = PiRGBArray(self.camera, size=resolution)
|
||||||
|
self.stream = self.camera.capture_continuous(self.rawCapture,
|
||||||
|
format="bgr", use_video_port=True)
|
||||||
|
|
||||||
|
# initialize the frame and the variable used to indicate
|
||||||
|
# if the thread should be stopped
|
||||||
|
self.frame = None
|
||||||
|
self.stopped = False
|
||||||
|
self.fps = None
|
||||||
|
self.thread = None
|
||||||
|
self.startup = True
|
||||||
|
|
||||||
|
def printSettings(self):
|
||||||
awb_gains = self.camera.awb_gains
|
awb_gains = self.camera.awb_gains
|
||||||
print ("sensor_mode : " + str(self.camera.sensor_mode))
|
print ("sensor_mode : " + str(self.camera.sensor_mode))
|
||||||
print ("resolution : " + str(self.camera.resolution))
|
print ("resolution : " + str(self.camera.resolution))
|
||||||
@@ -38,18 +54,6 @@ class PiVideoStream:
|
|||||||
print ("image_effect_params : " + str(self.camera.image_effect_params))
|
print ("image_effect_params : " + str(self.camera.image_effect_params))
|
||||||
print ("sharpness : " + str(self.camera.sharpness))
|
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)
|
|
||||||
|
|
||||||
# initialize the frame and the variable used to indicate
|
|
||||||
# if the thread should be stopped
|
|
||||||
self.frame = None
|
|
||||||
self.stopped = False
|
|
||||||
self.fps = None
|
|
||||||
self.thread = None
|
|
||||||
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=())
|
||||||
@@ -59,6 +63,11 @@ class PiVideoStream:
|
|||||||
def update(self):
|
def update(self):
|
||||||
# keep looping infinitely until the thread is stopped
|
# keep looping infinitely until the thread is stopped
|
||||||
for f in self.stream:
|
for f in self.stream:
|
||||||
|
if self.camera.exposure_mode == 'auto':
|
||||||
|
if self.camera.analog_gain > 1.75 and self.camera.digital_gain == 1.0:
|
||||||
|
self.camera.exposure_mode = 'off'
|
||||||
|
self.printSettings()
|
||||||
|
|
||||||
if self.startup:
|
if self.startup:
|
||||||
self.fps = FPS().start()
|
self.fps = FPS().start()
|
||||||
self.startup = False
|
self.startup = False
|
||||||
|
|||||||
+152
-151
@@ -62,55 +62,58 @@ class FindObjects():
|
|||||||
temp_objects = []
|
temp_objects = []
|
||||||
objectsCandIds = []
|
objectsCandIds = []
|
||||||
|
|
||||||
if hierachy is not None:
|
try:
|
||||||
while count != -1:
|
if hierachy.any():
|
||||||
family = FindObjects.getFamily([], count, hierachy)
|
while count != -1:
|
||||||
|
family = FindObjects.getFamily([], count, hierachy)
|
||||||
|
|
||||||
dupDict = {}
|
dupDict = {}
|
||||||
members = []
|
members = []
|
||||||
for member in family:
|
for member in family:
|
||||||
x,y,w,h = cv2.boundingRect(contours[member]);
|
x,y,w,h = cv2.boundingRect(contours[member]);
|
||||||
key = str([x,y,w,h]) + '.key'
|
key = str([x,y,w,h]) + '.key'
|
||||||
if not key in dupDict:
|
if not key in dupDict:
|
||||||
dupDict[key] = member
|
dupDict[key] = member
|
||||||
members.append({ 'id' : member, 'bbox' : [x,y,w,h]})
|
members.append({ 'id' : member, 'bbox' : [x,y,w,h]})
|
||||||
|
|
||||||
objectsCandIds.append(members)
|
objectsCandIds.append(members)
|
||||||
|
|
||||||
count = hierachy[0][count][0]
|
count = hierachy[0][count][0]
|
||||||
|
|
||||||
for family in objectsCandIds:
|
for family in objectsCandIds:
|
||||||
obj = []
|
obj = []
|
||||||
for member in family:
|
for member in family:
|
||||||
area = cv2.contourArea(contours[member['id']])
|
area = cv2.contourArea(contours[member['id']])
|
||||||
perimeter = cv2.arcLength(contours[member['id']], True)
|
perimeter = cv2.arcLength(contours[member['id']], True)
|
||||||
pi = 3.14159265359
|
pi = 3.14159265359
|
||||||
Q = 0
|
Q = 0
|
||||||
if (area > 10):
|
if (area > 10):
|
||||||
Q = 4*pi*area/(perimeter*perimeter)
|
Q = 4*pi*area/(perimeter*perimeter)
|
||||||
|
|
||||||
if Q > 0.7:
|
if Q > 0.7:
|
||||||
diameter = max(member['bbox'][2], member['bbox'][3])
|
diameter = max(member['bbox'][2], member['bbox'][3])
|
||||||
self.maxDiameter = max(self.maxDiameter, diameter)
|
self.maxDiameter = max(self.maxDiameter, diameter)
|
||||||
self.minDiameter = min(self.minDiameter, diameter)
|
self.minDiameter = min(self.minDiameter, diameter)
|
||||||
member['diameter'] = diameter
|
member['diameter'] = diameter
|
||||||
member['pos'] = (int(member['bbox'][0] + member['bbox'][2]/2), int(member['bbox'][1] + member['bbox'][3]/2))
|
member['pos'] = (int(member['bbox'][0] + member['bbox'][2]/2), int(member['bbox'][1] + member['bbox'][3]/2))
|
||||||
obj.append(member)
|
obj.append(member)
|
||||||
|
|
||||||
if obj:
|
if obj:
|
||||||
temp_objects.append(obj)
|
temp_objects.append(obj)
|
||||||
|
|
||||||
for obj in temp_objects:
|
for obj in temp_objects:
|
||||||
for member in obj:
|
for member in obj:
|
||||||
if member['diameter'] < self.innerOuterRatio*self.maxDiameter:
|
if member['diameter'] < self.innerOuterRatio*self.maxDiameter:
|
||||||
member['isHole'] = True
|
member['isHole'] = True
|
||||||
else:
|
else:
|
||||||
member['isHole'] = False
|
member['isHole'] = False
|
||||||
|
|
||||||
if len(obj) == 2:
|
if len(obj) == 2:
|
||||||
self.meanThickness = int(abs(obj[0]['diameter'] - obj[1]['diameter'])/2)
|
self.meanThickness = int(abs(obj[0]['diameter'] - obj[1]['diameter'])/2)
|
||||||
|
|
||||||
objects.append({'thickness' : self.meanThickness, 'members' : obj})
|
objects.append({'thickness' : self.meanThickness, 'members' : obj})
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
return objects
|
return objects
|
||||||
|
|
||||||
@@ -175,126 +178,127 @@ if args["calibrate"] == 'on':
|
|||||||
params.append(0)
|
params.append(0)
|
||||||
|
|
||||||
cv2.imwrite("lens_corr.png", np.uint8(lens_corr*127.0), params)
|
cv2.imwrite("lens_corr.png", np.uint8(lens_corr*127.0), params)
|
||||||
|
cv2.imwrite("lens_shot.png", frame, params)
|
||||||
|
|
||||||
sys.exit()
|
fps.stop()
|
||||||
|
|
||||||
|
|
||||||
frame = np.ones((height, width, 3), np.float32)
|
|
||||||
lens_corr = np.float32(cv2.imread("lens_corr.png"))/140.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 videoFile == "piCamera":
|
if videoFile == "piCamera":
|
||||||
frame = vs.read()
|
vs.stop()
|
||||||
else:
|
|
||||||
ret, frame = vs.read()
|
|
||||||
|
|
||||||
if args["use_calibration"] == 'on':
|
else:
|
||||||
frame = np.uint8(cv2.multiply(lens_corr, np.float32(frame)))
|
frame = np.ones((height, width, 3), np.float32)
|
||||||
|
lens_corr = np.float32(cv2.imread("lens_corr.png"))/128.0
|
||||||
|
|
||||||
kernel = np.ones((3,3),np.uint8)
|
# loop over some frames...this time using the threaded stream
|
||||||
frame_dilated = cv2.dilate(frame,kernel,iterations = 1)
|
while fps._numFrames < args["num_frames"]:
|
||||||
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
|
# grab the frame from the threaded video stream
|
||||||
gray_blurred = cv2.medianBlur(gray,5)
|
|
||||||
|
|
||||||
# Canny edge detection
|
if videoFile == "piCamera":
|
||||||
img1_canny = cv2.Canny(gray_blurred, 100, 50)
|
frame = vs.read()
|
||||||
|
|
||||||
# Thresholding
|
|
||||||
# ret,img1_thr = cv2.threshold(gray_blurred,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
|
|
||||||
# img1_thr = cv2.adaptiveThreshold(gray_blurred,255,cv2.ADAPTIVE_THRESH_MEAN_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, hierachy) = cv2.findContours(img1_canny.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
|
|
||||||
img1_contours = np.zeros((height,width,3), np.uint8)
|
|
||||||
img1_contours = cv2.drawContours(img1_contours, contours, -1, (0,255,0), 1)
|
|
||||||
|
|
||||||
objects = findObjects.find(contours, hierachy)
|
|
||||||
|
|
||||||
# print (ids)
|
|
||||||
|
|
||||||
img1_objects = frame.copy()
|
|
||||||
img1_colors = np.zeros((height,width,3), np.uint8)
|
|
||||||
maskCenter = 4
|
|
||||||
roides = []
|
|
||||||
beads = []
|
|
||||||
for obj in objects:
|
|
||||||
memberCount = 0
|
|
||||||
radius = 0
|
|
||||||
roi = 0
|
|
||||||
for member in obj['members']:
|
|
||||||
# Draw bounding box
|
|
||||||
x,y,w,h = [member['bbox'][0], member['bbox'][1], member['bbox'][2], member['bbox'][3]];
|
|
||||||
if member['isHole']:
|
|
||||||
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_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]/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)
|
|
||||||
|
|
||||||
memberCount += 1
|
|
||||||
|
|
||||||
for bead in beads:
|
|
||||||
beadColors.append(bead['color'])
|
|
||||||
|
|
||||||
|
|
||||||
colorClasses = []
|
|
||||||
for bead in beads:
|
|
||||||
if not colorClasses:
|
|
||||||
colorClasses.append(bead['color'])
|
|
||||||
else:
|
else:
|
||||||
d, i = mindistance(colorClasses, bead['color'])
|
ret, frame = vs.read()
|
||||||
if d > 0.14:
|
|
||||||
|
if args["use_calibration"] == 'on':
|
||||||
|
frame = np.uint8(cv2.multiply(lens_corr, np.float32(frame)))
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
# Thresholding
|
||||||
|
# ret,img1_thr = cv2.threshold(gray_blurred,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
|
||||||
|
# img1_thr = cv2.adaptiveThreshold(gray_blurred,255,cv2.ADAPTIVE_THRESH_MEAN_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, hierachy) = cv2.findContours(img1_canny.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
|
||||||
|
img1_contours = np.zeros((height,width,3), np.uint8)
|
||||||
|
img1_contours = cv2.drawContours(img1_contours, contours, -1, (0,255,0), 1)
|
||||||
|
|
||||||
|
objects = findObjects.find(contours, hierachy)
|
||||||
|
|
||||||
|
# print (ids)
|
||||||
|
|
||||||
|
img1_objects = frame.copy()
|
||||||
|
img1_colors = np.zeros((height,width,3), np.uint8)
|
||||||
|
maskCenter = 4
|
||||||
|
roides = []
|
||||||
|
beads = []
|
||||||
|
for obj in objects:
|
||||||
|
memberCount = 0
|
||||||
|
radius = 0
|
||||||
|
roi = 0
|
||||||
|
for member in obj['members']:
|
||||||
|
# Draw bounding box
|
||||||
|
x,y,w,h = [member['bbox'][0], member['bbox'][1], member['bbox'][2], member['bbox'][3]];
|
||||||
|
if member['isHole']:
|
||||||
|
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_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]/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)
|
||||||
|
|
||||||
|
memberCount += 1
|
||||||
|
|
||||||
|
for bead in beads:
|
||||||
|
beadColors.append(bead['color'])
|
||||||
|
|
||||||
|
|
||||||
|
colorClasses = []
|
||||||
|
for bead in beads:
|
||||||
|
if not colorClasses:
|
||||||
colorClasses.append(bead['color'])
|
colorClasses.append(bead['color'])
|
||||||
else:
|
else:
|
||||||
colorClasses[i][0] = 0.5*colorClasses[i][0] + 0.5*bead['color'][0]
|
d, i = mindistance(colorClasses, bead['color'])
|
||||||
colorClasses[i][1] = 0.5*colorClasses[i][1] + 0.5*bead['color'][1]
|
if d > 0.14:
|
||||||
colorClasses[i][2] = 0.5*colorClasses[i][2] + 0.5*bead['color'][2]
|
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):
|
if numColorClasses != len(colorClasses):
|
||||||
numColorClasses = len(colorClasses)
|
numColorClasses = len(colorClasses)
|
||||||
print("Found " + str(numColorClasses) + " color classes")
|
print("Found " + str(numColorClasses) + " color classes")
|
||||||
|
|
||||||
|
|
||||||
cv2.imshow('Corrected', frame)
|
cv2.imshow('Corrected', frame)
|
||||||
cv2.imshow('Contours', frame_dilated)
|
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)
|
||||||
|
if videoFile == "piCamera":
|
||||||
|
cv2.waitKey(1)
|
||||||
|
else:
|
||||||
|
# cv2.waitKey(0)
|
||||||
|
cv2.waitKey(int(1000.0/framerate))
|
||||||
|
|
||||||
|
|
||||||
|
# update the FPS counter
|
||||||
|
fps.update()
|
||||||
|
|
||||||
|
# stop the timer and display FPS information
|
||||||
|
fps.stop()
|
||||||
if videoFile == "piCamera":
|
if videoFile == "piCamera":
|
||||||
cv2.waitKey(1)
|
vs.stop()
|
||||||
else:
|
|
||||||
# cv2.waitKey(0)
|
|
||||||
cv2.waitKey(int(1000.0/framerate))
|
|
||||||
|
|
||||||
|
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
|
||||||
# update the FPS counter
|
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
|
||||||
fps.update()
|
if videoFile == "piCamera":
|
||||||
|
print("[INFO] Camera : elasped time: {:.2f}".format(vs.getfps().elapsed()))
|
||||||
# stop the timer and display FPS information
|
print("[INFO] Camera: approx. FPS: {:.2f}".format(vs.getfps().fps()))
|
||||||
fps.stop()
|
|
||||||
if videoFile == "piCamera":
|
|
||||||
vs.stop()
|
|
||||||
|
|
||||||
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
|
|
||||||
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
|
|
||||||
if videoFile == "piCamera":
|
|
||||||
print("[INFO] Camera : elasped time: {:.2f}".format(vs.getfps().elapsed()))
|
|
||||||
print("[INFO] Camera: approx. FPS: {:.2f}".format(vs.getfps().fps()))
|
|
||||||
|
|
||||||
if numColorClasses:
|
|
||||||
|
|
||||||
# Output stats
|
# Output stats
|
||||||
findObjects.printStats()
|
findObjects.printStats()
|
||||||
@@ -318,7 +322,7 @@ if numColorClasses:
|
|||||||
|
|
||||||
# Define criteria = ( type, max_iter, epsilon )
|
# Define criteria = ( type, max_iter, epsilon )
|
||||||
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.1)
|
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)
|
ret,label,center=cv2.kmeans(Z,numColorClasses,None,criteria,10,cv2.KMEANS_PP_CENTERS)
|
||||||
|
|
||||||
print (center)
|
print (center)
|
||||||
|
|
||||||
@@ -341,9 +345,6 @@ if numColorClasses:
|
|||||||
|
|
||||||
plt.show()
|
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