python opencv cv2在图片中画mask掩膜
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
mask_threth = 50
img = cv2.imread('./1.jpg')
coordinates = []
coordinate1 = [[[40, 135], [168,132], [164,330], [2,328]]]
coordinate2 = [[[300, 300], [600,300], [600,600], [300,600]]]
coordinate1 = np.array(coordinate1)
coordinate2 = np.array(coordinate2)
coordinates.append(coordinate1)
coordinates.append(coordinate2)
mask = np.zeros(img.shape[:2], dtype=np.int8)
mask = cv2.fillPoly(mask, coordinates, 255)
cv2.imwrite('./mask1.png', mask)
bbox_mask = mask
color_mask = np.array([0, 0, 255], dtype=np.uint8)
bbox_mask = bbox_mask.astype(np.bool)
img[bbox_mask] = img[bbox_mask] + color_mask * 0.5
img = img[:,:,::-1]
plt.imshow(img)
plt.show()
结果如图所示: