From 4fb15f2147bb7d6d249149ac57f2a327d30471f2 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 17 Sep 2016 13:34:00 +0000 Subject: [PATCH] - improved color detector git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@311 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- circledetect_threaded.py | 73 +++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 24 deletions(-) diff --git a/circledetect_threaded.py b/circledetect_threaded.py index 9b39b74..f1ddb32 100755 --- a/circledetect_threaded.py +++ b/circledetect_threaded.py @@ -10,6 +10,8 @@ import imutils import time import cv2 import pprint +from matplotlib import pyplot as plt +from mpl_toolkits.mplot3d import Axes3D width = 320 height = 240 @@ -90,7 +92,6 @@ class FindObjects(): objects.append({'thickness' : self.meanThickness, 'members' : obj}) -# self.pp.pprint(objects) return objects def printStats(self): @@ -114,20 +115,25 @@ time.sleep(2.0) fps = FPS().start() findObjects = FindObjects() +beadColors = [] + # loop over some frames...this time using the threaded stream while fps._numFrames < args["num_frames"]: # grab the frame from the threaded video stream frame = vs.read() gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) - gray_bluured = cv2.medianBlur(gray,5) + gray_blurred = cv2.medianBlur(gray,5) # 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 -# ret,img1_thr = cv2.threshold(gray1,120,255,cv2.THRESH_BINARY) -# img1_thr = cv2.adaptiveThreshold(gray1,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) +# 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) @@ -138,8 +144,9 @@ while fps._numFrames < args["num_frames"]: img1_objects = frame.copy() img1_colors = np.zeros((height,width,3), np.uint8) - maskCenter = 2 + maskCenter = 4 roides = [] + beads = [] for obj in objects: memberCount = 0 radius = 0 @@ -150,28 +157,26 @@ while fps._numFrames < args["num_frames"]: if member['isHole']: img1_objects = cv2.rectangle(img1_objects,(x,y),(x+w,y+h),(255,0,0),2) thickness = obj['thickness']-maskCenter - radius = int((member['diameter']+thickness+maskCenter)/2) - roi = frame[y-thickness:y+2*radius, x-thickness:x+2*radius] - roides.append({'roi' : roi, 'pos' : member['pos'], 'radius' : radius, 'thickness' : thickness}) + diameter = member['diameter']+thickness+maskCenter + roi = frame[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]}) + 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 r in roides: - if radius > 0: - 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) + for bead in beads: + beadColors.append(bead['color']) - cv2.imshow('detected colors', img1_colors) # cv2.imshow('Thresholded',img1_thr) + cv2.imshow('detected colors', img1_colors) cv2.imshow('Canny',img1_canny) cv2.imshow('Objects',img1_objects) cv2.waitKey(1) @@ -179,8 +184,6 @@ while fps._numFrames < args["num_frames"]: # update the FPS counter fps.update() -findObjects.printStats() - # stop the timer and display FPS information fps.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: 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 cv2.destroyAllWindows()