改变图像/掩码的背景
import cv2
import numpy as np
import time
def mask_adjust_background(
mask, mask_backgroud_color=(192, 192, 192),
mask_color=(255, 255, 255)
):
contours, _ = cv2.findContours(
mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
mask = cv2.cvtColor(
mask,
cv2.COLOR_GRAY2BGR
)
mask_background = np.ones_like(mask)*mask_backgroud_color
mask_visual = cv2.fillPoly(mask_background, contours, mask_color)
return mask_visual
if __name__ == '__main__':
start_time = time.time()
mask_path = "./masks/test.png"
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
mask_visual = mask_adjust_background(mask)
cv2.imwrite("./masks_backgroud_test/test.png", mask_visual)
print("--- %s seconds ---" % (time.time() - start_time))