Reputation: 1189
I have a set of images with their bounding box values as shown below:
image_names class xmin xmax ymin ymax
image1.png 1 260 361 45 184
I wish to convert these bounding box values into a mask having the bounding box dimensions and filled with white pixels and store the masks for each image in the format "image1_mask.png" and so on. Do we have predefined functions to do this?
Upvotes: 0
Views: 7710
Reputation: 151
Simple approach
import cv2
import numpy as np
img = cv2.imread('image1.png') # read image
mask = np.zeros((img.shape[0],img.shape[1]),dtype=np.uint8) # initialize mask
mask[ymin:ymax,xmin:xmax] = 255 # fill with white pixels
cv2.imwrite('image1_mask.png',mask) # save mask
Upvotes: 3
Reputation: 104474
First create a blank image that is the same size as image1.png
:
import cv2
image = cv2.imread('image1.png', -1)
mask = image.copy()
mask[:] = 0
Next, use cv2.rectangle
to draw the white rectangle within the bounds of interest. Thankfully it takes in a minimum and maximum horizontal and vertical coordinate that define the top left and bottom right limits of the rectangle.
num_channels = 1 if len(mask.shape) == 2 else mask.shape[2]
cv2.rectangle(mask, (xmin, ymin), (xmax, ymax), color=(255,) * num_channels)
mask
will contain a white rectangle in the region of interest. Take note that in the cv2.rectangle
call, I've adapted this depending on whether the input image is grayscale or colour so you won't have to worry about doing any colour to grayscale conversion or vice versa.
Upvotes: 3