import cv2 import numpy as np import math import test def correct_circle(img, circles): for i in reversed(range(len(circles))): h, w = img.shape[:2] if int(circles[i][0]) > h - 1 or int(circles[i][1]) > w - 1 or img[circles[i][1], circles[i][0]] != 0: circles[i][2] = 0 return circles def correct_contours(contours,center): tmp =[] for i in reversed(range(len(contours))): x,y,w,h = cv2.boundingRect(contours[i]) if cv2.pointPolygonTest(contours[i],(center[0],center[1]),False) > 0 and w * h < center[2] ** 2 * 4: tmp.append(i) return tmp cap = cv2.VideoCapture(0) tmp = None past = None while tmp is None: _, tmp = cap.read() canvas = np.zeros(tmp.shape, dtype=np.uint8) avg_hsv, avg_bgr = test.norm(cap) hue_thresh = 10 lower_hsv = (avg_hsv[0] - hue_thresh, 0, 0) upper_hsv = (avg_hsv[0] + hue_thresh, 255, 255) while( cap ) : ret, img = cap.read() real = np.copy(img) # img = cv2.GaussianBlur(img,(3,3),0) if img is None: continue img = test.pre_process(img, lower_hsv, upper_hsv) # gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # blur = cv2.GaussianBlur(gray,(5,5),0) # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # # circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 1, 60, # param1=30, # param2=30, # minRadius=0, # maxRadius=100) # canny = cv2.Canny(gray, 50, 100, 5) # contours = cv2.findContours(canny, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[0] # _, black = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY) # if circles is not None: # circles = circles[0] # circles = correct_circle(gray, circles) # circles = np.uint16(np.around(circles)) # # for i in range(len(circles)): # if circles[i][2] != 0: # cc = correct_contours(contours,circles[i]) # if cc != []: # cv2.drawContours(img,contours,cc[len(cc) - 1],(0,255,0),1) # # cv2.circle(img,(circles[i][0],circles[i][1]),2,(255,0,0),1) # #sub = cv2.subtract(img, past) # #cv2.imshow("sub", sub) # #x,y,w,h = cv2.boundingRect(contours[cc[len(cc) - 1]]) # rect = cv2.minAreaRect(contours[cc[len(cc) - 1]]) # box = cv2.cv.BoxPoints(rect) # box = np.int0(box) # cv2.drawContours(img,[box],0,(0,0,255),1) # x,y = test.med(box,black) # cv2.circle(canvas,(int(x),int(y)),2,(0,255,255),5) # # # # # # # cv2.add(real,canvas, real) # cv2.imshow('paint', real) cv2.imshow('11', img) k = cv2.waitKey(10) if k == 27: break elif k == 13: # cv2.imwrite("img.png",mask) pass