I'm wanting to know if there's a better way of doing this, but given an image, and bounding box coordinates, how can I transform the pixels OUTSIDE of the bounding box transparent?
Here's a simple pseudo reproducible example, just have to provide an image for it from your own machine. Note this code is not working as you'll see, looking for the best option to do this:
import cv2
# Read in the image and clone it
image = cv2.imread("YOUR_IMAGE.jpg", cv2.IMREAD_UNCHANGED)
clone = image.copy()
# Define a bounding box
bound_box = {"xmin": "54", "ymin": "40", "xmax": "434", "ymax": "306"}
# Create 4 rectangles (left, top, right, and bottom) around bounding box coords minus 1 pixel.
# This would be the sample line of code to use 4 times
cv2.rectangle(clone, (x, y), (x+w, y+h), (0, 200, 0), -1)
# Transparency factor.
alpha = 0.0
# Add the transparency to the 4 rectangles?
clone = cv2.addWeighted(clone, alpha, image, 1 - alpha, 0)