def focus_zone(img, focus_zone): mask = np.zeros_like(img) channel_count = img.shape[2] match_mask_color = (255, ) * channel_count cv2.fillPoly(mask, focus_zone, match_mask_color) masked_image = cv2.bitwise_and(img, mask) return masked_image
def roi(img, vertices): mask = np.zeros_like(img) #channel_count = img.shape[2] match_mask_color = 255 cv2.fillPoly(mask, vertices, match_mask_color) masked_image = cv2.bitwise_and(img, mask) return masked_image
def road_mask(self) -> Mask: canvas = self.make_empty_mask() # FIXME Refactor that crap for road_waypoints in self._each_road_waypoints: road_left_side = [ lateral_shift(w.transform, -w.lane_width * 0.5) for w in road_waypoints ] road_right_side = [ lateral_shift(w.transform, w.lane_width * 0.5) for w in road_waypoints ] polygon = road_left_side + [x for x in reversed(road_right_side)] polygon = [self.location_to_pixel(x) for x in polygon] if len(polygon) > 2: polygon = np.array([polygon], dtype=np.int32) # FIXME Hard to notice the difference without polylines cv.polylines(img=canvas, pts=polygon, isClosed=True, color=COLOR_ON, thickness=5) cv.fillPoly(img=canvas, pts=polygon, color=COLOR_ON) return canvas
def region_of_interest(image): height = image.shape[0] triangle = np.array([[(200, height),(550, 250),(1100, height),]], np.int32) mask = np.zeros_like(image) cv2.fillPoly(mask, triangle, 255) masked_image = cv2.bitwise_and(image, mask) return masked_image
def create_emoticon(): emoticon = np.zeros([512, 512, 3], np.uint8) emoticon = cv2.circle(emoticon, (256, 256), 200, (0, 215, 255), -1) emoticon = cv2.ellipse(emoticon, (166, 200), (40, 30), 90, 0, 360, (0, 0, 0), -1) emoticon = cv2.ellipse(emoticon, (166, 200), (40, 30), 90, 0, 360, (255, 255, 255), 1) emoticon = cv2.ellipse(emoticon, (346, 200), (40, 30), 90, 0, 360, (0, 0, 0), -1) emoticon = cv2.ellipse(emoticon, (346, 200), (40, 30), 90, 0, 360, (255, 255, 255), 1) emoticon = cv2.circle(emoticon, (180, 175), 7, (255, 255, 255), -1) emoticon = cv2.circle(emoticon, (360, 175), 7, (255, 255, 255), -1) pts = [(130, 260), (150, 390), (362, 390), (382, 260), (256, 248)] cv2.fillPoly(emoticon, np.array([pts]), (208, 224, 64)) cv2.polylines(emoticon, np.array([pts]), True, (255, 255, 255), 2) cv2.line(emoticon, (150, 290), (362, 290), (209, 206, 0), 2) cv2.line(emoticon, (155, 330), (357, 330), (209, 206, 0), 2) cv2.line(emoticon, (160, 360), (352, 360), (209, 206, 0), 2) cv2.line(emoticon, (130, 260), (65, 200), (255, 255, 255), 2) cv2.line(emoticon, (382, 260), (447, 200), (255, 255, 255), 2) cv2.line(emoticon, (150, 390), (125, 405), (255, 255, 255), 2) cv2.line(emoticon, (362, 390), (387, 405), (255, 255, 255), 2) cv2.imshow('emoticon', emoticon) cv2.waitKey(0) cv2.destroyAllWindows()
def zone_mask(img, vertices): # blank mask: mask = np.zeros_like(img) # fill the mask cv2.fillPoly(mask, vertices, 255) # now only show the area that is the mask return cv2.bitwise_and(img, mask)
def masked_to_shape(image: np.array, shape: [Point]) -> np.array: """Masks the image to the given shape.""" mask = np.zeros(image.shape, dtype=np.uint8) channel_count = image.shape[2] if is_colored(image) else 1 mask_color = (255, ) * channel_count cv2.fillPoly(mask, Point.to_numpy(shape), mask_color) return cv2.bitwise_and(image, mask)
def region_of_interest(image): height = image.shape[0] polygons = np.array([(200, height), (1100, height), (550, 250)]) mask = np.zeros_like(image) cv2.fillPoly(mask, np.array([polygons], dtype=np.int32), 255) masked_image = cv2.bitwise_and(image, mask) return masked_image
def draw_area(undist, binary_warped, Minv, left_fit, right_fit): """ Parameter: undist: binary_wraped: Minv: left_fit: right_fit: Return: result: """ # Generate x and y values for plotting ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.shape[0]) left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2] right_fitx = right_fit[0] * ploty**2 + right_fit[1] * ploty + right_fit[2] # Create an image to draw the lines on warp_zero = np.zeros_like(binary_warped).astype(np.uint8) color_warp = np.dstack((warp_zero, warp_zero, warp_zero)) # Recast the x and y points into usable format for cv2.fillPoly() pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))]) pts_right = np.array( [np.flipud(np.transpose(np.vstack([right_fitx, ploty])))]) pts = np.hstack((pts_left, pts_right)) # Draw the lane onto the warped blank image cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 0)) # Warp the blank back to original image space using inverse perspective matrix (Minv) newwarp = cv2.warpPerspective(color_warp, Minv, (undist.shape[1], undist.shape[0])) # Combine the result with the original image result = cv2.addWeighted(undist, 1, newwarp, 0.3, 0) return result
def drawCross(img, color): #pts = np.array([[20,10],[20,20],[10,20],[10,30],[20,30],[20,40],[30,40],[30,30],[40,30],[40,20],[30,20],[30,10]], np.int32) pts = np.array([[14, 5], [14, 18], [4, 18], [4, 33], [14, 33], [14, 45], [34, 45], [34, 33], [44, 33], [44, 18], [34, 18], [34, 5]], np.int32) pts = pts.reshape((-1, 1, 2)) cv2.fillPoly(img, [pts], color)
def fill_contour(contour, ny, nx): "Fill the contour with white points" img = np.zeros((ny, nx, 3), dtype=np.uint8) cv2.fillPoly(img, [contour, ], (255, 255, 255)) return img
def roi(img): """ Lấy ROI của ảnh: Ảnh có kích thước (x, y) - (width, height) ROI là hình chữ nhật 4 points a, b, c, d: - a là điểm bên trái phía dưới - b là điểm bên phải phía dưới - c là điểm bên phải phía trên - d là điểm bên trái phía trên shape là array chứa 4 điểm HCN(a, b, c, d) - kích thước của ROI muốn tìm a, b, c, d dùng matplotlib sẽ show ảnh với tọa độ điểm trên ảnh: plt.imshow(image) plt.show() """ x = int(img.shape[1]) y = int(img.shape[0]) shape = np.array([[int(0), int(170)], [int(1600), int(170)], [int(1600), int(0)], [int(0), int(0)]]) #define a numpy array with the dimensions of img, but comprised of zeros mask = np.zeros_like(img) #Uses 3 channels or 1 channel for color depending on input image if len(img.shape) > 2: channel_count = img.shape[2] ignore_mask_color = (255, ) * channel_count else: ignore_mask_color = 255 #creates a polygon with the mask color cv2.fillPoly(mask, np.int32([shape]), ignore_mask_color) #returns the image only where the mask pixels are not zero masked_image = cv2.bitwise_and(img, mask) return masked_image
def region_of_interest(img, vertices): mask_func = np.zeros_like(img) #channel_count = img.shape[2] match_mask_color = 255 cv.fillPoly(mask_func, vertices, match_mask_color) # return masked_image = cv.bitwise_and(img, mask_func) return masked_image
def mask_image2(self, img, contours): img2 = img.copy() contours = contours.reshape((contours.shape[0], 1, contours.shape[1])) mask = np.zeros(img.shape[:-1], np.uint8) cv.fillPoly(mask, [contours], 255, cv.LINE_AA) mask_inv = cv.bitwise_not(mask) img2[mask_inv == 0] = (255, 255, 255) return img2
def region_of_interest( image): #identificarea zonei intre marcajele de pe sosea height = image.shape[0] polygons = np.array([[(200, height), (1100, height), (550, 250)]]) mask = np.zeros_like(image) cv2.fillPoly(mask, polygons, 255) masked_image = cv2.bitwise_and(image, mask) return masked_image
def polygons_to_mask(img_shape, polygons): mask = np.zeros(img_shape, dtype=np.uint8) polygons = np.asarray(polygons, np.int32) # 这里必须是int32,其他类型使用fillPoly会报错 shape = polygons.shape polygons = polygons.reshape(shape[0], -1, 2) cv2.fillPoly(mask, polygons, color=1) # 非int32 会报错 return mask
def detect_dummy(img, vp, config): height = img.shape[0] width = img.shape[1] vertices = np.array([[(0, height), (0, int(height * 2 / 3)), (vp[0], vp[1]), (width, int(height * 2 / 3)), (width, height)]]) mask = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8) cv2.fillPoly(mask, vertices, 255) return mask
def focus_zone(img, focus_zone): mask = np.zeros_like(img) channel_count = img.shape[2] match_mask_color = (255, ) * channel_count cv2.fillPoly(mask, focus_zone, match_mask_color) # Fill black to region out of focus_zone masked_image = cv2.bitwise_and(img, mask) # Keep pixel where isnt black in mask return masked_image
def mask_image(self, img, contours): img2 = img.copy() contours = contours.reshape( (contours.shape[0], 1, contours.shape[1])) # cv.fillPoly 형식에 맞춰서 넣어주기 위해서 mask = np.zeros(img.shape[:-1], np.uint8) cv.fillPoly(mask, [contours], 255, cv.LINE_AA) img2[mask == 0] = (255, 255, 255) return img2
def region_of_interest( image): # isolating the triangle of focus in the picture height = image.shape[0] width = image.shape[1] polygons = np.array([[(205, 621), (1254, 311), (751, 721), (1672, 429)]]) # creating the polygon mask = np.zeros_like(image) # the array of mask cv2.fillPoly(mask, polygons, 255) # triangle masked_image = cv2.bitwise_and(image, mask) # masking the image return masked_image
def mask_image(self, img, contours): img2 = img.copy() img2 = cv.cvtColor(img2, cv.COLOR_BGR2BGRA) contours = contours.reshape( (contours.shape[0], 1, contours.shape[1])) # cv.fillPoly 형식에 맞춰서 넣어주기 위해서 mask = np.zeros(img.shape[:-1], np.uint8) cv.fillPoly(mask, [contours], 255, cv.LINE_AA) img2[mask == 0] = (0, 0, 0, 0) self.mask = mask return img2
def region_of_interest(img, vertices): mask = np.zeros_like(img) if len(img.shape) == 3: channel_count = img.shape[2] match_mask_color = (255, ) * channel_count else: match_mask_color = 255 cv2.fillPoly(mask, vertices, match_mask_color) masked_image = cv2.bitwise_and(img, mask) return masked_image
def region_of_interest(image): height = image.shape[0] polygons = np.array([[(200, height), (1100, height), (550, 250)]]) mask = np.zeros_like(image) # Fill poly-function deals with multiple polygon cv.fillPoly(mask, polygons, 255) # Bitwise operation between canny image and mask image masked_image = cv.bitwise_and(image, mask) return masked_image
def roi_cropping(image, points): """ 将游戏画面裁剪,只保留道路相关区域,丢弃天空等无关信息 :param image: :param points: :return: """ mask = np.zeros_like(image) cv2.fillPoly(mask, [points], 255) masked = cv2.bitwise_and(image, mask) return masked
def draw_landmarks(image: np.array, landmarks: Landmarks, points=False, draw_bg=False, color=None) -> None: """Draws facial landmarks on the specified image.""" lm = landmarks alpha = Config.Landmarks.ALPHA if alpha <= 0.0: return if color is None: color = Config.Landmarks.BASE_COLOR mouth_color = Config.Landmarks.MOUTH_COLOR eye_color = Config.Landmarks.EYE_COLOR else: mouth_color = color eye_color = color if draw_bg: bg_alpha = 0.2 overlay = image.copy() cv2.fillPoly(overlay, Point.to_numpy(lm.outer_shape), color) cv2.addWeighted(overlay, bg_alpha, image, 1.0 - bg_alpha, 0, image) thickness = Config.Landmarks.THICKNESS line_type = Config.Landmarks.LINE_TYPE overlay = image.copy() if alpha < 1.0 else image for pts in [lm.chin, lm.left_eyebrow, lm.right_eyebrow, lm.nose_bridge, lm.nose_tip]: if points: for point in pts: cv2.circle(overlay, point, thickness, color, thickness, line_type) else: cv2.polylines(overlay, Point.to_numpy(pts), False, color, thickness, line_type) for pts in [lm.left_eye, lm.right_eye]: if points: for point in pts: cv2.circle(overlay, point, thickness, eye_color, thickness, line_type) else: cv2.polylines(overlay, Point.to_numpy(pts), True, eye_color, thickness, line_type) for pts in [lm.top_lip, lm.bottom_lip]: if points: for point in pts: cv2.circle(overlay, point, thickness, mouth_color, thickness, line_type) else: cv2.polylines(overlay, Point.to_numpy(pts), True, mouth_color, thickness, line_type) if alpha < 1.0: cv2.addWeighted(overlay, alpha, image, 1.0 - alpha, 0.0, image)
def get_cityscapes_rm_da(img, semantics): """Applies two adaptive filters to find road markings""" semantics_road = (128, 64, 128) mask_road = cv2.inRange(semantics, semantics_road, semantics_road) # cv2.imwrite('temp/cityscapes_road_mask.png', mask_road) height = img.shape[0] width = img.shape[1] vertices = np.array([[(0, height), (0, int(height * 3 / 4)), (width, int(height * 3 / 4)), (width, height)]]) mask_bottom = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8) cv2.fillPoly(mask_bottom, vertices, 255) mask_top = cv2.bitwise_not(mask_bottom) mask_road_top = cv2.bitwise_and(mask_road, mask_road, mask=mask_top) mask_road_bottom = cv2.bitwise_and(mask_road, mask_road, mask=mask_bottom) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # img = cv2.bitwise_and(img, img, mask=mask) thr_adaptive_top = masked_adaptive_threshold(img, mask_road_top, max_value=255, size=101, C=-15) thr_adaptive_top = cv2.convertScaleAbs(thr_adaptive_top) thr_adaptive_top = cv2.bitwise_and(thr_adaptive_top, thr_adaptive_top, mask=mask_road_top) thr_adaptive_bottom = masked_adaptive_threshold(img, mask_road_bottom, max_value=255, size=251, C=-15) thr_adaptive_bottom = cv2.convertScaleAbs(thr_adaptive_bottom) thr_adaptive_bottom = cv2.bitwise_and(thr_adaptive_bottom, thr_adaptive_bottom, mask=mask_road_bottom) thr_adaptive_combined = cv2.bitwise_or(thr_adaptive_top, thr_adaptive_bottom) plt.figure() plt.subplot(3, 1, 1) plt.imshow(img, cmap='gray') plt.subplot(3, 1, 2) plt.imshow(thr_adaptive_top, cmap='gray') plt.subplot(3, 1, 3) plt.imshow(thr_adaptive_bottom, cmap='gray') return thr_adaptive_combined
def getCoverage(self, waypoints, return_map=False): num_agents = waypoints.shape[0] if self._all_waypoints is None: raise ValueError('Map has no waypoints') cover_map = np.copy(self._img) draw_map = np.copy(cover_map) buffer_mask = np.logical_and(self._safety_img < 0.3, self._safety_img > 0) travel_cost = np.zeros(num_agents) turning_cost = np.zeros(num_agents) for agent in range(num_agents): prev_theta = 0 for idx, wpt in enumerate(waypoints[agent]): if wpt == -1 or idx == len(waypoints[agent]) - 1: break wpt_loc = self._all_waypoints[wpt] wpt_theta = self._traverse_angles[wpt, waypoints[agent][idx + 1]] delta_theta = got.rad_wrap_pi(wpt_theta - prev_theta) travel_cost[agent] += self._traverse_dists[wpt, waypoints[agent][idx + 1]] turning_cost[agent] += self._rho * abs(delta_theta / (np.pi / 2))**self._rho center = (int(wpt_loc[1]), int(wpt_loc[0])) frustum = self._traverse_frustum[wpt, waypoints[agent][idx + 1]] cv2.fillPoly(draw_map, [frustum], 1, offset=center) #this line returns coverage of total pixel seen internal_coverage = (np.sum(draw_map) - self._num_occluded) / ( draw_map.size - self._num_occluded) #this line returns coverage of buffer seen (more important for mapping) wall_coverage = np.sum( draw_map[buffer_mask]) / draw_map[buffer_mask].size #blended coverage coverage = self._coverage_blend * wall_coverage + ( 1 - self._coverage_blend) * internal_coverage # minimize negative coverage and minimize travel distance if return_map: return -coverage, travel_cost, turning_cost, draw_map else: return -coverage, travel_cost, turning_cost
def region_of_interest(img, vertices): # Define a blank matrix that matches the image height/width. mask = np.zeros_like(img) # Retrieve the number of color channels of the image. channel_count = img.shape[2] # Create a match color with the same color channel counts. match_mask_color = (255,) * channel_count # Fill inside the polygon cv2.fillPoly(mask, vertices, match_mask_color) # Returning the image only where mask pixels match masked_image = cv2.bitwise_and(img, mask) return masked_image
def get_final_img(): min_x, max_y, max_x, min_y = get_points() square_img = img[min_y:max_y, min_x:max_x] mask = np.zeros(square_img.shape, square_img.dtype) cv2.fillPoly( img_to_cut, [np.asarray(all_points, dtype=np.int32)], (255, 255, 255)) mask = img_to_cut[min_y:max_y, min_x:max_x] mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) _, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY) mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) final = cv2.bitwise_and(square_img, mask) return final
def agent_vehicle_mask(self, agent: carla.Actor) -> Mask: canvas = self.make_empty_mask() bb = agent.bounding_box.extent corners = [ carla.Location(x=-bb.x, y=-bb.y), carla.Location(x=bb.x, y=-bb.y), carla.Location(x=bb.x, y=bb.y), carla.Location(x=-bb.x, y=bb.y), ] agent.get_transform().transform(corners) corners = [self.location_to_pixel(loc) for loc in corners] cv.fillPoly(img=canvas, pts=np.int32([corners]), color=COLOR_ON) return canvas