def findCircles(frame): global lastFrameWithCircle, pause, img original = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #switch to grayscale retval, image = cv2.threshold(original, 50, 255, cv2.cv.CV_THRESH_BINARY) el = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) image = cv2.dilate(image, el, iterations=4) image = cv2.GaussianBlur(image, (13, 13), 0) found = False alpha = 90 while not found: circles = cv2.HoughCircles(image, cv2.cv.CV_HOUGH_GRADIENT, 1.2, 100, param2=alpha) #find circles if circles is not None: # convert the (x, y) coordinates and radius of the circles to integers circles = np.round(circles[0, :]).astype("int") #check if the circles agree with previous data for x, y, r in circles: if normal(x, y, r): found = True circleCoords[currentFrame] = (x, y, r) # draw the circle in the output image, then draw a rectangle # corresponding to the center of the circle cv2.circle(frame, (x, y), r + 5, (228, 20, 20), 4) cv2.rectangle(frame, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1) lastFrameWithCircle = currentFrame if not found: alpha -= 5 if alpha <= 30: found = True else: #if no circles found then try again with new threshold (threshold stops at 30) alpha -= 5 if alpha <= 30: found = True #if we havent found a circle in more than 10 frames then ask the user for help if currentFrame - lastFrameWithCircle > 10: pause = True img = extra.feedback("Please click on the center of the circle", pause) return frame
def on_mouse(event, x, y, flags, params): #global rect,startPoint,endPoint global center, outside, currentFrame, circleCoords, lastFrameWithCircle, pause, length, height, width, fps, cap, img, first, points, ax, plot #get only left mouse click if event == cv2.EVENT_LBUTTONDOWN: #make sure click was in window if x < 0 or x > width or y < 0 or y > height: pass #only use if paused (paused when nothing is found)' if pause: #if second click (outside) if center is not None: outside = (x, y) if first is None: first = (center[0], center[1], distance(center, outside), currentFrame) #points = ax.plot(currentFrame,center[0],'ro')[0] plot = True #draw inputted cricle on frame and then show it cap.set(1, currentFrame) ret, frame = cap.read() #frame = extra.process(frame,height,width,fps,cap) #frame = memory[currentFrame] x, y = center r = distance(center, outside) circleCoords[currentFrame] = (x, y, r) #cv2.setTrackbarPos('Frames','frame',currentFrame) cv2.circle(frame, (x, y), r, (228, 20, 20), 4) cv2.rectangle(frame, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1) lastFrameWithCircle = currentFrame #cv2.imshow('frame', frame) #return to normal state pause = False center = outside = None img = extra.clear(pause) #if first click (center) else: center = (x, y) img = extra.feedback("Please click on the edge of the circle", pause)
background = fig.canvas.copy_from_bbox(ax.bbox) """ """LOOP FOR DISPLAYING VIDEO""" while (True): #advance frame advance() """BUTTON COMMANDS""" #get button press key = cv2.waitKey(1) & 0xFF if key == ord('p'): #pause if pause: pause = False else: pause = True img = extra.feedback("", pause) if key == ord('q'): #quit break if key == ord('w'): #slower if speed > -3: speed -= 1 if key == ord('e'): #faster if speed < 3: speed += 1 """not really used anymore""" #if key == ord('t'):#drawing options # window.show() if key == 3: #right arrow currentFrame += 1 if key == 2: #left arrow currentFrame -= 1