我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.convexHull()。
def drawlines(img, points): filler = cv2.convexHull(points) cv2.polylines(img, filler, True, (0, 0, 0), thickness=2) return img
def extract_corners(self, image): """ Find the 4 corners of a binary image :param image: binary image :return: 4 main vertices or None """ cnts, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:] cnt = cnts[0] _, _, h, w = cv2.boundingRect(cnt) epsilon = min(h, w) * 0.5 vertices = cv2.approxPolyDP(cnt, epsilon, True) vertices = cv2.convexHull(vertices, clockwise=True) vertices = self.correct_vertices(vertices) return vertices
def get_rectangles(contours): rectangles = [] for contour in contours: epsilon = 0.04*cv2.arcLength(contour,True) hull = cv2.convexHull(contour) approx = cv2.approxPolyDP(hull,epsilon,True) if (len(approx) == 4 and cv2.isContourConvex(approx)): rectangles.append(approx) return rectangles
def find_contours(self, img): thresh_img = self.threshold(img) _, contours, _ = cv2.findContours(thresh_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) result = [] for cnt in contours: approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True) if self.draw_approx: cv2.drawContours(self.out, [approx], -1, self.BLUE, 2, lineType=8) if len(approx) > 3 and len(approx) < 15: _, _, w, h = cv2.boundingRect(approx) if h > self.min_height and w > self.min_width: hull = cv2.convexHull(cnt) approx2 = cv2.approxPolyDP(hull,0.01*cv2.arcLength(hull,True),True) if self.draw_approx2: cv2.drawContours(self.out, [approx2], -1, self.GREEN, 2, lineType=8) result.append(approx2) return result
def _get_tip_position(array, contour, verbose = False): approx_contour = cv2.approxPolyDP(contour, 0.08 * cv2.arcLength(contour, True), True) convex_points = cv2.convexHull(approx_contour, returnPoints = True) cx, cy = 999, 999 for point in convex_points: cur_cx, cur_cy = point[0][0], point[0][1] if verbose: cv2.circle(array, (cur_cx, cur_cy), 4, _COLOR_GREEN,4) if (cur_cy < cy): cx, cy = cur_cx, cur_cy (screen_x, screen_y) = pyautogui.size() height, width, _ = array.shape x = _round_int((float(cx))/(width-0)*(screen_x+1)) y = _round_int((float(cy))/(height-0)*(screen_y+1)) return (array, (x, y))
def get_corners_from_contours(contours, corner_amount=4): """ Finds four corners from a list of points on the goal epsilon - the minimum side length of the polygon generated by the corners Parameters: :param: `contours` - a numpy array of points (opencv contour) of the points to get corners from :param: `corner_amount` - the number of corners to find """ coefficient = .05 while True: # print(contours) epsilon = coefficient * cv2.arcLength(contours, True) # epsilon = # print("epsilon:", epsilon) poly_approx = cv2.approxPolyDP(contours, epsilon, True) hull = cv2.convexHull(poly_approx) if len(hull) == corner_amount: return hull else: if len(hull) > corner_amount: coefficient += .01 else: coefficient -= .01
def extract_corners(self, image): """ Find the 4 corners of a binary image :param image: binary image :return: 4 main vertices or None """ cnts, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:] cnt = cnts[0] _, _, h, w = cv2.boundingRect(cnt) epsilon = min(h, w) * 0.5 o_vertices = cv2.approxPolyDP(cnt, epsilon, True) vertices = cv2.convexHull(o_vertices, clockwise=True) vertices = self.correct_vertices(vertices) if self.debug: temp = cv2.cvtColor(image.copy(), cv2.COLOR_GRAY2BGR) cv2.drawContours(temp, cnts, -1, (0, 255, 0), 10) cv2.drawContours(temp, o_vertices, -1, (255, 0, 0), 30) cv2.drawContours(temp, vertices, -1, (0, 0, 255), 20) self.save2image(temp) return vertices
def visualize(frame, coordinates_list, alpha = 0.80, color=[255, 255, 255]): """ Args: 1. frame: OpenCV's image which has to be visualized. 2. coordinates_list: List of coordinates which will be visualized in the given `frame` 3. alpha, color: Some parameters which help in visualizing properly. A convex hull will be shown for each element in the `coordinates_list` """ layer = frame.copy() output = frame.copy() for coordinates in coordinates_list: c_hull = cv2.convexHull(coordinates) cv2.drawContours(layer, [c_hull], -1, color, -1) cv2.addWeighted(layer, alpha, output, 1 - alpha, 0, output) cv2.imshow("Output", output)
def get_corners(contour): """ Given a contour that should have a rectangular convex hull, produce a sorted list of corners for the bounding rectangle :param contour: :return: """ hull = cv2.convexHull(contour) hull_poly = cv2.approxPolyDP(hull, 0.05 * cv2.arcLength(hull, True), True) return sort_corners(hull_poly)
def fill(img, points): filler = cv2.convexHull(points) cv2.fillConvexPoly(img, filler, 255) return img
def blendImages(src, dst, mask, featherAmount=0.2): #indeksy nie czarnych pikseli maski maskIndices = np.where(mask != 0) #te same indeksy tylko, ze teraz w jednej macierzy, gdzie kazdy wiersz to jeden piksel (x, y) maskPts = np.hstack((maskIndices[1][:, np.newaxis], maskIndices[0][:, np.newaxis])) faceSize = np.max(maskPts, axis=0) - np.min(maskPts, axis=0) featherAmount = featherAmount * np.max(faceSize) hull = cv2.convexHull(maskPts) dists = np.zeros(maskPts.shape[0]) for i in range(maskPts.shape[0]): dists[i] = cv2.pointPolygonTest(hull, (maskPts[i, 0], maskPts[i, 1]), True) weights = np.clip(dists / featherAmount, 0, 1) composedImg = np.copy(dst) composedImg[maskIndices[0], maskIndices[1]] = weights[:, np.newaxis] * src[maskIndices[0], maskIndices[1]] + (1 - weights[:, np.newaxis]) * dst[maskIndices[0], maskIndices[1]] return composedImg #uwaga, tutaj src to obraz, z ktorego brany bedzie kolor
def camera_gesture_trigger(): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray,(5,5),0) ret,thresh1 = cv2.threshold(blur,70,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) max_area=0 for i in range(len(contours)): cnt=contours[i] area = cv2.contourArea(cnt) if(area>max_area): max_area=area ci=i cnt=contours[ci] hull = cv2.convexHull(cnt) moments = cv2.moments(cnt) cnt = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True) hull = cv2.convexHull(cnt,returnPoints = False) defects = cv2.convexityDefects(cnt,hull) if defects is not None: if defects.shape[0] >= 5: return 1 return 0
def warpFace3D(im, oldMesh, pose, newMesh, accurate=False, fitter=None): oldVerts2d = projectMeshTo2D(oldMesh, pose, im) newVerts2d = projectMeshTo2D(newMesh, pose, im) if not accurate and fitter is not None: ALL_FACE_MESH_VERTS = fitter.landmarks_2_vert_indices[ALL_FACE_LANDMARKS] ALL_FACE_MESH_VERTS = np.delete(ALL_FACE_MESH_VERTS, np.where(ALL_FACE_MESH_VERTS == -1)).tolist() oldConvexHullIndexs = cv2.convexHull(oldVerts2d.astype(np.float32), returnPoints=False) warpPointIndexs = oldConvexHullIndexs.flatten().tolist() + ALL_FACE_MESH_VERTS oldVerts2d = oldVerts2d[warpPointIndexs] newVerts2d = newVerts2d[warpPointIndexs] warpedIm = warpFace(im, oldVerts2d, newVerts2d) return warpedIm
def decomposePose(mesh, pose, im): modelview = np.matrix(pose.get_modelview()) proj = np.matrix(pose.get_projection()) viewport = np.array([0, im.shape[0], im.shape[1], -im.shape[0]]) modelview = modelview.tolist() projection = proj.tolist() viewport = viewport.tolist() ALL_FACE_MESH_VERTS = BFM_FACEFITTING.landmarks_2_vert_indices[ALL_FACE_LANDMARKS] ALL_FACE_MESH_VERTS = np.delete(ALL_FACE_MESH_VERTS, np.where(ALL_FACE_MESH_VERTS == -1)).tolist() verts2d = projectMeshTo2D(mesh, pose, im) convexHullIndexs = cv2.convexHull(verts2d.astype(np.float32), returnPoints=False) warpPointIndexs = convexHullIndexs.flatten().tolist() + ALL_FACE_MESH_VERTS indexs = warpPointIndexs return modelview, projection, viewport, indexs
def drawConvexHull(img, contours): cnt = contours[0] mask = np.zeros(img.shape, np.uint8) hull = cv2.convexHull(cnt,returnPoints = False) defects = cv2.convexityDefects(cnt,hull) for i in range(defects.shape[0]): s,e,f,d = defects[i,0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) cv2.line(mask,start,end,[255,255,255],5) cv2.circle(mask,far,5,[255,255,255],-1) (x,y),radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) cv2.circle(mask,center,radius,(255,255,255),-1) return mask
def calculateFingers(res,drawing): # -> finished bool, cnt: finger count # convexity defect hull = cv2.convexHull(res, returnPoints=False) if len(hull) > 3: defects = cv2.convexityDefects(res, hull) if type(defects) != type(None): # avoid crashing. (BUG not found) cnt = 0 for i in range(defects.shape[0]): # calculate the angle s, e, f, d = defects[i][0] start = tuple(res[s][0]) end = tuple(res[e][0]) far = tuple(res[f][0]) a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2) c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2) angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) # cosine theorem if angle <= math.pi / 2: # angle less than 90 degree, treat as fingers cnt += 1 cv2.circle(drawing, far, 8, [211, 84, 0], -1) return True, cnt return False, 0 # Camera
def get_contours(image, polydb=0.03, contour_range=5, show=False): # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour # if cv2version == 3: im2, contours, hierarchy = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) contours = _findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range] # loop over the contours screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) #finds the Contour Perimeter approx = cv2.approxPolyDP(c, polydb * peri, True) # if our approximated contour has four points, then we can assume that we have found our screen if len(approx) == 4: screenCnt = approx break if screenCnt is None: raise EdgeNotFound() # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form if not cv2.isContourConvex(screenCnt): screenCnt = cv2.convexHull(screenCnt) x,y,w,h = cv2.boundingRect(screenCnt) screenCnt = np.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]]) if show: #this is for debugging puposes new_image = image.copy() cv2.drawContours(new_image, [screenCnt], -1, (255, 255, 0), 2) cv2.imshow("Contour1 image", new_image) cv2.waitKey(0) cv2.destroyAllWindows() return screenCnt
def get_contours(image, polydb=0.03, contour_range=5, show=False): # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour if cv2version == 3: im2, contours, hierarchy = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range] # loop over the contours screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) #finds the Contour Perimeter approx = cv2.approxPolyDP(c, polydb * peri, True) # if our approximated contour has four points, then we can assume that we have found our screen if len(approx) == 4: screenCnt = approx break if screenCnt is None: raise EdgeNotFound() # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form if not cv2.isContourConvex(screenCnt): screenCnt = cv2.convexHull(screenCnt) x,y,w,h = cv2.boundingRect(screenCnt) screenCnt = np.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]]) if show: #this is for debugging puposes new_image = image.copy() cv2.drawContours(new_image, [screenCnt], -1, (255, 255, 0), 2) cv2.imshow("Contour1 image", new_image) cv2.waitKey(0) cv2.destroyAllWindows() return screenCnt
def get_contours(image, polydb=0.1, contour_range=7, show=False): # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour contours = _findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range] # loop over the contours screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) #finds the Contour Perimeter approx = cv2.approxPolyDP(c, polydb * peri, True) # if our approximated contour has four points, then we can assume that we have found our screen if len(approx) == 4: screenCnt = approx break if screenCnt is None: raise EdgeNotFound() # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form if not cv2.isContourConvex(screenCnt): screenCnt = cv2.convexHull(screenCnt) x,y,w,h = cv2.boundingRect(screenCnt) screenCnt = numpy.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]]) if show: #this is for debugging puposes new_image = image.copy() cv2.drawContours(new_image, [screenCnt], -1, (255, 255, 0), 2) cv2.imshow("Contour1 image", new_image) cv2.waitKey(0) cv2.destroyAllWindows() return screenCnt
def get_contours(image, polydb=0.03, contour_range=7, show=False): # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour # if cv2version == 3: im2, contours, hierarchy = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) contours = _findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range] # loop over the contours screenCnt = None for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) #finds the Contour Perimeter approx = cv2.approxPolyDP(c, polydb * peri, True) # if our approximated contour has four points, then we can assume that we have found our screen if len(approx) == 4: screenCnt = approx break if screenCnt is None: raise EdgeNotFound() # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form if not cv2.isContourConvex(screenCnt): screenCnt = cv2.convexHull(screenCnt) x,y,w,h = cv2.boundingRect(screenCnt) screenCnt = numpy.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]]) if show: #this is for debugging puposes new_image = image.copy() cv2.drawContours(new_image, [screenCnt], -1, (255, 255, 0), 2) cv2.imshow("Contour1 image", new_image) cv2.waitKey(0) cv2.destroyAllWindows() return screenCnt
def solidity(contour, hull=None, area_min=0): """ Calculate the solidity of a contour, which is the ratio of its area to the area of its convex hull. :param contour: the target contour :param hull: the convex hull of the contour, see cv2.convexHull(contour) :param area_min: a contour with an area below the minimum has a solidity of 0 """ if hull is None: hull = cv2.convexHull(contour) try: contour_area = cv2.contourArea(contour) hull_area = cv2.contourArea(hull) return contour_area / hull_area if hull_area > area_min else 0 except ArithmeticError: return 0
def partial_blur(img, points, kenel_size = 9, type = 1): """ Partial Gaussian blur within convex hull of points. Args: type = 0 for Gaussian blur type = 1 for average blur """ points = cv2.convexHull(points) copy_img = img.copy() black = (0, 0, 0) if type: cv2.blur(img, (kenel_size, kenel_size)) else: cv2.GaussianBlur(img, (kenel_size, kenel_size), 0) cv2.fillConvexPoly(copy_img, points, color = black) for row in range(img.shape[:2][0]): for col in range(img.shape[:2][1]): if numpy.array_equal(copy_img[row][col], black): copy_img[row][col] = blur_img[row][col] return copy_img
def get_face_mask(img, img_l): img = np.zeros(img.shape[:2], dtype = np.float64) for idx in OVERLAY_POINTS_IDX: cv2.fillConvexPoly(img, cv2.convexHull(img_l[idx]), color = 1) img = np.array([img, img, img]).transpose((1, 2, 0)) img = (cv2.GaussianBlur(img, (BLUR_AMOUNT, BLUR_AMOUNT), 0) > 0) * 1.0 img = cv2.GaussianBlur(img, (BLUR_AMOUNT, BLUR_AMOUNT), 0) return img
def get_contour_mask(dshape, img_fl): mask = np.zeros(dshape) hull = cv2.convexHull(img_fl) cv2.drawContours(mask, [hull], 0, (1, 1, 1) , -1) return np.uint8(mask) # Orients input_ mask onto tmpl_ face
def convex_hull(pts, ccw=True): """ Returns the convex hull of points, ordering them in ccw/cw fashion Note: Since the orientation of the coordinate system is x-right, y-up, ccw is interpreted as cw in the function call. """ assert(pts.ndim == 2 and pts.shape[1] == 2) return (cv2.convexHull(pts.reshape(-1,1,2), clockwise=ccw)).reshape(-1,2) # =========================================================================== # BBOX-functions
def convex_hulls(contours): """ Convenience method to get a list of convex hulls from list of contours :param contours: contours that should be turned into convex hulls :return: a list of convex hulls that match each contour """ hulls = [] for contour in contours: hulls.append(cv2.convexHull(contour)) return hulls
def draw_convex_hull(self,im, points, color): ''' ?????? ''' points = cv2.convexHull(points) cv2.fillConvexPoly(im, points, color=color)
def __init__(self,g_pool): super().__init__(g_pool) self.menu=None logger.error("This will be implemented as part of gaze mapper soon.") self.alive= False return width,height = self.g_pool.capture.frame_size if g_pool.app == 'capture': cal_pt_path = os.path.join(g_pool.user_dir,"user_calibration_data") else: cal_pt_path = os.path.join(g_pool.rec_dir,"user_calibration_data") try: user_calibration_data = load_object(cal_pt_path) except: logger.warning("Please calibrate first") self.close() return if self.g_pool.binocular: fn_input_eye1 = cal_pt_cloud[:,2:4].transpose() cal_pt_cloud[:,0:2] = np.array(map_fn(fn_input_eye0, fn_input_eye1)).transpose() cal_pt_cloud[:,2:4] = cal_pt_cloud[:,4:6] else: fn_input = cal_pt_cloud[:,0:2].transpose() cal_pt_cloud[:,0:2] = np.array(map_fn(fn_input)).transpose() ref_pts = cal_pt_cloud[inlier_map][:,np.newaxis,2:4] ref_pts = np.array(ref_pts,dtype=np.float32) logger.debug("calibration ref_pts %s"%ref_pts) if len(ref_pts)== 0: logger.warning("Calibration is bad. Please re-calibrate") self.close() return self.calib_bounds = cv2.convexHull(ref_pts) # create a list [[px1,py1],[wx1,wy1],[px2,py2],[wx2,wy2]...] of outliers and inliers for gl_lines self.outliers = np.concatenate((cal_pt_cloud[~inlier_map][:,0:2],cal_pt_cloud[~inlier_map][:,2:4])).reshape(-1,2) self.inliers = np.concatenate((cal_pt_cloud[inlier_map][:,0:2],cal_pt_cloud[inlier_map][:,2:4]),axis=1).reshape(-1,2) self.inlier_ratio = cal_pt_cloud[inlier_map].shape[0]/float(cal_pt_cloud.shape[0]) self.inlier_count = cal_pt_cloud[inlier_map].shape[0] # hull = cv2.approxPolyDP(self.calib_bounds, 0.001,closed=True) full_screen_area = 1. logger.debug("calibration bounds %s"%self.calib_bounds) self.calib_area_ratio = cv2.contourArea(self.calib_bounds)/full_screen_area
def gl_display(self): for grid_points in self.img_points: calib_bounds = cv2.convexHull(grid_points)[:,0] #we dont need that extra encapsulation that opencv likes so much draw_polyline(calib_bounds,1,RGBA(0.,0.,1.,.5),line_type=gl.GL_LINE_LOOP) if self._window: self.gl_display_in_window() if self.show_undistortion: gl.glPushMatrix() make_coord_system_norm_based() draw_gl_texture(self.undist_img) gl.glPopMatrix()
def build_correspondance(self, visible_markers,camera_calibration,min_marker_perimeter,min_id_confidence): """ - use all visible markers - fit a convex quadrangle around it - use quadrangle verts to establish perpective transform - map all markers into surface space - build up list of found markers and their uv coords """ all_verts = [m['verts'] for m in visible_markers if m['perimeter']>=min_marker_perimeter] if not all_verts: return all_verts = np.array(all_verts,dtype=np.float32) all_verts.shape = (-1,1,2) # [vert,vert,vert,vert,vert...] with vert = [[r,c]] # all_verts_undistorted_normalized centered in img center flipped in y and range [-1,1] all_verts_undistorted_normalized = cv2.undistortPoints(all_verts, camera_calibration['camera_matrix'],camera_calibration['dist_coefs']*self.use_distortion) hull = cv2.convexHull(all_verts_undistorted_normalized,clockwise=False) #simplify until we have excatly 4 verts if hull.shape[0]>4: new_hull = cv2.approxPolyDP(hull,epsilon=1,closed=True) if new_hull.shape[0]>=4: hull = new_hull if hull.shape[0]>4: curvature = abs(GetAnglesPolyline(hull,closed=True)) most_acute_4_threshold = sorted(curvature)[3] hull = hull[curvature<=most_acute_4_threshold] # all_verts_undistorted_normalized space is flipped in y. # we need to change the order of the hull vertecies hull = hull[[1,0,3,2],:,:] # now we need to roll the hull verts until we have the right orientation: # all_verts_undistorted_normalized space has its origin at the image center. # adding 1 to the coordinates puts the origin at the top left. distance_to_top_left = np.sqrt((hull[:,:,0]+1)**2+(hull[:,:,1]+1)**2) bot_left_idx = np.argmin(distance_to_top_left)+1 hull = np.roll(hull,-bot_left_idx,axis=0) #based on these 4 verts we calculate the transformations into a 0,0 1,1 square space m_from_undistored_norm_space = m_verts_from_screen(hull) self.detected = True # map the markers vertices into the surface space (one can think of these as texture coordinates u,v) marker_uv_coords = cv2.perspectiveTransform(all_verts_undistorted_normalized,m_from_undistored_norm_space) marker_uv_coords.shape = (-1,4,1,2) #[marker,marker...] marker = [ [[r,c]],[[r,c]] ] # build up a dict of discovered markers. Each with a history of uv coordinates for m,uv in zip (visible_markers,marker_uv_coords): try: self.markers[m['id']].add_uv_coords(uv) except KeyError: self.markers[m['id']] = Support_Marker(m['id']) self.markers[m['id']].add_uv_coords(uv) #average collection of uv correspondences accros detected markers self.build_up_status = sum([len(m.collected_uv_coords) for m in self.markers.values()])/float(len(self.markers)) if self.build_up_status >= self.required_build_up: self.finalize_correnspondance()
def get_defects(self, cnt, drawing): defects = None hull = cv2.convexHull(cnt) cv2.drawContours(drawing, [cnt], 0, (0, 255, 0), 0) cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 0) hull = cv2.convexHull(cnt, returnPoints=False) # For finding defects if hull.size > 2: defects = cv2.convexityDefects(cnt, hull) return defects # Gesture Recognition
def mask_from_points(size, points): mask = np.zeros(size, np.uint8) cv2.fillConvexPoly(mask, cv2.convexHull(points), 255) return mask
def do_touch(self): width, height = 1080, 1920 screen = self.device.screenshot_cv2() h, w = screen.shape[:2] img = cv2.resize(screen, (w/2, h/2)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 80, 200) _, thresh = cv2.threshold(edges, 0, 255, cv2.THRESH_OTSU) contours, _ = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) contours.sort(key=lambda cnt: len(cnt), reverse=True) rects = [] for cnt in contours: hull = cv2.convexHull(cnt) hull_area = cv2.contourArea(hull) x,y,w,h = cv2.boundingRect(cnt) rect_area = float(w*h) if w<20 or h<20 or rect_area<100: continue if hull_area/rect_area < 0.50: continue rects.append((x, y, x+w, y+h)) cv2.rectangle(img, (x, y), (x+w, y+h), 255, 2) if not rects: x, y = randint(1, width), randint(1, height) else: x1, y1, x2, y2 = choice(rects) x, y = randint(x1, x2), randint(y1, y2) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) x, y = self.device.screen2touch(x*2, y*2) self.device.touch(x, y) cv2.imshow('img', img) cv2.waitKey(1)
def draw_convex_hull(im, points, color): points = cv2.convexHull(points) cv2.fillConvexPoly(im, points, color=color)
def get_convex_hull(contour): return cv2.convexHull(contour)
def _find_hull_defects(self, segment): # Use cv2 findContours function to find all the contours in segmented img contours, hierarchy = cv2.findContours(segment, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # assume largest contour is the one of interest max_contour = max(contours, key=cv2.contourArea) epsilon = 0.01*cv2.arcLength(max_contour, True) max_contour = cv2.approxPolyDP(max_contour, epsilon, True) # determine convex hull & convexity defects of the hull hull = cv2.convexHull(max_contour, returnPoints=False) defects = cv2.convexityDefects(max_contour, hull) return (max_contour, defects)
def get_landmarks(self, img): img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) rects = self.detector(img, 0) if len(rects) > 1: print 'TooManyFaces' if len(rects) == 0: raise ValueError('Error: NoFaces!!') landmarks = np.matrix([[p.x, p.y] for p in self.predictor(img, rects[0]).parts()]) for group in self.MOUTH_POINTS: hull = cv2.convexHull(landmarks[group]) return hull
def get_face_mask(self,img,landmarks): for group in self.OVERLAY_POINTS: hull = cv2.convexHull(landmarks[group]) cv2.fillConvexPoly(img, hull, 0)
def __filter_contours(input_contours, min_area, min_perimeter, min_width, max_width, min_height, max_height, solidity, max_vertex_count, min_vertex_count, min_ratio, max_ratio): """Filters out contours that do not meet certain criteria. Args: input_contours: Contours as a list of numpy.ndarray. min_area: The minimum area of a contour that will be kept. min_perimeter: The minimum perimeter of a contour that will be kept. min_width: Minimum width of a contour. max_width: MaxWidth maximum width. min_height: Minimum height. max_height: Maximimum height. solidity: The minimum and maximum solidity of a contour. min_vertex_count: Minimum vertex Count of the contours. max_vertex_count: Maximum vertex Count. min_ratio: Minimum ratio of width to height. max_ratio: Maximum ratio of width to height. Returns: Contours as a list of numpy.ndarray. """ output = [] for contour in input_contours: x,y,w,h = cv2.boundingRect(contour) if (w < min_width or w > max_width): continue if (h < min_height or h > max_height): continue area = cv2.contourArea(contour) if (area < min_area): continue if (cv2.arcLength(contour, True) < min_perimeter): continue hull = cv2.convexHull(contour) solid = 100 * area / cv2.contourArea(hull) if (solid < solidity[0] or solid > solidity[1]): continue if (len(contour) < min_vertex_count or len(contour) > max_vertex_count): continue ratio = (float)(w) / h if (ratio < min_ratio or ratio > max_ratio): continue output.append(contour) return output
def draw_convex_hull(im, points, color): points = cv2.convexHull(points) cv2.fillConvexPoly(im, points, color=color) print(points)
def get_patches(segment_arr): ret = [] im = segment_arr.astype(np.uint8) contours = cv2.findContours(im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) hulls = [cv2.convexHull(cont) for cont in contours[1]] #seems my version of CV2 (3.0) uses [1] for contour_idx in xrange(len(hulls)): cimg = np.zeros_like(im) cv2.drawContours(cimg, hulls, contour_idx, color=255, thickness=-1) pts = np.array(np.where(cimg == 255)).T ret.append(pts) return ret
def get_patches(segment_arr): ret = [] im = segment_arr.astype(np.uint8)*255 contours = cv2.findContours(im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) hulls = [cv2.convexHull(cont) for cont in contours[0]] for contour_idx in xrange(len(hulls)): cimg = np.zeros_like(im) cv2.drawContours(cimg, hulls, contour_idx, color=255, thickness=-1) pts = np.array(np.where(cimg == 255)).T ret.append(pts) return ret
def contourImg(image): # Find contours in the image _, contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) image.fill(0) # Filter out some contours i = 0 found = False cnt = [] boundingRects = [] while i < len(contours): if cv2.contourArea(contours[i]) > 200: approx = cv2.convexHull(contours[i]) x,y,w,h = cv2.boundingRect(contours[i]) aspect = w/h coverage = w*h/cv2.contourArea(contours[i]) if(abs(aspect - .4) < .1 and coverage > 0.85): boundingRects.append([x,y,w,h]) i += 1 i = 0 while i < len(boundingRects): j = i+1 while j < len(boundingRects): if(abs(boundingRects[i][1] - boundingRects[j][1]) < 20 and abs(((boundingRects[i][0] - boundingRects[j][0]) / boundingRects[i][1]) - 1.65 < .1)): return (createRectCnt(boundingRects[i]), createRectCnt(boundingRects[j])) j+=1 i+=1 return -1 # Define color range