python opencv cv2在图片中画mask掩码/掩膜

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') #自己qq截图一张图片就行,要大于下面的坐标点

# binary mask
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)
# cv2.imwrite('./bbox_mask.png', bbox_mask)

img[bbox_mask] = img[bbox_mask] + color_mask * 0.5
img = img[:,:,::-1]
plt.imshow(img)
# plt.savefig('./result.png')
plt.show()

结果如图所示:
python opencv cv2在图片中画mask掩码/掩膜_第1张图片

你可能感兴趣的:(python使用,python,opencv,cv2,mask,掩码)