简体   繁体   中英

Converting Bounding box regions into masks and saving them as PNG files

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM