python opencv cv2在图片中画mask掩码/掩膜
程序员文章站
2023-11-13 14:20:46
python opencv cv2在图片中画mask掩膜import cv2import numpy as npfrom PIL import Imageimport matplotlib.pyplot as pltmask_threth = 50img = cv2.imread('./1.jpg') #自己qq截图一张图片就行,要大于下面的坐标点# binary maskcoordinates = []coordinate1 = [[[40, 135], [168,132], [1...
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()
结果如图所示:
本文地址:https://blog.csdn.net/xjtdw/article/details/107073396
上一篇: “变位词”判断问题及算法复杂度