def remove_background(image): """ Removes background from image """ # Paramters. BLUR = 21 CANNY_THRESH_1 = 10 CANNY_THRESH_2 = 30 MASK_DILATE_ITER = 10 MASK_ERODE_ITER = 10 MASK_COLOR = (0.0, 0.0, 1.0) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Edge detection. edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2) edges = cv2.dilate(edges, None) edges = cv2.erode(edges, None) # Find contours in edges, sort by area contour_info = [] contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) for c in contours: contour_info.append(( c, cv2.isContourConvex(c), cv2.contourArea(c), )) contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True) max_contour = contour_info[0] # Create empty mask. mask = np.zeros(edges.shape) cv2.fillConvexPoly(mask, max_contour[0], (255)) # Smooth mask and blur it. mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER) mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER) mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0) mask_stack = np.dstack([mask] * 3) # Blend masked img into MASK_COLOR background mask_stack = mask_stack.astype('float32') / 255.0 image = image.astype('float32') / 255.0 masked = (mask_stack * image) + ((1 - mask_stack) * MASK_COLOR) masked = (masked * 255).astype('uint8') c_red, c_green, c_blue = cv2.split(image) img_a = cv2.merge((c_red, c_green, c_blue, mask.astype('float32') / 255.0)) return img_a * 255
def generate_jointsmap(uv_coord, depth, width, height, channel=3): canvas = np.ones((height, width, channel)) * sys.maxsize _canvas = canvas.copy() bones = [ ((0, 17), [160] * channel), ((0, 1), [170] * channel), ((0, 5), [180] * channel), ((0, 9), [190] * channel), ((0, 13), [200] * channel), ((17, 18), [130] * channel), ((18, 19), [140] * channel), ((19, 20), [150] * channel), ((1, 2), [10] * channel), ((2, 3), [20] * channel), ((3, 4), [30] * channel), ((5, 6), [40] * channel), ((6, 7), [50] * channel), ((7, 8), [60] * channel), ((9, 10), [70] * channel), ((10, 11), [80] * channel), ((11, 12), [90] * channel), ((13, 14), [100] * channel), ((14, 15), [110] * channel), ((15, 16), [120] * channel), ] for connection, color in bones: temp_canvas = np.ones(canvas.shape) * sys.maxsize coord1 = uv_coord[connection[0]] coord2 = uv_coord[connection[1]] coords = np.stack([coord1, coord2]) avg_depth = (depth[connection[0]] + depth[connection[1]]) / 2 x = coords[:, 0] y = coords[:, 1] mX = x.mean() mY = y.mean() length = ((x[0] - x[1])**2 + (y[0] - y[1])**2)**0.5 angle = np.math.degrees(np.math.atan2(y[0] - y[1], x[0] - x[1])) radius = 5 polygon = cv2.ellipse2Poly( (int(mX), int(mY)), (int(length / 2), radius), int(angle), 0, 360, 1) cv2.fillConvexPoly(temp_canvas, polygon, [avg_depth] * channel) _canvas = np.minimum(_canvas, temp_canvas) canvas[_canvas == avg_depth] = color[0] canvas[canvas == sys.maxsize] = 0 return canvas
def generate_image_cv2(bac, sim_univs, base_univ, nuniv, univ_width, univ_height): univ = sim_univs[bac.univ - base_univ] mn = min(univ_width, univ_height) length = bac.length * mn / 2 width = bac.width * mn / 2 bac_x = (bac.x * mn + univ_width) / 2 bac_y = (bac.y * mn + univ_height) / 2 x = bac_x - length * cos(bac.orient) y = bac_y + length * sin(bac.orient) head_pos = np.array([x, y, 0]) end_point_1 = np.array([ x + width * cos(bac.orient - pi / 2), y - width * sin(bac.orient - pi / 2), 0 ]) end_point_2 = np.array([ x - width * cos(bac.orient - pi / 2), y + width * sin(bac.orient - pi / 2), 0 ]) x = bac_x + length * cos(bac.orient) y = bac_y - length * sin(bac.orient) tail_pos = np.array([x, y, 0]) end_point_3 = np.array([ x - width * cos(bac.orient - pi / 2), y + width * sin(bac.orient - pi / 2), 0 ]) end_point_4 = np.array([ x + width * cos(bac.orient - pi / 2), y - width * sin(bac.orient - pi / 2), 0 ]) # head and tail cv2.circle(univ, tuple(head_pos[:2].astype(int)), int(width), 1, -1) cv2.circle(univ, tuple(tail_pos[:2].astype(int)), int(width), 1, -1) # body points = [ tuple(end_point_1[:2]), tuple(end_point_2[:2]), tuple(end_point_3[:2]), tuple(end_point_4[:2]) ] points = np.array([(int(point[0]), int(point[1])) for point in points]) cv2.fillConvexPoly(univ, points, 1, 1)
def render(img, obj, projection, model): vertices = obj.vertices scale_matrix = np.eye(3) * 3 h, w = model.shape for face in obj.faces: face_vertices = face[0] points = np.array([vertices[vertex - 1] for vertex in face_vertices]) points = np.dot(points, scale_matrix) # render model in the middle of the reference surface. To do so, # model points must be displaced points = np.array([[p[0] + w / 2, p[1] + h / 2, p[2]] for p in points]) dst = cv2.perspectiveTransform(points.reshape(-1, 1, 3), projection) imgpts = np.int32(dst) cv2.fillConvexPoly(img, imgpts, (137, 27, 211)) return img
def get_target(self, gt_polys, gt_labels, mask_shape, scale, device): img_masks = [] for i, polys in enumerate(gt_polys): mask = np.zeros(shape=mask_shape, dtype=np.int32) polys = np.array([poly[0] for poly in polys], dtype=np.float) polys *= scale labels = gt_labels[i].cpu().numpy() for j, poly in enumerate(polys): new_box = np.int0(poly).reshape([4, 2]) color = int(labels[j]) cv.fillConvexPoly(mask, new_box, color=color) img_masks.append(mask) # mask[mask > 0 ] = 255 # cv.imwrite('/code/AerialDetection/work_dirs/attention_vis/mask_{}.jpg'.format(i), mask) # mask[mask > 0 ] = 1 img_masks = np.stack(img_masks) if self.binary_mask: img_masks[img_masks > 0] = 1 mask_targets = torch.from_numpy(img_masks).float().to(device) return mask_targets
def render(img, obj, projection, model, color=False): vertices = obj.vertices scale_matrix = np.eye(3) * 0.5 h, w, c = model.shape for face in obj.faces: face_vertices = face[0] points = np.array([vertices[vertex - 1] for vertex in face_vertices]) points = np.dot(points, scale_matrix) # render model in the middle of the reference surface. To do so, # model points must be displaced points = np.array([[p[0] + w / 2, p[1] + h / 2, p[2]] for p in points]) dst = cv2.perspectiveTransform(points.reshape(-1, 1, 3), projection) imgpts = np.int32(dst) #if color is False: cv2.fillConvexPoly(img, imgpts, (35, 240, 90)) #else: # color = hex_to_rgb(face[-1]) # color = color[::-1] # reverse # cv2.fillConvexPoly(img, imgpts, color) return img
# read video frame & show on screen ret, frame = cap.read() # cv.imshow("Original Scene", frame) # snip section of video frame of interest & show on screen if frame is None: cv.waitKey(0) break snip = frame[500:700, 300:900] cv.imshow("Snip", snip) # create polygon (trapezoid) mask to select region of interest mask = np.zeros((snip.shape[0], snip.shape[1]), dtype="uint8") pts = np.array([[25, 190], [275, 50], [380, 50], [575, 190]], dtype=np.int32) cv.fillConvexPoly(mask, pts, 255) cv.imshow("Mask", mask) # apply mask and show masked image on screen masked = cv.bitwise_and(snip, snip, mask=mask) cv.imshow("Region of Interest", masked) # convert to grayscale then black/white to binary image frame = cv.cvtColor(masked, cv.COLOR_BGR2GRAY) thresh = 200 frame = cv.threshold(frame, thresh, 255, cv.THRESH_BINARY)[1] cv.imshow("Black/White", frame) # blur image to help with edge detection blurred = cv.GaussianBlur(frame, (11, 11), 0) # cv.imshow("Blurred", blurred)
def vis_frame(frame, im_res, format='coco'): ''' frame: frame image im_res: im_res of predictions format: coco or mpii return rendered image ''' if format == 'coco': l_pair = [ (0, 1), (0, 2), (1, 3), (2, 4), # Head (5, 6), (5, 7), (7, 9), (6, 8), (8, 10), (17, 11), (17, 12), # Body (11, 13), (12, 14), (13, 15), (14, 16) ] p_color = [ (0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), # Nose, LEye, REye, LEar, REar (77, 255, 255), (77, 255, 204), (77, 204, 255), (191, 255, 77), (77, 191, 255), (191, 255, 77), # LShoulder, RShoulder, LElbow, RElbow, LWrist, RWrist (204, 77, 255), (77, 255, 204), (191, 77, 255), (77, 255, 191), (127, 77, 255), (77, 255, 127), (0, 255, 255) ] # LHip, RHip, LKnee, Rknee, LAnkle, RAnkle, Neck line_color = [(0, 215, 255), (0, 255, 204), (0, 134, 255), (0, 255, 50), (77, 255, 222), (77, 196, 255), (77, 135, 255), (191, 255, 77), (77, 255, 77), (77, 222, 255), (255, 156, 127), (0, 127, 255), (255, 127, 77), (0, 77, 255), (255, 77, 36)] elif format == 'mpii': l_pair = [(8, 9), (11, 12), (11, 10), (2, 1), (1, 0), (13, 14), (14, 15), (3, 4), (4, 5), (8, 7), (7, 6), (6, 2), (6, 3), (8, 12), (8, 13)] p_color = [ PURPLE, BLUE, BLUE, RED, RED, BLUE, BLUE, RED, RED, PURPLE, PURPLE, PURPLE, RED, RED, BLUE, BLUE ] line_color = [ PURPLE, BLUE, BLUE, RED, RED, BLUE, BLUE, RED, RED, PURPLE, PURPLE, RED, RED, BLUE, BLUE ] else: raise NotImplementedError img = frame height, width = img.shape[:2] img = cv.resize(img, (int(width / 2), int(height / 2))) for human in im_res['result']: part_line = {} kp_preds = human['keypoints'] kp_scores = human['kp_score'] kp_preds = torch.cat( (kp_preds, torch.unsqueeze((kp_preds[5, :] + kp_preds[6, :]) / 2, 0))) kp_scores = torch.cat( (kp_scores, torch.unsqueeze((kp_scores[5, :] + kp_scores[6, :]) / 2, 0))) # Draw keypoints for n in range(kp_scores.shape[0]): if kp_scores[n] <= 0.05: continue cor_x, cor_y = int(kp_preds[n, 0]), int(kp_preds[n, 1]) part_line[n] = (int(cor_x / 2), int(cor_y / 2)) bg = img.copy() cv.circle(bg, (int(cor_x / 2), int(cor_y / 2)), 2, p_color[n], -1) # Now create a mask of logo and create its inverse mask also transparency = max(0, min(1, kp_scores[n])) img = cv.addWeighted(bg, transparency, img, 1 - transparency, 0) # Draw limbs for i, (start_p, end_p) in enumerate(l_pair): if start_p in part_line and end_p in part_line: start_xy = part_line[start_p] end_xy = part_line[end_p] bg = img.copy() X = (start_xy[0], end_xy[0]) Y = (start_xy[1], end_xy[1]) mX = np.mean(X) mY = np.mean(Y) length = ((Y[0] - Y[1])**2 + (X[0] - X[1])**2)**0.5 angle = math.degrees(math.atan2(Y[0] - Y[1], X[0] - X[1])) stickwidth = (kp_scores[start_p] + kp_scores[end_p]) + 1 polygon = cv.ellipse2Poly((int(mX), int(mY)), (int(length / 2), stickwidth), int(angle), 0, 360, 1) cv.fillConvexPoly(bg, polygon, line_color[i]) # cv2.line(bg, start_xy, end_xy, line_color[i], (2 * (kp_scores[start_p] + kp_scores[end_p])) + 1) transparency = max( 0, min(1, 0.5 * (kp_scores[start_p] + kp_scores[end_p]))) img = cv.addWeighted(bg, transparency, img, 1 - transparency, 0) img = cv.resize(img, (width, height), interpolation=cv.INTER_CUBIC) return img
def draw_convex_hull(img, points, color): points = cv2.convexHull(points) cv2.fillConvexPoly(img, points, color=color)
def remove_ui_clock(image): h, w = image.shape[:2] var = int(w / 6) triangle = np.array([[w - var, 0], [w, 0], [w, var]]) return cv2.fillConvexPoly(image, triangle, white)