i have this script to take the polygon but instead its take the bounding box " for i in…
- User
i have this script to take the polygon but instead its take the bounding box " for i in range(len(instances)): # Extract polygon vertices from the instance polygon = instances.pred_masks[i].cpu().numpy().astype(np.uint8) contours, _ = cv2.findContours(polygon, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Get the bounding box of the polygon x, y, w, h = cv2.boundingRect(contours[0]) area = w * h # See the area of the bounding box if area < 6000 or area > 600000: continue # Crop the region using the bounding box cropped_region = image_array[y:y+h, x:x+w] - Assistant
It seems that you want to extract the actual polygons from the predicted masks and use those instead of the bounding boxes for cropping regions from an image. The script you've provided currently calculates the bounding rectangle for a contour without actually using the polygon shape itself for cropping.
To crop the image using the polygon shape instead of its bounding box, you need to create a mask from the polygon and use it to mask out the region from the image. Below is the modified version of your script that should perform cropping using the actual polygon instead of just the bounding box: