def findFrame(contours,canvas,axis): #compute the width and height of a square sqNum = 3 lw = canvas.shape[1]/sqNum lh = canvas.shape[0]/sqNum if axis==0: #vertical frames hist = [0,]*sqNum #create a histogram to store the info about the contours for cont in contours: center,r = cv2.minEnclosingCircle(cont) #we simplify computations. x = center[0] for i in range(sqNum): #if "center is in the frame" or the circle intersects the frame if(i*lw<x and (i+1)*lw>x) or (abs(x-i*lw)<r or abs(x-(i+1)*lw)<r): hist[i]+=1 #paint the info stored in hist for i in enumerate(hist): if i[1]!=0: plane = np.ones((canvas.shape[0],lw),np.uint8)*125 canvas[:,i[0]*lw:(i[0]+1)*lw,1]=plane if axis==1: #horizontal frames hist = [0,]*sqNum #create a histogram to store the info about the contours for cont in contours: center,r = cv2.minEnclosingCircle(cont) #we simplify computations. y = center[1] for i in range(sqNum): #if "center is in the frame" or the circle intersects the frame if(i*lh<y and (i+1)*lh>y)or(abs(y-i*lh)<r or abs(y-(i+1)*lh)<r): hist[i]+=1 #paint the info stored in hist for i in enumerate(hist): if i[1]!=0: plane = np.ones((lh,canvas.shape[0]),np.uint8)*125 canvas[i[0]*lh:(i[0]+1)*lh,:,1]=plane if axis==2:#horizontal and vertical frames sqHist = [] #create an appropiate histogram (2D) for i in range(sqNum): aux = [0,]*sqNum sqHist.append(aux) for cont in contours: center,r = cv2.minEnclosingCircle(cont) x,y = center[0],center[1] for i in range(sqNum): #if "center is in the vertical frame" or the circle intersects the vertical frame if(i*lw<x and (i+1)*lw>x)or(abs(x-i*lw)<r or abs(x-(i+1)*lw)<r): for j in range(sqNum): #if "center is in the horizontal frame" or the circle intersects the horizontal frame if(j*lh<y and (j+1)*lh>y)or(abs(y-j*lh)<r or abs(y-(j+1)*lh)<r): sqHist[j][i]+=1 #paint the result for row in enumerate(sqHist): for column in enumerate(row[1]): if column[1]!=0: plane = np.ones((lh,lw),np.uint8)*125 canvas[row[0]*lh:(row[0]+1)*lh,column[0]*lw:(column[0]+1)*lw,1]=plane
def process_image_using_canny(img, img2): kernel = np.ones((4,4),np.uint8) img = cv2.erode(img,kernel,iterations = 1) img = cv2.dilate(img,kernel,iterations = 1) threshold = int(img.max() - img.std()) #rect, thresh = cv2.threshold(img, threshold, 255, 0) rect,thresh = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) #blur = cv2.GaussianBlur(img,(5,5),0) #rect, thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) #import pdb; pdb.set_trace() contours, hier = cv2.findContours(thresh,mode=cv2.RETR_LIST, method=cv2.CHAIN_APPROX_NONE) cv2.drawContours(img2, contours, -1, 255, 2) #import pdb; pdb.set_trace() #import pdb; pdb.set_trace() circles = [] for cnt in contours: (x, y), radius = cv2.minEnclosingCircle(cnt) center = (int(x), int(y)) radius = int(radius) if radius > 23 or radius < 2: continue cv2.circle(img2, center, radius, 0, 2) circles.append([center[0], center[1], radius]) """ Filter and return the circles which satisfy this criteria """ radiis = [cv2.minEnclosingCircle(cnt) for cnt in contours] return circles, img2
def get_led_pos (frame): HSV_Mask = cv2.cvtColor (frame, cv2.COLOR_BGR2HSV) # converts to HSV color_mask = cv2.inRange (HSV_Mask, LED_COLOR_LOWER, LED_COLOR_UPPER) # Blurring blur = cv2.blur (color_mask, (9, 9)) # try cv2.erode dilate = cv2.dilate (blur, None, iterations=21) #cv2.imshow ("dilate", dilate) # Detects lines thresh = cv2.adaptiveThreshold (dilate, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, BLOCKSIZE, 1) # cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) -> dst # Finds contours of lines contours, heirarchy = cv2.findContours (dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy radius = [] center = [] for contour in contours: center.append (cv2.minEnclosingCircle (contour)[0]) radius.append (cv2.minEnclosingCircle (contour)[1]) return radius, center, frame
def find_the_two_objects(img): ret, thresh = cv2.threshold(img, 127, 255, 0) contours, hierarchy = cv2.findContours(thresh, 1, 2) largest = None second = None if len(contours) > 0: lcont = contours[0] scont = lcont largest_area = 0 second_largest_area = 0 for contour in contours: a = cv2.contourArea(contour, False) if a > largest_area: second_largest_area = largest_area largest_area = a scont = lcont lcont = contour elif a > second_largest_area: second_largest_area = a scont = contour largest, radius = cv2.minEnclosingCircle(lcont) second, r = cv2.minEnclosingCircle(scont) return second, largest
def runPolyDetect(buffer_size): camera = cv2.VideoCapture(0) while True: grabbed, frame = camera.read() frame = imutils.resize(frame, width = 600) blur = cv2.GaussianBlur(frame, (11, 11), 0) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, lower, upper) mask = cv2.erode(mask, None, iterations = erode_iter) mask = cv2.dilate(mask, None, iterations = dilate_iter) cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] centers = [] if len(cnts) == 1: # print "One Object Found!" # c = max(cnts, key = cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(cnts[0]) M = cv2.moments(cnts[0]) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) else: # print "%d Objects Found!" % (len(cnts)) for i in range(0, len(cnts)): ((x, y), radius) = cv2.minEnclosingCircle(cnts[i]) M = cv2.moments(cnts[i]) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) if radius > max_radius: centers.append(center) cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) for x in range(1, len(centers)): thickness = 1 cv2.line(frame, centers[x], centers[x - 1], (0, 0, 255), thickness, lineType = cv2.CV_AA) if x == len(centers) - 1: cv2.line(frame, centers[x], centers[0], (0, 0, 255), thickness, lineType = cv2.CV_AA) cv2.imshow("frame", cv2.flip(frame, 1)) cv2.imshow("mask", cv2.flip(mask, 1)) maskFilterEdit() key = cv2.waitKey(1) if key == ord("q"): break
def within(cnt, contours): print len(contours) (x1, y1), radius1 = cv2.minEnclosingCircle(cnt) for c in contours: (x2, y2), radius2 = cv2.minEnclosingCircle(c) distance = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) radDiff = (radius1 - radius2)*(radius1 - radius2) if radDiff > distance and radius1 < radius2: return True return False
def FindTheBalls(img, contours, similarity_threshold=5): """ Find and circle all of the balls on the table. Currently struggles with balls on the rail. Not yet tested on clusters. Returns a three-tuple containing a tuple of x,y coords, a radius and the masked out area of the image. Needs to be made into a ball object. """ #dimensions of image height,width, channels = img.shape #compare the difference in area of a min bounding circle and the cotour area diffs = [] indexes = [] for i,contour in enumerate(contours): contourArea = cv2.contourArea(contour) (x,y),radius = cv2.minEnclosingCircle(contour) circleArea = 3.141 * (radius**2) diffs.append(abs(circleArea-contourArea)) indexes.append(i) sorted_data = sorted(zip(diffs,indexes)) diffs = [x[0] for x in sorted_data] indexes = [x[1] for x in sorted_data] #list of center coords as tuples centers = [] radii = [] masks = [] for i,d in zip(indexes,diffs): #if the contour is a similar shape to the circle it is likely to be a ball. if (d < diffs[0] * similarity_threshold): (x,y),radius = cv2.minEnclosingCircle(contours[i]) center = (int(x),int(y)) radius = int(radius) #remove .copy() to display a circle round each ball cv2.circle(img.copy(),center,radius,(0,0,255),2) centers.append(center) radii.append(radius) circle_img = np.zeros((height,width), np.uint8) cv2.circle(circle_img,center,radius,1,thickness=-1) masked_data = cv2.bitwise_and(img, img, mask=circle_img) masks.append(masked_data) return zip(centers,radii,masks)
def locateBlobs(self, frame): #load mask maskedFrame = frame & self.all_mask; #gaussian blur #cv2.GaussianBlur(maskedFrame, (3, 3), 0, maskedFrame) #convert to HLS colour space (slow, not currently used) #yuvframe = cv2.cvtColor(maskedFrame, cv2.COLOR_RGB2HLS) # threshold on saturation being high # Would probably be more effective to transform into # HSV color-space, but two RGB windows seems pretty # effective, and probably about the same cost. # A single RGB window covering both bright and dark orange # ends up including too much other stuff. # These values are calibrated for daylight (with floodlight) # If adjusting for artificial light please preserve these valus low = np.array([0x28, 0x50, 0xa0], np.uint8) mid1 = np.array([0x50, 0x90, 0xff], np.uint8) mid2 = np.array([0x40, 0x60, 0xe0], np.uint8) high = np.array([0xa0, 0xc0, 0xff], np.uint8) mask1 = cv2.inRange(maskedFrame, low, mid1) mask2 = cv2.inRange(maskedFrame, mid2, high) threshframe = mask1 | mask2 if display_mask: self.foo = threshframe.copy() #dilate the Thresholded image #cv2.dilate(threshframe, None, dst=threshframe) #cv2.erode(threshframe, None, dst=threshframe) #self.t = threshframe.copy() contours, hierarchy = cv2.findContours(threshframe, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) maxContour = None maxContourArea = 0 for c in contours: # get points circle = cv2.minEnclosingCircle(c) area = cv2.contourArea(c) if maxContourArea < area: maxContour = c maxContourArea = area if maxContour is None: return None circle = cv2.minEnclosingCircle(maxContour) # just return the biggest circle return circle
def create_circle(thresh,cnt,cx,cy): thresh = cv2.circle(thresh,(cx,cy), 5, (0,0,255), -1) (x,y),radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) thresh = cv2.circle(thresh,center,radius,(0,255,0),2) return thresh
def detect_color_blob(self, img, lower, upper, color): og_img = img hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, lower, upper) mask = cv2.erode(mask, (3,3), iterations=1) contours, h = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #if self.debugging: #cv2.drawContours(img, contours, -1, (0, 0, 255), 2) sorted_contours = sorted(contours, key = lambda c: cv2.contourArea(c), reverse=True) if len(sorted_contours) < 1: return None c = sorted_contours[0] area = cv2.contourArea(c) if area < 1000: # minimum area threshold return None perim = cv2.arcLength(c, True) # perimeter approx = cv2.approxPolyDP(c, 0.05 * perim, True) #detecting circles (x,y), radius = cv2.minEnclosingCircle(c) if len(approx) == 4: (x, y, w, h) = cv2.boundingRect(approx) ar = w / float(h) self.shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"
def getLetter(self): if self.thresh is None: print "no thresh for getLetter" return 0 contours, hierarchy = cv2.findContours( self.thresh.copy() ,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # sort contours by cnt area / img area ratio from max to min contours = sorted(contours, key = lambda x: cv2.contourArea(x)/self.thresh.size, reverse = False) for cnt in contours: area = cv2.contourArea(cnt) # area calculation (x,y),radius = cv2.minEnclosingCircle(cnt) radius = int(radius) minArea = 3.14159*radius*radius*0.6 maxArea = 3.14159*radius*radius*1.4 # if actual area is close enough to 2*pi*r area && area > 10% of pic if area >= minArea and area <= maxArea and area > self.thresh.size*0.1: # copy thresh = cv2.bitwise_not(self.thresh) mask = np.zeros_like(thresh) # Create mask where white is what we want, black otherwise cv2.drawContours(mask, [cnt], 0, 255, -1) # Draw filled contour in mask self.letter = np.zeros_like(thresh) # Extract out the object and place into output image self.letter[mask == 255] = thresh[mask == 255]
def getShape(contour): p = cv2.arcLength(contour, True) aV = cv2.approxPolyDP(contour, 0.04 * p, True) vertices = len(aV) if vertices == 3: return 'Triangle' elif vertices == 4: rect = cv2.minAreaRect(contour) contourArea = cv2.contourArea(contour) fittedArea = rect[1][0] * rect[1][1] #print "Countor Area:", contourArea , " Fiited A:", fittedArea (x, y, w, h) = cv2.boundingRect(aV) ar = w / float(h) if .95 * fittedArea <= contourArea and ar >= 0.95 and ar <= 1.05: return 'Square' else: return 'Rectangle' elif vertices == 5: return 'Pentagon' elif vertices == 6: return 'Hexagon' elif vertices == 7: return 'Heptagon' else: (xC, yC), radius = cv2.minEnclosingCircle(contour) contourArea = cv2.contourArea(contour) fittedArea = radius*radius*3.14 # print "Countor Area:", contourArea , " Circle A:", fittedArea if abs(contourArea-fittedArea) / max(contourArea, fittedArea) < 0.10: return 'Circle' else: return str(str(len(aV))+'-Polygon') return 'Unknown'
def find_target(image_to_analyze, trackbar_window, image_to_draw_on=None, indicator_color=(0, 0, 0), adjustment_ratio=1): lower, upper = trackbar_window.get_range() median = cv2.medianBlur(image_to_analyze, 15) mask = cv2.inRange(median, lower, upper) mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, None, iterations=2) cv2.imshow(trackbar_window.window_name, mask) # Find contours in the mask and initialize the current (x, y) center of the ball contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None radius = None # Proceed only if at least one contour was found. if len(contours) > 0: # Find the largest contour in the mask, then use it to # compute the minimum enclosing circle and centroid. c = max(contours, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) center = (int(adjustment_ratio*x), int(adjustment_ratio*y)) radius = int(adjustment_ratio*radius) if center is not None and image_to_draw_on is not None: draw_largest_contour(image_to_draw_on, center, radius, color=indicator_color) return center, radius
def InitFromContour(self, contour): # Prefilter on bounding rect because moments take some time self.recX, self.recY, self.recW, self.recH = cv2.boundingRect(contour) if self.recH < config['hand']['minimumHeight'] or self.recH > config['hand']['maximumHeight'] or self.recW < \ config['hand']['minimumWidth'] or self.recW > config['hand']['maximumWidth']: return False self.handContour = contour self.properties['foundHand'] = True # Get general info self.moments = cv2.moments(contour) self.hull = cv2.convexHull(self.handContour, returnPoints = False) self.defects = cv2.convexityDefects(self.handContour, self.hull) _,radius = cv2.minEnclosingCircle(self.handContour) self.radius = int(radius / 1.2) self.centerX = int(self.moments['m10'] / self.moments['m00']) self.centerY = int(self.moments['m01'] / self.moments['m00']) self.properties['centerX'] = self.centerX self.properties['centerY'] = self.centerY self.InitGestures() return True
def getContours(img, fit_type='ellipse', AREA_EXCLUSION_LIMITS=(200, 2000), CELL_RADIUS_THRESHOLD = 4): contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) coord_list = [] for i in range(len(contours)): if fit_type == 'circle': radius_list = [] center_list = [] (x,y),radius = cv2.minEnclosingCircle(contours[i]) if radius > CELL_RADIUS_THRESHOLD: center = (int(x), int(y)) center_list.append(center) radius_list.append(radius) cv2.circle(img,center,int(radius),(0,255,0),-11) coord_list.append([center_list, radius_list]) elif fit_type == 'ellipse': if len(contours[i]) >= 5: ellipse = cv2.fitEllipse(contours[i]) area = np.pi*np.product(ellipse[1]) if area >= AREA_EXCLUSION_LIMITS[0] and area < AREA_EXCLUSION_LIMITS[1]: cv2.ellipse(img,ellipse,(0,255,0),-1) return img, contours, coord_list
def get_center(cnt): (x, y), radius = cv2.minEnclosingCircle(cnt) x, y = int(x), int(y) radius = int(radius) return x, y, radius
def find_contour_center(self, cnt): """ finds the center of a contour using a bounding circle. """ (x,y), r = cv2.minEnclosingCircle(cnt) center = (int(x), int(y)) return center
def liczenie(crop_img): while(True): #crop_img = cv2.imread("6.png",1) #gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY) lower_white = np.array([0,0,150], dtype=np.uint8) upper_white = np.array([200,100,255], dtype=np.uint8) hsv = cv2.cvtColor(crop_img, cv2.COLOR_BGR2HSV) white = cv2.inRange(hsv, lower_white, upper_white) #img = cv2.erode(white, None, 1) #img1 = cv2.dilate(img,None,1) #mask = cv2.dilate(white,None,1) mask=white # Bitwise-AND mask and original image #res = cv2.bitwise_and(crop, crop, mask= mask) cv2.imshow('Kostka', mask) cv2.moveWindow('Kostka', 2200, 0) #image = cv2.Canny(res,50,50) #ret,thresh = cv2.threshold(imgg,200,200,0) contours , hierarchy = cv2 . findContours ( mask , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE ) #cv2.drawContours(crop, contours, -1, (0,255,0), 1) liczba=0 for i in contours[1:]: (x,y),radius = cv2.minEnclosingCircle(i) center = (int(x),int(y)) radius = int(radius) #print(radius) #cv2.circle(crop_img,center,radius,(0,255,0),2) liczba=liczba+1 #cv2.imshow("cropped", crop_img) #cv2.moveWindow('cropped', 1500, 0) return liczba
def segment(image, thresh): #preprocess image gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #perform euclidean distance transform distances = ndimage.distance_transform_edt(thresh) localMax = peak_local_max(distances, indices = False, min_distance = 3, labels = thresh) #perform connected component analysis on local peaks markers = ndimage.label(localMax, structure = np.ones((3, 3)))[0] labels = watershed(-distances, markers, mask = thresh) #loop over labels returned from watershed to mark them for label in np.unique(labels): if label == 0: continue mask = np.zeros(gray.shape, dtype="uint8") mask[labels == label] = 255 #find contours in mask and choose biggest contour by area contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] contour = max(contours, key = cv2.contourArea) #draw circle around max size contour ((x, y), r) = cv2.minEnclosingCircle(contour) cv2.circle(image, (int(x), int(y)), int(r), (0, 255, 0), 2) #show final image cv2.imshow("Output", image) return len(np.unique(labels) - 1)
def process_image(self): self.images["HSV_Mask"] = cv2.cvtColor(self.images["Src"], cv2.COLOR_BGR2HSV) # Expensive command self.images["Color_Mask"] = cv2.inRange(self.images["HSV_Mask"], self.LED_COLOR_LOWER, self.LED_COLOR_UPPER) self.images["Blur"] = cv2.blur(self.images["Color_Mask"], (9, 9)) self.images["Dilate"] = cv2.dilate(self.images["Blur"], None, iterations=21) self.images["Dilate_Output"] = deepcopy( self.images["Dilate"] ) # Since cv2.adaptiveThreshold modifies the src image # Detects lines self.images["Thresh"] = cv2.adaptiveThreshold( self.images["Dilate"], 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, self.BLOCKSIZE, self.C ) # Finds contours of lines contours, heirarchy = cv2.findContours(self.images["Dilate"], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) self.led_pos = [] for contour in contours: self.led_pos.append(cv2.minEnclosingCircle(contour)[0]) # Index 0 for minEnclosingCircle returns the center # radius.append (cv2.minEnclosingCircle (contour)[1]) # Can be used for error checking if len(self.led_pos) == 3: self.LEDS_FOUND = True else: self.LEDS_FOUND = False
def get_contour_centre(contour): """ Find the center of a contour by minimum enclousing circle approximation. Returns: ((x, y), radius) """ return cv2.minEnclosingCircle(contour)
def get_contour_feature(contour): # fit center center, radius = cv2.minEnclosingCircle(contour) circle_area = radius * radius * math.pi # fit rectangle x, y, w, h = cv2.boundingRect(contour) rect_area = w * h # find centroid M = cv2.moments(contour) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) # contour area contour_area = cv2.contourArea(contour) # factors circle_factor = contour_area / float(circle_area) rect_factor = contour_area / float(rect_area) flat_factor = w / float(h) # final feature feature = [circle_factor, rect_factor, flat_factor, cx, cy, contour_area] return feature
def get_green_circle(frame, hlg, slg, vlg, hhg, shg, vhg): global numberErrorsBk global centerBk global radiusBk side_mask = make_image_mask_green(frame, hlg, slg, vlg, hhg, shg, vhg) # if type(side_mask) != type(''): contours, hierarchy = cv2.findContours(side_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) if len(contours): cnt = contours[0] (x,y),radiusBk = cv2.minEnclosingCircle(cnt) centerBk = (int(x),int(y)) radiusBk = int(radiusBk) side_mask = cv2.cvtColor(side_mask, cv2.COLOR_GRAY2RGB) cv2.circle(side_mask, centerBk, radiusBk, np.array([0,255,0]), 10) cv2.imshow('green mask', side_mask) #print 'center: ', center, 'radius: ', radius, ' found from side camera' else: print 'no contours found for green' numberErrorsBk = numberErrorsBk + 1 else: print 'green string'
def drawStuff(centerCordinates, image): # http://opencvpython.blogspot.no/2012/06/contours-2-brotherhood.html # http://docs.opencv.org/3.1.0/dd/d49/tutorial_py_contour_features.html#gsc.tab=0pyth ############## creating a minimum rectangle around the object ###################### rect = cv2.minAreaRect(points=centerCordinates) box = cv2.cv.BoxPoints(rect) box = np.int0(box) cv2.drawContours(image,[box],0,(255,255,255),2) ########### circle around object #######3 (x, y),radius = cv2.minEnclosingCircle(centerCordinates) center = (int(x),int(y)) radius = int(radius) cv2.circle(image, center, radius, (255,255,255),2) ########### finding a elipse ############## ellipse = cv2.fitEllipse(centerCordinates) cv2.ellipse(image,ellipse,(255,255,255),2) ##### fitting a line ########### rows,cols = image.shape[:2] [vx,vy,x,y] = cv2.fitLine(points=centerCordinates, distType=cv2.cv.CV_DIST_L2, param =0, reps=0.01, aeps=0.01) lefty = int((-x*vy/vx) + y) righty = int(((cols-x)*vy/vx)+y) cv2.line(image,(cols-1,righty),(0,lefty),(255,255,255),2) pixelSizeOfObject = radius # an okay estimate for testing return image, pixelSizeOfObject
def hueBasedTracking(frame): #useful for smoothening the images, median of all the pixels under kernel #area and central element is replaced with this median value. #highly effective against salt-and-pepper noise in the images. frame = cv2.medianBlur(frame,5) #convert color from BGR to HSV hsv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #define lower and upper limit of hue range for red color lower_red_hue_range = cv2.inRange(hsv_image, np.array([0, 100, 100]), np.array([10, 255, 255])) upper_red_hue_range = cv2.inRange(hsv_image, np.array([160, 100, 100]), np.array([179, 255, 255])) #Calculates the weighted sum of two arrays. red_hue_image = cv2.addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 2.0, 0.0) #Blurs an image using a Gaussian filter #The function convolves the source image with the specified Gaussian kernel red_hue_image = cv2.GaussianBlur(red_hue_image, (9, 9), 2, 2) #find contours in the red_hue_image formed after weighted adding of lower and upper ranges of red cnts = cv2.findContours(red_hue_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None # only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use it to compute the minimum enclosing circle and centroid c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) #gives x and y coordinates of center center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) return center #returns the centroid for th image
def find(self, frame, queue): for color in self.color: contours, hierarchy, mask = self.preprocess( frame, self.crop, color['min'], color['max'], color['contrast'], color['blur'] ) if len(contours) <= 0: # print 'No ball found.' pass # queue.put(None) else: # Trim contours matrix cnt = self.get_largest_contour(contours) # Get center (x, y), radius = cv2.minEnclosingCircle(cnt) queue.put({ 'name': self.name, 'x': x, 'y': y, 'angle': None, 'velocity': None }) return queue.put(None) return
def merge_contour(i,j,i_x,i_y,j_x,j_y): ''' This is a very critical and potentially time consuming step. We need to find the nearest point of the two contours, and then splice in the smaller area contour into larger area contour ''' c_d = contours[i] c_s = contours[j] min_dist = numpy.Infinity min_i = 0 min_j = 0 for i in range(0,len(c_d)): for j in range(0,len(c_s)): dist = numpy.sqrt(numpy.square(c_d[i][0][0] - c_s[j][0][0]) + numpy.square(c_d[i][0][1] - c_s[j][0][1])) if dist < min_dist: min_dist = dist min_i = i min_j = j # Now that we have identified the point of insertion into destination the source, we create a new contour list, by copying all elements upto 'i' # from destination, then copying all elements from 'j' till end from source, then from '0' to 'j-1' of source, and finally the i+1th to end of destination new_c = [] for k in range(0,min_i): new_c.append(c_d[k]) for k in range(min_j,len(c_s)): new_c.append(c_s[k]) for k in range (0,min_j): new_c.append(c_s[k]) for k in range(min_i,len(c_d)): new_c.append(c_d[k]) # Finally, we replace the contour 'i' with new_c, recompute the circle equivalent of new_c, and replace the data in contours_circles for index 'i' contours[i] = numpy.asarray(new_c) (new_x, new_y), new_radius = cv2.minEnclosingCircle(contours[i]) new_circle_data_list = [new_x,new_y,new_radius] contours_circles[i] = new_circle_data_list return
def outputs(self): global hsv,mask stream=io.BytesIO() for i in range(npic): yield stream start_time=time.time() stream.seek(0) try: tmp=np.fromstring(stream.getvalue(),dtype=np.uint8) tmp=tmp.reshape((W,H,3)) hsv=cv2.cvtColor(tmp,cv2.COLOR_BGR2HSV) mask=cv2.inRange(hsv,ball_min,ball_max) cmask=mask.copy() contours,hierarchy=cv2.findContours(cmask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) maxarea=0 cnt=None for h,tcnt in enumerate(contours): area=cv2.contourArea(tcnt) if area>maxarea: maxarea=area cnt=tcnt (x,y),r=(0,0),0 if cnt != None: (x,y),r=cv2.minEnclosingCircle(cnt) cv2.circle(tmp,((int)(x),(int)(y)),(int)(r),(0,0,255),1) cv2.putText(tmp,"%.1f %.1f %.1f"%(x,y,r),(1,20),cv2.FONT_HERSHEY_PLAIN,1.0,(255,0,255)) finish=time.time() cv2.putText(tmp,"dist:%.1f, ang:%.1f"%self.calc_loc(x,y),(0,40),cv2.FONT_HERSHEY_PLAIN,1.0,(0,255,255)) cv2.putText(tmp,"fps:%.1f"%(1/(finish-start_time)),(0,60),cv2.FONT_HERSHEY_PLAIN,1.0,(255,255,0)) images.put(tmp) except Exception,e: print e stream.truncate()
def find(self, frame, queue): for color in self.color: """ contours, hierarchy, mask = self.preprocess( frame, self.crop, color['min'], color['max'], color['contrast'], color['blur'] ) """ # adjustments = {'min':,'mz'} contours, hierarchy, mask = self.get_contours(frame.copy(), self.crop, color, "BALL") if len(contours) <= 0: print "No ball found." pass # queue.put(None) else: # Trim contours matrix cnt = self.get_largest_contour(contours) # Get center (x, y), radius = cv2.minEnclosingCircle(cnt) queue.put({"name": self.name, "x": x, "y": y, "angle": None, "velocity": None}) queue.put(None) pass
def findRobot(lower,upper,hsv): # construct a mask for the color "green", then perform # a series of dilations and erosions to remove any small # blobs left in the mask inRange = cv2.inRange(hsv, lower, upper) mask = cv2.erode(inRange, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) # find contours in the mask and initialize the current # (x, y) center of the ball cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] hsvInRange = cv2.bitwise_and(hsv,hsv,mask=inRange) bgrInRange = cv2.cvtColor(hsvInRange,cv2.COLOR_HSV2BGR) # only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) # draw the circle and centroid on the frame, # then update the list of tracked points if radius > 2: return ((int(x),int(y)),int(radius)),bgrInRange else: return None,bgrInRange else: return None,bgrInRange
def contour_info(_cnt): _mmt = cv.moments(_cnt) x, y, w, h = cv.boundingRect(_cnt) aspect_ratio = w / h hull = cv.convexHull(_cnt) solidity = cv.contourArea(_cnt) / cv.contourArea(hull) equivalent_diameter = np.sqrt(4 * cv.contourArea(_cnt) / np.pi) # 최적원 지름 구하기 minRect = cv.minAreaRect(_cnt) # 바운딩박스 값에 추가로 기울어진 각도까지 구한다. boxPts = cv.boxPoints(minRect) boxPts = np.int0(boxPts) cv.drawContours(im, [boxPts], 0, (0, 255, 0), 1) #외각원 minCircle = cv.minEnclosingCircle(_cnt) minCirclePos = np.int0(minCircle[0]) minCircleR = int(minCircle[1]) coronaSize = minCircleR - (equivalent_diameter / 2) print(aspect_ratio, solidity, coronaSize) cx = int(_mmt["m10"] / _mmt["m00"]) cy = int(_mmt["m01"] / _mmt["m00"]) cv.drawContours(im, [_cnt], 0, (0, 0, 255), 3) cv.circle(im, (cx, cy), 4, (255, 0, 0), -1) cv.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), thickness=1) cv.circle(im, (cx, cy), int(equivalent_diameter / 2), (255, 255, 0), thickness=1) cv.circle(im, (minCirclePos[0], minCirclePos[1]), minCircleR, (255, 255, 255), thickness=1) cv.putText(im, f'{cx},{cy}', (cx, cy), font, 0.25, (255, 0, 0), 1, cv.LINE_AA) cv.putText(im, f'{solidity}', (cx, cy + 10), font, 0.25, (0, 0, 0), 1, cv.LINE_4) cv.putText(im, f'{aspect_ratio}', (cx, cy + 20), font, 0.25, (0, 0, 0), 1, cv.LINE_4) #원인지 검사 if coronaSize < 5 and solidity > 0.8 and abs(1 - aspect_ratio) < 0.1: cv.putText(im, "it is circle", (cx, cy - 10), font, 0.5, (0, 0, 255), 1, cv.LINE_4) # 끝점 구하기 leftmost = tuple(_cnt[_cnt[:, :, 0].argmin()][0]) rightmost = tuple(_cnt[_cnt[:, :, 0].argmax()][0]) topmost = tuple(_cnt[_cnt[:, :, 1].argmin()][0]) bottommost = tuple(_cnt[_cnt[:, :, 1].argmax()][0]) print(_cnt[_cnt[:, :, 0].argmax()]) print(leftmost, rightmost) cv.circle(im, leftmost, 16, (0, 255, 0), -1) cv.circle(im, rightmost, 16, (0, 255, 0), -1) cv.circle(im, topmost, 16, (0, 255, 0), -1) cv.circle(im, bottommost, 16, (0, 255, 0), -1)
def image_callback(self, image): # TODO: get frame, find red pixel frame = self.bridge.imgmsg_to_cv2(image, "bgr8") frame = imutils.resize(frame, width=600) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, redLower, redUpper) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) # find contours # (x, y) cetner of ball cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None if len(cnts) > 0: # find the largest contour on the mask and use it to calc circle c = max(cnts, key=cv2.contourArea) ((self.ball_x, self.ball_y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # minimum radius if radius > 10: # draw circle and center # update tracked pts self.ball_found = True cv2.circle(frame, (int(self.ball_x), int(self.ball_y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) else: self.ball_found = False else: self.ball_found = False # update the pts queue self.pts.appendleft(center) # loop over tracked pts for i in xrange(1, len(self.pts)): # if the points are None, ignore if self.pts[i - 1] is None or self.pts[i] is None: continue # otherwise, compute thickness of the line, draw connecting line thickness = int(np.sqrt(pts_buffer / float(i + 1)) * 2.5) cv2.line(frame, self.pts[i - 1], self.pts[i], (0, 0, 255), thickness) vel_msg = Twist() vel_msg.linear.x = 0 vel_msg.linear.y = 0 vel_msg.linear.z = 0 vel_msg.angular.z = 0 if self.ball_found: if self.ball_x < (self.center_x - self.center_zone): vel_msg.angular.z = self.speed cv2.putText(frame, "ACTION: turn left", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 255)) elif self.ball_x > (self.center_x + self.center_zone): vel_msg.angular.z = -self.speed cv2.putText(frame, "ACTION: turn right", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 255)) else: vel_msg.angular.z = 0 cv2.putText(frame, "ACTION: tracked", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 255)) else: vel_msg.angular.z = 0 cv2.putText(frame, "ACTION: ball not found", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 255)) self.pub_cmd_vel.publish(vel_msg) # show the frame to the screen cv2.imshow("Frame", frame) cv2.waitKey(3)
#cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] center = None go = False # only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(cnts, key=cv2.contourArea) x, y, w, h = cv2.boundingRect(c) ((a, b), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # finding the extreme values extLeft = tuple(c[c[:, :, 0].argmin()][0]) extRight = tuple(c[c[:, :, 0].argmax()][0]) extTop = tuple(c[c[:, :, 1].argmin()][0]) extBot = tuple(c[c[:, :, 1].argmax()][0]) cv2.circle(frame, extLeft, 5, (0, 0, 255), -1) #red left point cv2.circle(frame, extRight, 5, (0, 255, 0), -1) #green right point cv2.circle(frame, extTop, 5, (255, 0, 0), -1) #blue top point cv2.circle(frame, extBot, 5, (255, 255, 0), -1) #teal bottom point cv2.circle(frame, (170, 143), 5, (0, 0, 255), -1) #red left point
x3_prev = x3 y3_prev = y3 """ edited by yash jain 13/6 4:54 """ # find contours in the mask and initialize the current # (x, y) center of the ball cnts = cv2.findContours(green.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[1] #image, contours, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #if imutils.is_cv2() else cnts[1] center = None if (len(cnts) > 0 and area_green > 300): c = max(cnts, key=cv2.contourArea) ((xc, yc), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) if (M["m00"] != 0): center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) pts.appendleft(center) dynamic_bool = 0 if radius > 10: # draw the circle and centroid on the frame, # then update the list of tracked points cv2.circle(img, (int(xc), int(yc)), int(radius), (0, 255, 255), 2) cv2.circle(img, center, 5, (0, 0, 255), -1) # loop over the set of tracked points dynamic_square = 0 for i in range(1, len(pts)): # if either of the tracked points are None, ignore
def callback(self, ros_data): '''Callback function of subscribed topic. Here images get converted and features detected''' if VERBOSE : print 'received image of type: "%s"' % ros_data.format #### direct conversion to CV2 #### np_arr = np.fromstring(ros_data.data, np.uint8) image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) # Defining RED range redLower = (160, 100, 100) redUpper = (179, 255, 255) blurred = cv2.GaussianBlur(image_np, (11, 11), 0) hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, redLower, redUpper) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) center = None # Only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # centroid coordinates # Only proceed if the radius meets a minimum size if radius > 10: # draw the circle and centroid on the frame, # then update the list of tracked points cv2.circle(image_np, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(image_np, center, 5, (0, 0, 255), -1) # Image Points coordinates float_center = np.float32(center) float_center1 = ([float_center[0]+radius, float_center[1]]) float_center2 = ([float_center[0]-radius, float_center[1]]) float_center3 = ([float_center[0], float_center[1]-radius]) float_center4 = ([float_center[0], float_center[1]+radius]) image_point = np.array([float_center1, float_center2, float_center3, float_center4]) # Correspondent coordinates of the object points in the image frame # Calling solvePnP to compute T-matrix between the object frame and the camera (_, rotation_vector, translation_vector) = cv2.solvePnP(self.object_point, image_point , self.camera_matrix, self.dist_coefs) # Printing translation vector print 'Object position: ' print translation_vector # Publishing the translation vector self.translation_pub.publish(float(translation_vector[0]), float(translation_vector[1]), float(translation_vector[2])) # update the points queue cv2.imshow('window', image_np) cv2.waitKey(2)
def connected(image): #image = cv2.imread("processed/2.tif") gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #blurred = cv2.GaussianBlur(gray, (11, 11), 0) # threshold the image to reveal light regions in the # blurred image #thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1] # perform a series of erosions and dilations to remove # any small blobs of noise from the thresholded image #thresh = cv2.erode(thresh, None, iterations=2) #thresh = cv2.dilate(thresh, None, iterations=5) # perform a connected component analysis on the thresholded # image, then initialize a mask to store only the "large" # components labels = measure.label(gray, neighbors=8, background=0) mask = np.zeros(gray.shape, dtype="uint8") height, width, channels = image.shape # loop over the unique components for label in np.unique(labels): # if this is the background label, ignore it if label == 0: continue # otherwise, construct the label mask and count the # number of pixels labelMask = np.zeros(gray.shape, dtype="uint8") labelMask[labels == label] = 255 numPixels = cv2.countNonZero(labelMask) # if the number of pixels in the component is sufficiently # large, then add it to our mask of "large blobs" if numPixels > 100: mask = cv2.add(mask, labelMask) # find the contours in the mask, then sort them from left to # right cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] cnts = contours.sort_contours(cnts)[0] # loop over the contours for (i, c) in enumerate(cnts): rect = cv2.minAreaRect(c) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(img, [box], 0, (0, 0, 255), 20) # draw the bright spot on the image (x, y, w, h) = cv2.boundingRect(c) if w < width * 0.60 or h < height * 0.60: continue cv2.imshow("rec", img) cv2.waitKey(0) cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) ((cX, cY), radius) = cv2.minEnclosingCircle(c) """if height/2 > radius >= height*0.25: cv2.circle(image, (int(cX), int(cY)), int(radius), (0, 0, 255), 3) cv2.putText(image, "#{}".format(i + 1), (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) cv2.line(image, (int(cX), 0), (int(cX), height), (255, 0, 0), 5)""" # return the output image return image
cv2.imshow("original", img) ret, thresh = cv2.threshold(cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY) image, contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in contours: x, y, w, h = cv2.boundingRect(c) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) rect = cv2.minAreaRect(c) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(img, [box], 0, (0, 255, 0), 2) (x, y), radius = cv2.minEnclosingCircle(c) center = (int(x), int(y)) radius = int(radius) img = cv2.circle(img, center, radius, (0, 0, 255), 2) epsilon = 0.01 * cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, epsilon, True) hull = cv2.convexHull(c) length_hull = len(hull) - 2 if length_hull > 2: for i in range(0, len(hull) - 2): cv2.line(img, (hull[i][0][0], hull[i][0][1]), (hull[i + 1][0][0], hull[i + 1][0][1]), (255, 255, 0), 2) cv2.line(img, (hull[0][0][0], hull[0][0][1]), (hull[i + 1][0][0], hull[i + 1][0][1]), (255, 255, 0), 1)
(cnts, hierarchy) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) print(f'Found {len(cnts)} coins in this image.') # Create a copy of the image since we want to preserve the original. coins = image.copy() # Args: image we want to draw on, list of contours, contour index (-1 indicates that we want all contours), color, and thickness of line. cv2.drawContours(coins, cnts, -1, (0, 255, 0), 2) cv2.imshow('Coins', coins) cv2.waitKey(0) # Crop each individual coin. for (i, c) in enumerate(cnts): # Create a box that encloses the contour. # This function takes in the contour and returns the x and y position that the rectangle starts at, and its width and height. (x, y, w, h) = cv2.boundingRect(c) print(f'Coin #{i+1}') # Display image of the box enclosure of the contour. coin = image[y:y + h, x:x + w] cv2.imshow('Coin', coin) mask = np.zeros(image.shape[:2], dtype='uint8') # This function fits a circle to our contour - returns x and y coordinate of the center of the circle and its radius. ((centerX, centerY), radius) = cv2.minEnclosingCircle(c) # Draw a circle that will mask our image of the box enclosure so that we only see the coin itself. cv2.circle(mask, (int(centerX), int(centerY)), int(radius), 255, -1) mask = mask[y:y + h, x:x + w] cv2.imshow('Masked Coin', cv2.bitwise_and(coin, coin, mask=mask)) cv2.waitKey(0)
ball_mask = cv2.dilate(ball_mask, None, iterations=2) # find contours in the mask and initialize the current # (x, y) center of the ball ball_cnts = cv2.findContours(ball_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) ball_cnts = imutils.grab_contours(ball_cnts) center = None # only proceed if at least one contour was found if len(ball_cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid ball_c = max(ball_cnts, key=cv2.contourArea) ((ball_x, ball_y), radius) = cv2.minEnclosingCircle(ball_c) M = cv2.moments(ball_c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # only proceed if the radius meets a minimum size if radius > 5: # draw the circle and centroid on the frame, # then update the list of tracked points diameter = radius * 2 dist_from_img_centre = ball_x - image_width / 2 ball_distance = (ball_width * focal_length / diameter) ball_tan_angle = 2 * dist_from_img_centre * tan( 0.5 * fov) / image_width ball_angle = atan(ball_tan_angle) print "-----------------------------------------" print "ball_x: " + str(dist_from_img_centre) + "ball_y: " + str(
def ball_track(image, configIO): vectPub = configIO[0] errPub = configIO[1] cvBridge = configIO[2] # cv2.createTrackbar('H low','Object Tracker',0,255, nothing) # cv2.createTrackbar('S low','Object Tracker',0,255, nothing) # cv2.createTrackbar('V low','Object Tracker',0,255, nothing) # cv2.createTrackbar('H high','Object Tracker',0,255, nothing) # cv2.createTrackbar('S high','Object Tracker',0,255, nothing) # cv2.createTrackbar('V high','Object Tracker',0,255, nothing) # # hLow = cv2.getTrackbarPos('H low', 'Object Tracker') # sLow = cv2.getTrackbarPos('S low', 'Object Tracker') # vLow = cv2.getTrackbarPos('V low', 'Object Tracker') # hHigh = cv2.getTrackbarPos('H high', 'Object Tracker') # sHigh = cv2.getTrackbarPos('S high', 'Object Tracker') # vHigh = cv2.getTrackbarPos('V high', 'Object Tracker') mv_image = imagecb(image, cvBridge) # greenLower = (hLow, sLow, vLow) # greenUpper = (hHigh, sHigh, vHigh) greenLower = (0, 0, 15) greenUpper = (0, 0, 34) mask = cv2.inRange(mv_image, greenLower, greenUpper) # mask = cv2.erode(mask, None, iterations=2) # mask = cv2.dilate(mask, None, iterations=2) # v = cam_model.projectPixelTo3dRay((400, 400)) circcont = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] outvect = Vector3() if (len(circcont) > 0): c = max(circcont, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) try: center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) print center except ZeroDivisionError: pass cv2.circle(mask, (int(x), int(y)), int(radius), (240, 255, 255), 2) v = cam_model.projectPixelTo3dRay((x, y)) outvect.x = v[0] outvect.y = v[1] outvect.z = v[2] vectPub.publish(outvect) # print outvect cent_err = (x - 376, y - 240) err_msg = Int16MultiArray() err_msg.data = cent_err errPub.publish(err_msg) # toServos.publish(err_msg) # print cent_err # print outvect # print radius # print x, y # print vect cv2.imshow('Object Tracker', mask) cv2.waitKey(1)
ret, frame = cap.read() tab_image.append(frame) i += 1 image_back = meth.moyenne_images(tab_image) i = 0 while True: ret, frame = cap.read() tickmark = cv2.getTickCount() mask = meth.calcul_mask(frame, image_back, seuil) elements = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] for e in elements: ## contour_area = cv2.contourArea(e) ## if (contour_area > MIN_CONTOUR_AREA) : ((x, y), rayon) = cv2.minEnclosingCircle(e) if rayon > SEILLE_RAYON: cv2.circle(frame, (int(x) + xmin, int(y) + ymin), 5, color_infos, 10) calcule_matricule(frame) i = 0 else: fps = cv2.getTickFrequency() / (cv2.getTickCount() - tickmark) cv2.putText(frame, "FPS: {:05.2f} Seuil: {:d}".format(fps, seuil), (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, color_infos, 1) cv2.imshow('frame', frame) i += 1 if (i == 50):
def cntsearch(frameorg, state): list_prevkalman = state list_currentkalman = [] list_matched = [] frame = frameorg gray = cv2.cvtColor(frameorg, cv2.COLOR_BGR2GRAY) corners = cv2.goodFeaturesToTrack(gray, 0, 0.0001, 0.01) ctc = np.zeros(gray.shape) for i in corners: x, y = i.ravel() ctc[int(y)][int(x)] = 1 t2 = close_detect_help.get_cnt(frame) #######IoU frame = cv2.GaussianBlur(frame, (11, 11), 0) contourlist = [] for cnt in t2: rects_temp = cv2.boundingRect(cnt) rects = np.array( [[rects_temp[0], rects_temp[1]], [rects_temp[0] + rects_temp[2], rects_temp[1]], [rects_temp[0] + rects_temp[2], rects_temp[1] + rects_temp[3]], [rects_temp[0], rects_temp[1] + rects_temp[3]]]) rects = rects.reshape(cnt.shape) center = np.sum(rects, axis=0) / 4 rects = np.maximum((0.5 * (rects - center) + center).astype(int), np.zeros(rects.shape)).astype(int) rects_temp = cv2.boundingRect(rects) rectl = np.maximum((3 * (rects - center) + center).astype(int), np.zeros(rects.shape)).astype(int) rectl_temp = cv2.boundingRect(rectl) rectl_temp = [ rectl_temp[0], rectl_temp[1], rectl_temp[2], rectl_temp[3] ] if rectl_temp[0] + rectl_temp[2] >= ctc.shape[1]: rectl_temp[2] = ctc.shape[1] - rectl_temp[0] - 1 if rectl_temp[1] + rectl_temp[3] >= ctc.shape[0]: rectl_temp[3] = ctc.shape[0] - rectl_temp[1] - 1 sel = np.ix_( np.arange(rects_temp[1], rects_temp[1] + rects_temp[3]).tolist(), np.arange(rects_temp[0], rects_temp[0] + rects_temp[2]).tolist()) selc = np.ix_( np.arange(rects_temp[1], rects_temp[1] + rects_temp[3]).tolist(), np.arange(rects_temp[0], rects_temp[0] + rects_temp[2]).tolist(), [0, 1, 2]) acs = np.sum(frameorg[selc], axis=(0, 1)).astype( np.float) / rects_temp[2] / rects_temp[3] fcs = np.sum(ctc[(sel)]) vs = np.var(frame[selc]) sel = np.ix_( np.arange(rectl_temp[1], rectl_temp[1] + rectl_temp[3]).tolist(), np.arange(rectl_temp[0], rectl_temp[0] + rectl_temp[2]).tolist()) selc = np.ix_( np.arange(rectl_temp[1], rectl_temp[1] + rectl_temp[3]).tolist(), np.arange(rectl_temp[0], rectl_temp[0] + rectl_temp[2]).tolist(), [0, 1, 2]) fcl = np.sum(ctc[(sel)]) acl = np.sum(frameorg[selc], axis=(0, 1)).astype( np.float) / rectl_temp[2] / rectl_temp[3] d = fcs * rectl_temp[2] * rectl_temp[3] / fcl / rects_temp[ 2] / rects_temp[3] t = (acs - acl) / 255.0 t = t * t if (d < 1 or fcl == 0) and np.sum(t) > 0.04: # print(vs) contourlist.append(cnt) cv2.drawContours(frameorg, [rectl, rects], -1, (0, 255, 0), 1) for cnt in contourlist: ((cX, cY), radius) = cv2.minEnclosingCircle(cnt) cl = np.array([cX, cY]) matched = False R = 0 for (x, P), dc, mc, _ in list_prevkalman: br = False for (x2, P2) in list_matched: if (x == x2).all() and (P == P2).all(): br = True break if br: continue lcv = P[:2, :2] t = np.array([cX - x[0][0], cY - x[1][0]]).reshape((1, 2)) lpp = np.matmul(np.matmul(t, lcv), t.T) if lpp < 50000: matched = True list_matched.append((x, P)) list_currentkalman.append((kalman_xy(x, P, cl, R), dc + 1, 0, cnt)) if not matched: x = np.matrix('0. 0. 0. 0.').T P = np.matrix(np.eye(4)) * 10 list_currentkalman.append((kalman_xy(x, P, cl, R), 1, 0, cnt)) contourlist = [] for (x, P), dc, mc, cnt in list_prevkalman: br = False for (x2, P2) in list_matched: if (x == x2).all() and (P == P2).all(): br = True break if not br: if mc <= 3: # see this, this is to remove the rong kalman list_currentkalman.append(((x, P), dc, mc + 1, cnt)) for (x, P), dc, mc, cnt in list_currentkalman: if dc >= 2 and mc <= 0: # to finalise the kalman that it is correct contourlist.append(cv2.boundingRect(cnt)) list_prevkalman = list_currentkalman[:] return contourlist, list_prevkalman
#contouring filterd = cv.bitwise_and(frame, frame, mask=medianed) im2, contours, hierarchy = cv.findContours(medianed, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) #countor cordinants try: (x1, y1) = (0, 0) cntx = 9999 cnty = 9999 #countor cordinants and circling if len(contours) > 0: cnt = max(contours, key=cv.contourArea) (x1, y1), radius = cv.minEnclosingCircle(cnt) center = (int(x1), int(y1)) radius = int(radius) x, y, w, h = cv.boundingRect(cnt) cv.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv.circle(frame, center, radius, (0, 0, 255), 2) cv.circle(frame, center, 2, (255, 0, 0), 4) cntx = int(x1) cnty = int(y1) if (cntx == 9999 or cnty == 9999): notfound + 3 else: errx = mrkzx - cntx erry = mrkzy - cnty #servo stuff
def main(): # change camera setup here filter_name = "Gawang" camera = cv2.VideoCapture(MAIN_CAMERA, cv2.CAP_DSHOW) range_filter = 'HSV' setup_trackbars(range_filter, filter_name) centerX = 319 centerY = 239 cv2.createTrackbar("Focus", filter_name + "Trackbars", 0, 255, callback) while True: focus = cv2.getTrackbarPos("Focus", filter_name + "Trackbars") camera.set(28, focus) ret, image = camera.read() image = cv2.flip(image, 1) if not ret: break frame_to_thresh = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) kernel = np.ones((5, 5), np.uint8) #! find mask Gawang v1_min, v2_min, v3_min, v1_max, v2_max, v3_max, focus = get_trackbar_values( range_filter, "Gawang") thresh = cv2.inRange(frame_to_thresh, (v1_min, v2_min, v3_min), (v1_max, v2_max, v3_max)) maskGawang = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) maskGawang = cv2.morphologyEx(maskGawang, cv2.MORPH_CLOSE, kernel) #! find contours in the mask and initialize the current # (x, y) center of the Gawang cnts = cv2.findContours(maskGawang.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None # only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # only proceed if the radius meets a minimum size if radius > 5: # draw the circle and centroid on the frame, # then update the list of tracked points cv2.circle(image, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(image, center, 3, (0, 0, 255), -1) cv2.putText(image, "Gawang", (center[0] + 10, center[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 1) cv2.putText(image, "(" + str(center[0]) + "," + str(center[1]) + ")", (center[0] + 10, center[1] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 1) # * show the frame to our screen cv2.imshow("Original", image) cv2.imshow("Mask", maskGawang) # * Ketika Tombol ditekan # S untuk save data # O untuk load data # r untuk reset # esc untuk nutup program key = cv2.waitKey(1) if key == 27: break elif key == 115: pickle.dump(get_trackbar_values(range_filter, filter_name), open("data_gawang3.dat", "wb")) elif key == 111: data_gawang = pickle.load(open("data_gawang3.dat", "rb")) print(data_gawang) set_trackbar_values(data_gawang, filter_name) elif key == 114: # v1_min, v2_min, v3_min, v1_max, v2_max, v3_max, focus setValue = 0, 0, 0, 255, 255, 255, 80 set_trackbar_values(setValue, filter_name) print('succes reset')
def extract(img): hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) hsv_img = cv2.medianBlur(hsv_img, 5) hsv_img = cv2.medianBlur(hsv_img, 5) hsv_img = cv2.medianBlur(hsv_img, 5) hsv_img = cv2.medianBlur(hsv_img, 5) hsv_img = cv2.medianBlur(hsv_img, 5) hsv_img = cv2.medianBlur(hsv_img, 5) hsv_img = cv2.medianBlur(hsv_img, 5) #hsv_img = cv2.blur(hsv_img,(5,5)) #hsv_img = cv2.GaussianBlur(hsv_img,(5,5),0) #hsv_img = cv2.bilateralFilter(hsv_img,9,75,75) lower_hsv = np.array([11, 43, 150]) upper_hsv = np.array([28, 255, 255]) mask = cv2.inRange(hsv_img, lowerb=lower_hsv, upperb=upper_hsv) #获得提取黄色后的图像 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1) mask = cv2.medianBlur(mask, 5) mask = cv2.medianBlur(mask, 5) #ret, thresh = cv2.threshold(cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY), # 200, 255, cv2.THRESH_BINARY) #cv2.imshow('one',mask) #cv2.waitKey() #cv2.destroyAllWindows() #findContour用于找到不规则形状的轮廓 img11, contours, hier = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) print(len(contours)) count = 0 for contour in contours: #计算一个简单的边界框 x, y, w, h = cv2.boundingRect(contour) #画出边界框 cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2) #计算包围目标的最小矩形区域 rect = cv2.minAreaRect(contour) box = cv2.boxPoints(rect) box = np.int0(box) # cv2.drawContours(img, [box], 0, (255,0,255), 2) # cv2.minEnclosingCircle函数返回一个二元组,第一个元组为圆心坐标,第二个为半径 (x0, y0), radius = cv2.minEnclosingCircle(contour) if radius < 10: continue else: center = (int(x0), int(y0)) radius = int(radius) img = cv2.circle(img, center, radius, (0, 255, 255)) count = count + 1 point_list_l = [] point_list_r = [] last_y = 0 for pose in contours[0]: x = (pose[0][0]) y = pose[0][1] if y < last_y: break else: point = (x, y) point_list_r.append(point) # print (pose) last_y = y last_y = 0 for pose in contours[1]: x = (pose[0][0]) y = pose[0][1] if y < last_y: break else: point = (x, y) point_list_l.append(point) # print (pose) last_y = y print(len(point_list_l)) print(len(point_list_r)) for dian in point_list_l: cv2.circle(img, dian, 1, (0, 0, 255), 4) for dian in point_list_r: cv2.circle(img, dian, 1, (0, 0, 255), 4) #cv2.drawContours(img, contours, -1, (255,0,0), 1) cv2.imshow("findcontour", img) cv2.waitKey() cv2.destroyAllWindows()
# ex: now_color = 0(yellow)이면, # 노란색에 해당하는 부분은 흰색으로, 그 외는 검정색으로 처리 # mask = cv2.erode(mask, None, iterations=1) # mask = cv2.dilate(mask, None, iterations=1) # mask = cv2.GaussianBlur(mask, (3, 3), 2) # softly # now_color와 동일한 색의 물체의 contour 정보를 cnts에 저장 # now_color와 동일한 색의 물체가 없으면 cnts에 아무 값도 저장 안 됨(NULL) cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None if len(cnts) > 0: # cnts에 저장된 값이 있으면(now_color와 동일한 색의 물체가 있으면) 아래코드 수행 c = max(cnts, key=cv2.contourArea) ((X, Y), radius) = cv2.minEnclosingCircle(c) Area[i] = int(cv2.contourArea(c) / min_area[now_color]) # 물체의 넓이 구하기 if Area[i] > min_area[now_color]: # 물체의 넓이가 임계값보다 크면 아래 코드 수행 minRect = cv2.minAreaRect(c) # 물체에 접하는 사각형 정보를 minRect에 저장 box = cv2.boxPoints(minRect) # 사각형의 4 꼭짓점을 box에 저장 box = np.int0(box) # 꼭짓점의 좌표를 float -> integer형으로 변환 # 물체에 접하는 사각형(contour) 그리기 cv2.drawContours(frame_roi, [box], -1, color_bgr[i], 3) # 화면에 contour의 꼭짓점 표시 cv2.circle(frame_roi, (box[0][0], box[0][1]), 10, color_bgr[i], -1) cv2.circle(frame_roi, (box[1][0], box[1][1]), 10, color_bgr[i], -1) cv2.circle(frame_roi, (box[2][0], box[2][1]), 10, color_bgr[i], -1)
cv2.CHAIN_APPROX_SIMPLE) cnts_up = imutils.grab_contours(cnts_up) center_up = None cnts_down = cv2.findContours(down_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts_down = imutils.grab_contours(cnts_down) center_down = None # only proceed if at least one contour was found if len(cnts_up) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and centroid c = max(cnts_up, key=cv2.contourArea) #find circle of minimum area eclosing a 2D point set ((x, y), radius) = cv2.minEnclosingCircle(c) #The function cv2.moments() gives a dictionary of all moment values calculated. #Moments can be used to calculate COM,area,centroid,etc M = cv2.moments(c) # find the center from the moments 0.000001 is added to the denominator so that divide by # zero exception doesn't occur center_up = (int(M["m10"] / (M["m00"] + 0.000001)), int(M["m01"] / (M["m00"] + 0.000001))) # only proceed if the radius meets a minimum size if radius > circle_radius: # draw the circle and centroid on the frame, cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center_up, 5, (0, 0, 255), -1) #TOP LEFT is "A" key pressed and TOP RIGHT is for "D" key pressed
def Range(img, parameters_dict, finalimage): Range = np.array([]) ZDistance = np.array([]) Bearing = np.array([]) Center = np.array([]) #LWidth=np.array([]) #LHeight=np.array([]) #GrayFiltimg=cv2.cvtColor(img,cv2.COLOR_HSV2BGR) #GrayFiltimg=cv2.cvtColor(GrayFiltimg,cv2.COLOR_RGB2GRAY) Contour = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if Contour == []: print("there is nothing here") else: Contour = imutils.grab_contours(Contour) for a in Contour: #find the center of the contour Moment = cv2.moments(a) Area = cv2.contourArea(a) if parameters_dict["Circle"] == True: Lx1, Ly1, LWidth, LHeight = cv2.boundingRect(a) if Area > 30: if LWidth / LHeight < 1.1 and LHeight / LWidth < 1.1: (x, y), radius = cv2.minEnclosingCircle(a) cv2.rectangle(finalimage, (int(x - radius), int(y + radius)), (int(x + radius), int(y - radius)), parameters_dict["BBoxColour"], 2) Distance = (parameters_dict["Height"] * (f / (2 * radius)) / 8) * math.cos(0.2967) Distance = (-0.0005 * Distance**2) + (1.4897 * Distance) - 66.919 Distance = Distance / 1000 ZDistance = np.append(ZDistance, Distance) Bearing = np.append( Bearing, math.radians((x - 160) * (31.1 / 160))) Range = np.vstack( (ZDistance, -Bearing) ).T #Put Bearing and ZDistance into one array and arrange #columnwise Range = Range[Range[:, 0].argsort()] else: continue else: continue elif parameters_dict["type"] == 3: Lx1, Ly1, LWidth, LHeight = cv2.boundingRect(a) if Area > 150 and Area < 5000: if LWidth / LHeight < 1.2 and LHeight / LWidth < 1.2: Lx = int(Moment["m10"] / Moment["m00"]) Ly = int(Moment["m01"] / Moment["m00"]) Centroid = np.array([Lx, Ly]) Center = np.append(Center, Centroid) cv2.rectangle( finalimage, (Lx - int(LWidth / 2), Ly + int(LHeight / 2)), (Lx + int(LWidth / 2), Ly - int(LHeight / 2)), parameters_dict["BBoxColour"], 2) Distance = (parameters_dict["Height"] * (f / LHeight) / 8) * math.cos(0.2967) Distance = ((-0.0002 * Distance**2) + (0.8492 * Distance) + 51) / 1000 ZDistance = np.append(ZDistance, Distance) MaxMinLocations(a, finalimage) Bearing = np.append( Bearing, math.radians((Lx - 160) * (31.1 / 160))) Range = np.vstack( (ZDistance, -Bearing) ).T #Put Bearing and ZDistance into one array and arrange #columnwise Range = Range[Range[:, 0].argsort()] #if positive then it's to the right if negative then to left of center else: continue else: continue elif parameters_dict["type"] == 2: Lx1, Ly1, LWidth, LHeight = cv2.boundingRect(a) if Area > 150: if LWidth / LHeight < 1.2 and LHeight / LWidth < 1.2: Lx = int( Moment["m10"] / Moment["m00"]) #centroids of shapes identified Ly = int(Moment["m01"] / Moment["m00"]) Centroid = np.array([Lx, Ly]) Center = np.append(Center, Centroid) cv2.rectangle( finalimage, (Lx - int(LWidth / 2), Ly + int(LHeight / 2)), (Lx + int(LWidth / 2), Ly - int(LHeight / 2)), parameters_dict["BBoxColour"], 2) Distance = (parameters_dict["Height"] * (f / LHeight) / 8) * math.cos(0.2967) Distance = (262.22 * np.log(Distance) - 1222.1) / 1000 ZDistance = np.append(ZDistance, Distance) MaxMinLocations(a, finalimage) Bearing = np.append( Bearing, math.radians((Lx - 160) * (31.1 / 160))) Range = np.vstack( (ZDistance, -Bearing) ).T #Put Bearing and ZDistance into one array and arrange #columnwise Range = Range[Range[:, 0].argsort()] #if positive then it's to the right if negative then to left of center else: continue else: continue elif parameters_dict["type"] == 1: Lx1, Ly1, LWidth, LHeight = cv2.boundingRect(a) if Area > 3000: Lx = int(Moment["m10"] / Moment["m00"]) Ly = int(Moment["m01"] / Moment["m00"]) Centroid = np.array([Lx, Ly]) Center = np.append(Center, Centroid) cv2.rectangle( finalimage, (Lx - int(LWidth / 2), Ly + int(LHeight / 2)), (Lx + int(LWidth / 2), Ly - int(LHeight / 2)), parameters_dict["BBoxColour"], 2) Distance = (parameters_dict["Height"] * (f / LHeight) / 8) * math.cos(0.2967) Distance = 0.8667 * Distance - 3 ZDistance = np.append(ZDistance, Distance) #self.MaxMinLocations(a,finalimage) Bearing = np.append( Bearing, math.radians((Lx - 160) * (31.1 / 160))) Range = np.vstack( (ZDistance, -Bearing) ).T #Put Bearing and ZDistance into one array and arrange #columnwise Range = Range[Range[:, 0].argsort()] else: continue elif parameters_dict["type"] == 4: Lx1, Ly1, LWidth, LHeight = cv2.boundingRect(a) if Area > 30 and Area < 3000: if LWidth / LHeight < 1.1 and LHeight / LWidth < 1.1: (x, y), radius = cv2.minEnclosingCircle(a) cv2.rectangle(finalimage, (int(x - radius), int(y + radius)), (int(x + radius), int(y - radius)), parameters_dict["BBoxColour"], 2) Distance = (parameters_dict["Height"] * (f / (2 * radius)) / 8) * math.cos(0.2967) Distance = (-0.0005 * Distance**2) + (1.4897 * Distance) - 66.919 Distance = Distance / 1000 ZDistance = np.append(ZDistance, Distance) Bearing = np.append( Bearing, math.radians((x - 160) * (31.1 / 160))) Range = np.vstack( (ZDistance, -Bearing) ).T #Put Bearing and ZDistance into one array and arrange #columnwise Range = Range[Range[:, 0].argsort()] else: continue else: continue return Range
def grade_img(self, img_path, number): # Choose image based on the image path. cimg = cv2.imread(img_path, 1) # Convert image from BGR to HSV. hsv = cv2.cvtColor(cimg, cv2.COLOR_BGR2HSV) # Define lower and upper boundaries of orange in HSV. # HSV uses max values as H: 180, S: 255, V: 255 for HSV. # This compares to the normal max values of H: 360, S: 100, V: 100. # Setting upper and lower bounds for first mask. mask = cv2.inRange(hsv, self.orange_lower_1, self.orange_upper_1) # Bitwise-AND mask and original image (combine the two). result = cv2.bitwise_and(cimg, cimg, mask=mask) #cv2.imshow('res', result) # Save the result to the library to display on screen. if number == 1: cv2.imwrite('images/specimen_mask_1.jpg', result) self.img_pixmap = QtGui.QPixmap('images/specimen_mask_1.jpg') self.img_pixmap = self.img_pixmap.scaled(250, 250, QtCore.Qt.KeepAspectRatio) self.ui.pixmap_mask_1.setPixmap(self.img_pixmap) elif number == 2: cv2.imwrite('images/specimen_mask_2.jpg', result) self.img_pixmap = QtGui.QPixmap('images/specimen_mask_2.jpg') self.img_pixmap = self.img_pixmap.scaled(250, 250, QtCore.Qt.KeepAspectRatio) self.ui.pixmap_mask_2.setPixmap(self.img_pixmap) # Convert result to gray for contouring. gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY) # ret, thresh = cv2.threshold(gray, 127, 255, 3) result_2, contours, hierachy = cv2.findContours( gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #cv2.drawContours(result, contours, -1, (0, 255, 0), 1). specimen_area_1 = 0 specimen_area_2 = 0 # Loop through the contours found in the image. for c in contours: # If contour area is big enough, then continue with loop. if cv2.contourArea(c) <= 1500: continue # Largest contour will be area of the specimen. if number == 1: if cv2.contourArea(c) > specimen_area_1: specimen_area_1 = cv2.contourArea(c) self.area_of_specimen_1 = specimen_area_1 elif number == 2: if cv2.contourArea(c) > specimen_area_2: specimen_area_2 = cv2.contourArea(c) self.area_of_specimen_2 = specimen_area_2 # Add to number of defects. self.number_of_defects += 1 # Get the location and radius of a cirlce that covers the contour area. (x, y), radius = cv2.minEnclosingCircle(c) # Set center and radius values. center = (int(x), int(y)) radius = int(radius) # Draw circle on original image where contour was found. cv2.circle(cimg, center, radius, (0, 255, 0), 10) # Add area of contours to the area_of_defects to get total area of defects. if number == 1: self.area_of_defects_1 += cv2.contourArea(c) elif number == 2: self.area_of_defects_2 += cv2.contourArea(c) # Depending on which specimen is being analysed, save the image with suffix '1' or '2'. if number == 1: cv2.imwrite('images/specimen_result_1.jpg', cimg) self.img_pixmap = QtGui.QPixmap('images/specimen_result_1.jpg') self.img_pixmap = self.img_pixmap.scaled(250, 250, QtCore.Qt.KeepAspectRatio) self.ui.pixmap_result_1.setPixmap(self.img_pixmap) elif number == 2: cv2.imwrite('images/specimen_result_2.jpg', cimg) self.img_pixmap = QtGui.QPixmap('images/specimen_result_2.jpg') self.img_pixmap = self.img_pixmap.scaled(250, 250, QtCore.Qt.KeepAspectRatio) self.ui.pixmap_result_2.setPixmap(self.img_pixmap)
# In[ ]: diff = cv2.absdiff(cropped_dotted, cropped_raw) gray = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY) ret,th1 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU) cnts = cv2.findContours(th1.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2] print("Sea Lions Found: {}".format(len(cnts))) x, y = [], [] lion_patches = [] for loc in cnts: ((xx, yy), _) = cv2.minEnclosingCircle(loc) # store patches of some sea lions if xx > 10 and xx < gray.shape[1] - 10: lion_patches.append(cropped_raw[yy-10:yy+10, xx-10:xx+10]) x.append(xx) y.append(yy) x = np.array(x) y = np.array(y) # The code below will take each of the markers as individual coordinates and feed it to a 2D kernel density estimation using a very common kernel, the gaussian one. We then show the estimated density by showing it's contour plot. # # See [stackoverflow][1] for a very detailed explanation of the code below.
def thresh_callback(src): threshold = 190 src_temp = src.copy() src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) src_gray = cv2.blur(src_gray, (3, 3)) ## [Canny] # Detect edges using Canny canny_output = cv2.Canny(src_gray, threshold, threshold * 2) ## [Canny] # cv2.imshow('Contours', canny_output) # cv2.waitKey(0) ## [findContours] # Find contours (contours, _) = cv2.findContours(canny_output, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) ## [findContours] ## [allthework] # Approximate contours to polygons + get bounding rects and circles contours_poly = [None] * len(contours) boundRect = [None] * len(contours) centers = [None] * len(contours) radius = [None] * len(contours) for i, c in enumerate(contours): contours_poly[i] = cv2.approxPolyDP(c, 3, True) boundRect[i] = cv2.boundingRect(contours_poly[i]) centers[i], radius[i] = cv2.minEnclosingCircle(contours_poly[i]) ## [allthework] percentage = [0] * 200 ball = 0 for n in range(len(contours)): xmin_ball = int(boundRect[n][0]) ymin_ball = int(boundRect[n][1]) w_ball = int(boundRect[n][2]) h_ball = int(boundRect[n][3]) if 25 >= w >= 12 and 25 >= h >= 12 and ymin > 200: crop_img = src_temp[ymin_ball:ymin_ball + h_ball + 5, xmin_ball:xmin_ball + w_ball + 5] # cv2.imshow('Contours', crop_img) # cv2.waitKey(0) color, ratio = detect_ball(crop_img) # print(percentage) if color == 'ball': percentage[n] = ratio # print("color" , color) if any(percentage): ball = np.argmax(percentage) centers[ball] = [round(x) for x in centers[ball]] # cv2.rectangle(src, (int(boundRect[ball][0]), int(boundRect[ball][1])), \ # (int(boundRect[ball][0] + boundRect[ball][2]), int(boundRect[ball][1] + boundRect[ball][3])), # (0, 0, 255), 1) cv2.circle(src, (int(centers[ball][0]), int(centers[ball][1])), int(radius[ball]), (0, 0, 255), 2) # print("center", centers[ball]) if ball != 0: pts.appendleft(centers[ball]) for i in range(1, len(pts)): # if either of the tracked points are None, ignore # them if pts[i - 1] is None or pts[i] is None: continue
cv.imshow('CONTOUR', frame) cv.moveWindow('CONTOUR', 1400, 0) #get conture centers contours_poly = len(contours) center = len(contours) radius = len(contours) #find enclosing polygon which fits around the contures rMax = 1 origo = (100, 100) for i in range(0, len(contours)): cont = contours[i] approx = cv.approxPolyDP(cont, 2, True) #(x,y),radius = cv.minEnclosingCircle(approx) (x, y), radius = cv.minEnclosingCircle(cont) center = (int(x), int(y)) radius = int(radius) frameCircle = cv.circle(frame, center, radius, (255, 0, 0), 2) if x > 0 and x < 640 and y > 0 and y < 480: #ROI window for region of interes can be smaller (not the full window) if int(radius) > rMax: rMax = int(radius) origo = center print("rMax & origo: ") print(rMax, origo) frameCircle = cv.line(frame, (100, 200), (100, 100), (0, 255, 255), 2) # ideal line frameCircle = cv.line(frame, (100, 200), origo, (0, 0, 250), 2) # direction required
cntsr = imutils.grab_contours(cntsr) centerr = None cntsb = cv2.findContours(maskb.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cntsb = imutils.grab_contours(cntsb) centerb = None # only proceed if at least one contour was found if len(cntsg) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid cntsg = sorted(cntsg, key=cv2.contourArea, reverse=True) cg = cntsg[0] ((xg, yg), radiusg) = cv2.minEnclosingCircle(cg) Mg = cv2.moments(cg) if not (Mg["m00"] == 0 or Mg["m00"] == 0): centerg = (int(Mg["m10"] / Mg["m00"]), int(Mg["m01"] / Mg["m00"])) # only proceed if the radius meets a minimum size if radiusg > 10: #print(str(x) + "," + str(y)) if (calmode == 0): win32api.SetCursorPos( (int(remap(xg, 0, 640, 0, resw)), int(remap(yg, 70, 480, 0, resh)))) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) # draw the circle and centroid on the frame,
closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, np.ones((3, 3), np.uint8)) # 闭运算 (image, contours, hierarchy) = cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 找出轮廓 areaMaxContour, area_max = getAreaMaxContour(contours) # 找出最大轮廓 if areaMaxContour is not None: if area_max > max_area: # 找最大面积 max_area = area_max color_max = i areaMaxContour_max = areaMaxContour if max_area != 0: ((centerX, centerY), rad) = cv2.minEnclosingCircle(areaMaxContour_max) # 获取最小外接圆 centerX, centerY, rad = int(centerX), int(centerY), int( rad) # 获取圆心,半径 cv2.circle(orgframe, (centerX, centerY), rad, (0, 255, 0), 2) # 画出圆心 if color_max == 'red': # 红色最大 # print("red") Color_BGR = range_rgb["red"] if get_color is False: get_color = True elif color_max == 'green': # 绿色最大 Color_BGR = range_rgb["green"] # print("green") if get_color is False: get_color = True
def process_image(filename, test, show = False, debug = False): image = cv2.imread(filename) img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Incoming image is roughly 2k px wide and is cropped roughly to include only the bar codes, left & right grids & inner bottle area # Try and extract the left grid and right grid from the image img = cv2.medianBlur(img,1) threshold = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\ cv2.THRESH_BINARY,11,2) contours, _=cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) i = 0 bottle_enclosure_area = 100000000 font = cv2.FONT_HERSHEY_SIMPLEX inner_approx = None out = test + "_colors.csv" f = open(out, "w") f.write("R,G,B,Label\n") for cnt in contours: #Throw out any contours that are small or too big. We are only interested in large enough squares if cv2.arcLength(cnt,True) < 1000 or cv2.arcLength(cnt, True) > 5000: continue approx = cv2.approxPolyDP(cnt, 0.08*cv2.arcLength(cnt, True), True) x, y, w, h = cv2.boundingRect(approx) ar = w / float(h) #Drop the contours that are not squares, not four-sided and criss-crossed #This should roughly be the left and right outer contours. if w > image.shape[1] / 4 and len(approx) == 4 and ar >= 0.8 and ar <= 1.2 \ and cv2.pointPolygonTest(cnt, (x + w / 2, y + h / 2), False) > 0 \ and cv2.pointPolygonTest(cnt, (x + w / 3, y + h * 2 / 3), False) > 0: pts = np.zeros((4, 2), dtype = "float32") for idx in range(4): pts[idx] = approx[idx] warped = get_warped_image(image, pts) #Assuming that these are the outer grids, we can apply binary threshold #to get the inner grids xxx = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) thresh1 = cv2.adaptiveThreshold(xxx,255,cv2.ADAPTIVE_THRESH_MEAN_C,\ cv2.THRESH_BINARY,11,2) innerc, _=cv2.findContours(thresh1, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) yes = {0} xes = {0} xes.remove(0) yes.remove(0) rsum = 0 count = 0 for ic in innerc: if cv2.arcLength(ic,True) > 1000 or cv2.arcLength(ic,True) < 200: continue innera = cv2.approxPolyDP(ic, 0.08*cv2.arcLength(ic, True), True) xin, yin, win, hin = cv2.boundingRect(innera) arin = win / float(hin) #Let us try to locate the centroid of each grid (each of which are polygons with four sides #aspect ratio roughly equal to 1 and not criss-cross shapes. Once the centroids are #located, we can cluster the centroids and identify general x-coord of each column # and similarly identify the general y-coord of each row. Using which we can locate # all the 25 (x,y) of the inner grids if len(innera) == 4 and arin >= 0.8 and arin <= 1.2 \ and cv2.pointPolygonTest(ic, (xin + win / 2, yin + hin / 2), False) > 0 \ and cv2.pointPolygonTest(ic, (xin + win / 3, yin + hin * 2 / 3), False) > 0: (cx, cy), radius = cv2.minEnclosingCircle(ic) rsum += radius count += 1 if len(xes) == 0: xes.add(int(cx)) else: added = False for item in xes: if cx < item + 20 and cx > item - 20: added = True break if added == False: xes.add(int(cx)) if len(yes) == 0: yes.add(int(cy)) else: added = False for item in yes: if cy < item + 20 and cy > item - 20: added = True break if added == False: yes.add(int(cy)) xpos = 1 for xx in sorted(xes): for yy in sorted(yes): if labels[test][xpos] == -1: xpos += 1 continue rd = int(rsum*7/(12*count)) # Once we have located the centroid, we can create a bounding rectangle large enough to get # enough pixels, but smaller than the bounding black line to avoid color errors. Each pixel # is now a sample for learning, let us write these pixels along with labels for preparing # the learning set. cv2.rectangle(warped, (xx-rd, yy-rd), (xx+rd, yy+rd), (255, 255, 255)) cv2.putText(warped, str(labels[test][xpos]), (xx, yy), font, 1,(255,255,255),2,cv2.LINE_AA) for ycoord in range(xx-rd+1, xx+rd-1): for xcoord in range(yy-rd+1, yy+rd-1): f.write(str(warped[xcoord][ycoord][2]) + "," + str(warped[xcoord][ycoord][1]) + "," + str(warped[xcoord][ycoord][0]) + "," + test + str(labels[test][xpos]) + '\n') xpos+=1 if show: cv2.imshow("Warped" + str(i), warped) i = i + 1 else: #These are large enough polygons, but not the left and right grids, let us identify the bottle #enclosure and pick that if bottle_enclosure_area > cv2.contourArea(cnt) and len(approx) == 4 and x > image.shape[1]/3: inner_approx = approx bottle_enclosure_area = cv2.contourArea(cnt) ex, ey, ew, eh = cv2.boundingRect(inner_approx) ex = int(ex + ew / 3) ey = int(ey + eh / 2) ew = int(ew/3) eh = int(eh/4) cnt = 0 rs = 0 gs = 0 bs = 0 for bxcoord in range(ex, ex+ew): for bycoord in range(ey, ey+eh): cnt+=1 rs+=image[bycoord][bxcoord][2] gs+=image[bycoord][bxcoord][1] bs+=image[bycoord][bxcoord][0] if debug and show: cv2.rectangle(image, (ex, ey), (ex+ew, ey+eh), (255, 255, 255), 0) f.close() if show: cv2.imshow("img", image) cv2.waitKey(0) cv2.destroyAllWindows() return int(rs/cnt), int(gs/cnt), int(bs/cnt), out
centerBY = centerY if area > 1500: cv.circle(img_color, (centerX, centerY), 10, (255,0,0), 10) #cv.rectangle(img_color, (x,y), (x+width,y+height), (255,0,0)) cv.arrowedLine(img_color, (centerAX, centerAY), (centerBX, centerBY),(0,255,255) , 3) cnts = cv.findContours(img_maskA.copy(), cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)[-2] center = None if len(cnts) > 0: c = max(cnts, key=cv.contourArea) ((x, y), radius) = cv.minEnclosingCircle(c) M = cv.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # only proceed if the radius meets a minimum size cv.putText(img_color,"("+str(center[0])+","+str(center[1])+")"+"angle:"+str(getAngle(centerAX, centerAY, centerBX, centerBY)), (center[0]+10,center[1]+15), cv.FONT_HERSHEY_SIMPLEX, 0.8,(0, 0, 255),1) cv.imshow('img_result', img_result) cv.imshow('img_color', img_color) key = cv.waitKey(1) & 0xFF if key == 27: # esc
def recent_events(self, events): self.recent_fixation = None events['fixations'] = [] fs = self.g_pool.capture.frame_size gaze = events['gaze_positions'] self.queue.extend((gp for gp in gaze if gp['confidence'] > self.confidence_threshold)) try: # use newest gaze point to determine age threshold age_threshold = self.queue[-1]['timestamp'] - self.duration / 1000. while self.queue[1]['timestamp'] < age_threshold: self.queue.popleft() # remove outdated gaze points except IndexError: pass gaze_3d = [ gp for gp in self.queue if '3d' in gp['base_data'][0]['method'] ] use_3d = len(gaze_3d) > 0.8 * len(self.queue) if use_3d: base_data = gaze_3d points0 = [(pp['theta'], pp['phi']) for pp in chain(*(gp['base_data'] for gp in gaze_3d)) if pp['id'] == 0] points1 = [(pp['theta'], pp['phi']) for pp in chain(*(gp['base_data'] for gp in gaze_3d)) if pp['id'] == 1] points = np.array( points0 if len(points0) > len(points1) else points1, dtype=np.float32) else: base_data = list(self.queue) points = np.array( [denormalize(gp['norm_pos'], fs) for gp in base_data], dtype=np.float32) if points.shape[0] <= 2 or base_data[-1]['timestamp'] - base_data[0][ 'timestamp'] < self.duration / 1000.: self.recent_fixation = None return center, radius = cv2.minEnclosingCircle(points) radius *= 2 # all dispersion measures use the diameter instead of radius if use_3d and radius < np.deg2rad(self.dispersion_3d): norm_pos = np.mean([gp['norm_pos'] for gp in base_data], axis=0).tolist() method = '3d pupil angle [deg]' dispersion = np.rad2deg(radius) # in degrees elif not use_3d and radius < self.dispersion_2d: norm_pos = normalize(center, fs) method = '2d gaze [px]' dispersion = radius else: base_data = None if base_data: new_fixation = { 'topic': 'fixation', 'norm_pos': norm_pos, 'dispersion': dispersion, 'method': method, 'base_data': base_data, 'timestamp': base_data[0]['timestamp'], 'duration': base_data[-1]['timestamp'] - base_data[0]['timestamp'], 'confidence': float(np.mean([gp['confidence'] for gp in base_data])) } events['fixations'].append(new_fixation) self.recent_fixation = new_fixation else: self.recent_fixation = None
mask = cv2.erode(mask,kernel,iterations=2) mask = cv2.dilate(mask,kernel,iterations=2) #morphological closing mask = cv2.dilate(mask,kernel,iterations=2) mask = cv2.erode(mask,kernel,iterations=2) #Detect contours from the mask cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] if(len(cnts) > 0): #Contour with greatest area c = max(cnts,key=cv2.contourArea) #Radius and center pixel coordinate of the largest contour circles = [cv2.minEnclosingCircle(c) for c in cnts] for (x,y),radius in circles : if radius > 5: #Draw an enclosing circle cv2.circle(frame,(int(x), int(y)), int(radius),(0, 255, 255), 2) #Draw a line from the center of the frame to the center of the contour cv2.line(frame,(320,240),(int(x), int(y)),(0, 0, 255), 1) #Reference line cv2.line(frame,(320,0),(320,480),(0,255,0),1) radius = int(radius) #distance of the 'x' coordinate from the center of the frame
mask = cv2.dilate(mask, None, iterations=2) # find contours in the mask and initialize the current # (x, y) center of the contours cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None contours = orig.copy() cv2.drawContours(contours, cnts, -1, (0,255,0), 3) cv2.imshow("Contours", contours) # only proceed if at least one contour was found if len(cnts) > 0: for i in range(0, len(cnts)): # find the minimum enclosing circle in the contour ((x, y), radius) = cv2.minEnclosingCircle(cnts[i]) # Only proceed if radius is less than maximum size if radius < 10: # Find center coordinates M = cv2.moments(cnts[i]) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) # Used to ignore the sky (place on the lefthand side of the frame # because the sky is the same color as the headlights) if cX < 150: continue # If the headlight is on lefthand side, print left,
def detectGoal(): print("Check for goal!") # Initialize frame count frame_count = 0 FRAME_MAX = 5 RADIUS_MAX = 120 ##X_MIN = 163 ##X_MAX = 476 ##Y_MIN = 150 ##Y_MAX = 450 X_MIN = 256 X_MAX = 384 Y_MIN = 240 Y_MAX = 360 # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): frame_count = frame_count + 1 # grab the raw NumPy array representing the image, then initialize the timestamp # and occupied/unoccupied text # resize the frame, blur it, and convert it to the HSV # color space image = frame.array frame_im = imutils.resize(image, width=600) blurred = cv2.GaussianBlur(frame_im, (11, 11), 0) hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) threshLower = (8,0,8) threshUpper = (115,68,98) # construct a mask for the color "purple", then perform # a series of dilations and erosions to remove any small # blobs left in the mask mask = cv2.inRange(hsv, threshLower, threshUpper) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) ## cv2.imshow("mask",mask) # find contours in the mask and initialize the current # (x, y) center of the ball cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] center = None # only proceed if at least one contour was found else we exit if len(cnts) > 0: ## ser.close() # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # draw the circle and centroid on the frame, # then update the list of tracked points rawCapture.truncate(0) return 1 elif frame_count == FRAME_MAX: ## ser.close() # show the frame ## cv2.rectangle(image,(X_MIN,Y_MIN),(X_MAX,Y_MAX),(0,255,0),2) ## cv2.imshow("Frame",image ) rawCapture.truncate(0) return 0 ## if frame_count == FRAME_MAX: ## frame_count = 0 # clear the stream in preparation for the next frame rawCapture.truncate(0)